相关疑难解决方法(0)

使用动态基地址改装客户端

我正在使用 Refit 使用 asp.net core 2.2 中的 Typed Client 调用 API,该 API 当前使用我们的配置选项中的单个 BaseAddress 进行引导:

services.AddRefitClient<IMyApi>()
        .ConfigureHttpClient(c => { c.BaseAddress = new Uri(myApiOptions.BaseAddress);})
        .ConfigurePrimaryHttpMessageHandler(() => NoSslValidationHandler)
        .AddPolicyHandler(pollyOptions);
Run Code Online (Sandbox Code Playgroud)

在我们的配置 json 中:

"MyApiOptions": {
    "BaseAddress": "https://server1.domain.com",
}
Run Code Online (Sandbox Code Playgroud)

在我们的 IMyApi 界面中:

public IMyAPi interface {
        [Get("/api/v1/question/")]
        Task<IEnumerable<QuestionResponse>> GetQuestionsAsync([AliasAs("document_type")]string projectId);
}
Run Code Online (Sandbox Code Playgroud)

当前服务示例:

public class MyProject {
     private IMyApi _myApi;
     public MyProject (IMyApi myApi) {
        _myApi = myApi;
     }

    public Response DoSomething(string projectId) {
        return _myApi.GetQuestionsAsync(projectId);
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在需要在运行时根据数据使用不同的 BaseAddresses。我的理解是 Refit 将 HttpClient 的单个实例添加到 DI …

c# dotnet-httpclient refit asp.net-core-2.2

7
推荐指数
1
解决办法
3593
查看次数

标签 统计

asp.net-core-2.2 ×1

c# ×1

dotnet-httpclient ×1

refit ×1