我正在使用 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 …