无法访问已处置的对象。对象名称:“ IServiceProvider”

时间:2019-03-29 08:48:47

标签: c# asp.net-core

我在启动时注册了两个httpclient。我想根据其他服务中的不同名称获取httpclient

public class WechatLogicService : IWechatLogicService{
    private readonly IServiceProvider provider;
    private readonly IOptions<AppSettings> options;
    private readonly ILogger<WechatLogicService> logger;
    private IWeChatRefundHttpClinet httpClient;
    private readonly IHttpContextAccessor accessor;
    public WechatLogicService(IHttpContextAccessor accessor,
            ILogger<WechatLogicService> logger,
              IServiceProvider serviceProvider,
                IOptions<AppSettings> options)
        {
            this.accessor = accessor;
            this.options = options;
            this.logger = logger;
            provider = serviceProvider;
        }
     public void Refund(){
           settings = options.Value.WeChatSettings.Settings.First(p => p.MchId 
                   == refundDto.Account);
           httpClient = provider.GetServices<IWeChatRefundHttpClinet> 
                   ().First(p => p.GetType().Name.Contains(settings.Scoped));
     }
 }

启动:

services.AddHttpClient<IEnterpriseHttpClient, EnterpriseAppHttpClient>(option =>
{
    option.BaseAddress = new Uri(Configuration.GetSection("EnterpriseAppSettings")["Domain"].ToString());
});
services.AddHttpClient<IEnterpriseHttpClient, EnterpriseNormalHttpClient>(option =>
{
    option.BaseAddress = new Uri(Configuration.GetSection("EnterpriseNormalSettings")["Domain"].ToString());
});
services.AddScoped<IWechatLogicService, WechatLogicService>();

它引发异常:

  

无法访问已处置的对象。对象名称:'IServiceProvider'。

我对自己的代码出了什么问题感到困惑?

0 个答案:

没有答案