【发布时间】:2019-11-21 17:32:58
【问题描述】:
我们有一个 dotnet-core 2.1 web-api 项目设置如下。
我们正在尝试将 MediatR 与 HttpClient 一起使用,并在 HttpMagicService 中为不同的响应发送事件。在调用 HttpClient.SendAsync() 后尝试使用注入服务时出现问题。
IMagicService 和 MediatR 的初始设置。
public void ConfigureServices(IServiceCollection services)
{
// other services configured above
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestLoggingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPerformanceBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));
services.AddMediatR(typeof(ApplicaitonCommand).GetTypeInfo().Assembly);
services.AddHttpClient<IMagicService, HttpMagicService>();
}
IMagicService 的实现。
public class HttpMagicService : IMagicService
{
private readonly IMediator mediator;
private readonly HttpClient httpClient;
public HttpMagicService(IMediator mediator, HttpClient httpClient)
{
this.mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}
public async Task AddAsync(Magic data, Metadata metadata)
{
// This one works
await this.mediator.Publish(new MagicAddInitIntegrationEvent(data));
var url = metadata.RemoteUri + "/command/AddMagic";
using (var request = new HttpRequestMessage(HttpMethod.Post, url))
{
try
{
var httpContent = CreateHttpContent(data);
request.Content = httpContent;
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", metadata.AuthKey);
// httpClient clears the ResolvedServices?!
var response = await this.httpClient.SendAsync(request).ConfigureAwait(false);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
// This one fails
await this.mediator.Publish(new MagicAddIntegrationEvent(data));
}
else
{
await this.mediator.Publish(new MagicAddFailedIntegrationEvent(data));
}
}
catch (Exception ex)
{
await this.mediator.Publish(new MagicAddFailedIntegrationEvent(meeting, ex));
}
}
}
private static HttpContent CreateHttpContent(object content)
{
// ...
}
}
对于我们的第一次调用await this.mediator.Publish(new MagicAddInitIntegrationEvent(data));,事件已发布并且处理程序可以处理该事件。
查看this.mediator,我们看到有一个已解决的服务。
当我们运行var response = await this.httpClient.SendAsync(request).ConfigureAwait(false); 时,似乎httpClient 只是决定为每个人处理所有服务。 ????
在这行之后查看this.mediator 时,我们看到所有内容都已清理完毕。
由于没有服务,调用 await this.mediator.Publish(new MagicAddIntegrationEvent(data)); 将无法运行,我们得到一个异常。
这是this.httpClient.SendAsync 的预期行为吗?我们应该以另一种方式来做吗?
似乎可行的一种解决方案是忽略设置services.AddHttpClient<IMagicService, HttpMagicService>();,而是为HttpClient 设置单例服务,这似乎可行,但我不确定这是否是正确的方法。
解决方案
使用了 Panagiotis Kanavos 的建议并做了一个作用域 httpClient,所以我的解决方案变成了这个。
public void ConfigureServices(IServiceCollection services)
{
// other services configured above
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestLoggingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPerformanceBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));
services.AddMediatR(typeof(ApplicaitonCommand).GetTypeInfo().Assembly);
services.AddScoped<HttpClient>();
}
如果尝试在范围内使用 IMediator 而不使用 var mediator= scope.ServiceProvider.GetRequiredService<IMediator>();,则会出现与之前相同的问题。所以我错过了 DI 容器的一些范围问题。
public class HttpMagicService : IMagicService
{
private readonly IMediator mediator;
private readonly IServiceProvider services;
public HttpMagicService(IServiceProvider services)
{
this.services = services?? throw new ArgumentNullException(nameof(services));
}
public async Task AddAsync(Magic data, Metadata metadata)
{
using (var scope = Services.CreateScope())
{
var httpClient = scope.ServiceProvider.GetRequiredService<HttpClient>();
var mediator= scope.ServiceProvider.GetRequiredService<IMediator>();
...
}
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: c# asp.net-core dotnet-httpclient mediatr