【发布时间】:2015-04-14 00:00:17
【问题描述】:
我无法找到通过 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),
});
}
有人有建议吗?
【问题讨论】:
标签: c# inversion-of-control autofac owin startup