c# – 在Owin Startup上解析InstancePerLifetimeScope中的Autofa
发布时间:2020-12-15 23:45:46 所属栏目:百科 来源:网络整理
导读:我无法找到通过Autofac解析服务的正确方法,该方法在构造Owin上下文时使用,并且也在请求端处理. 由于此时OwinContext仍在构建中,因此无法通过调用HttpContext.Current.GetOwinContext().GetAutofacLifetimeScope()找到LifetimeScope. OwinContext还没有. 在我
我无法找到通过Autofac解析服务的正确方法,该方法在构造Owin上下文时使用,并且也在请求端处理.
由于此时OwinContext仍在构建中,因此无法通过调用HttpContext.Current.GetOwinContext().GetAutofacLifetimeScope()找到LifetimeScope. OwinContext还没有. 在我的代码中,IAdfsAuthorizationProvider服务直接在Container处解析,但在请求之后不会被处理并且存活时间更长. 我可以通过调用container.BeginLifetimeScope()来创建一个新的LifeTimeScope,但现在LifeTime被分离了请求,并且可能会解析同一请求中的不同实例.并且无法在正确的时间自行处理lifeTimeScope. public void Configure(IAppBuilder app) { var builder = new ContainerBuilder(); builder.RegisterType<AdfsAuthorizationProvider>().As<IAdfsAuthorizationProvider>().InstancePerLifetimeScope(); var container = builder.Build(); app.UseAutofacMiddleware(container); // **Service that's not binding to the request lifetime.** var service = container.Resolve<IAdfsAuthorizationProvider>(); app.USEOAuthAuthorizationServer(new OAuthAuthorizationServerOptions { Provider = new AuthorizationServerProvider(service),}); } 有没有人有建议? 解决方法
OAuthAuthorizationServerProvider是一个为所有请求调用方法的单例.
此类尚未设计为注入.如果要解析一个请求的依赖关系,则必须使用每个方法提供的owinContext手动解析它 public class AuthorizationServerProvider : OAuthAuthorizationServerProvider { public override Task GrantAuthorizationCode( OAuthGrantAuthorizationCodeContext context) { IAdfsAuthorizationProvider authorizationProvider = context.OwinContext .GetAutofacLifetimeScope() .Resolve<IAdfsAuthorizationProvider>(); return base.GrantAuthorizationCode(context); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |