Autofac Hangfire-请求的服务“ Microsoft.Owin.IOwinContext”尚未注册

时间:2018-06-21 14:44:23

标签: asp.net-web-api2 autofac hangfire hangfire-autofac

我正在使用Hangfire.Autofac软件包尝试使Hangfire与我的Web Api项目一起工作。

我在Startup类(使用Owin)中注册了两项服务,第一个看起来像这样

builder.RegisterType<SimpleService>().As<ISimpleService>().InstancePerDependency();

另一个比较复杂,看起来像这样

builder.Register(x =>
{
  var client = new CrmServiceClient(connString);
  var serviceProxy = client.OrganizationServiceProxy;
  serviceProxy.EnableProxyTypes();

  var owin = x.Resolve<IOwinContext>();
  var idClaim = owin?.Authentication?.User?.Claims?.FirstOrDefault(y => y.Type == CrmImpersonationClaimType)?.Value;
  Guid id;
  if (Guid.TryParse(idClaim, out id))
   {
        serviceProxy.CallerId = id;
   }
  return serviceProxy;
}).As<IOrganizationService>().ExternallyOwned().InstancePerDependency();

IOrganizationService被注入到SimpleService中-每当我在SimpleService中调用我的方法时,都会收到错误消息。如果我不注入IOrganizationService,则所有其他依赖项都可以解决。

错误

Autofac.Core.Registration.ComponentNotRegisteredException

所请求的服务“ Microsoft.Owin.IOwinContext”尚未注册。为避免此异常,请注册一个组件以提供服务,或者使用IsRegistered()检查服务注册,或者使用ResolveOptional()方法解决可选的依赖项。

0 个答案:

没有答案
相关问题