В какой -то момент мы изменили конфигурацию OWIN, чтобы получить одинаковую жизнь LifeTimesCope в нашем коде промежуточного программного обеспечения OWIN и в контроллерах Web API 2 (переключен с использования GlobalConfiguration.Configuration.DependendencyResolver на new httpconfiguration.). Овин). В этот момент логика очистки в конце сеанса и в конце приложения стала сломанной, и у нас есть объект objectDisposedException < /code> попытки разрешить услугу через глобальный контейнер Autofac: < /p>
system.objectdisposedExexexcept Уже был расположен. AutoFacweBapidePendencyResolver )
[*] Созданный код в OWIN ShuttingDown Callback
[*]
Код: Выделить всё
MvcApplication.Session_EndВопрос в том, что идет не так? Контейнер, расположенный до session_end перед «разбившим изменением»?Global.asax.cs:
protected void Application_Start()
{
var container = ConfigureAutofac();
AutofacHelper.Container = container;
GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
protected void Session_End(Object sender, EventArgs e)
{
AutofacHelper.Container.Resolve().ReleaseLocksBySessionId(Session.SessionID);
}
protected void Application_End()
{
AutofacHelper.Container.Resolve().ReleaseAllLocks();
}
< /code>
Startup.cs:
public void Configuration(IAppBuilder app)
{
app.UseAutofacMiddleware(AutofacHelper.Container);
ConfigureSession(app);
//init
ConfigureOAuth(app);
//signalr
app.MapSignalR();
//post auth
ConfigurePostAuthentification(app);
}
< /code>
This version doesn't work for cleaning up:
Global.asax.cs:
protected void Application_Start()
{
var container = ConfigureAutofac();
AutofacHelper.Container = container;
GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
protected void Session_End(Object sender, EventArgs e)
{
AutofacHelper.Container.Resolve().ReleaseLocksBySessionId(Session.SessionID);
}
protected void Application_End()
{
AutofacHelper.Container.Resolve().ReleaseAllLocks();
}
< /code>
Startup.cs:
public void Configuration(IAppBuilder app)
{
// ***** attempt to execute cleaning up code before autofac container disposal ****
var context = new OwinContext(app.Properties);
var token = context.Get("host.OnAppDisposing");
if (token != CancellationToken.None)
{
token.Register(() =>
{
Console.WriteLine("Unfortunately this is called after autofac container disposal.")
});
}
// Autofac & user context
app.UseAutofacMiddleware(AutofacHelper.Container);
app.UseAutofacMvc();
var config = new HttpConfiguration();
config.DependencyResolver = new AutofacWebApiDependencyResolver(AutofacHelper.Container);
// Nodatime-Typen registrieren.
var formatters = config.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
WebApiConfig.Register(config);
app.UseAutofacWebApi(config);
ConfigureSession(app);
ConfigureOAuth(app);
// SignalR
app.MapSignalR();
// post auth
ConfigurePostAuthentication(app);
app.UseWebApi(config);
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... n-shutdown
Мобильная версия