我得到以下异常“实例无法解析,并且无法从此LifetimeScope中创建嵌套生命周期,因为它已经被处理掉了。”当我尝试从global.asax Application_EndRequest事件中解析对象时。我在2.5.2.830版本中使用Autofac
public class Global : System.Web.HttpApplication, IContainerProviderAccessor
{
// Provider that holds the application container.
static Autofac.Integration.Web.IContainerProvider _containerProvider;
// Instance property that will be used by Autofac HttpModules
// to resolve and inject dependencies.
public Autofac.Integration.Web.IContainerProvider ContainerProvider
{
get { return _containerProvider; }
}
protected void Application_Start(object sender, EventArgs e)
{
var builder = new ContainerBuilder();
...
_containerProvider = new ContainerProvider(builder.Build());
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
ISession session = _containerProvider.RequestLifetime.Resolve<ISession>();
session.BeginTransaction();
}
private void Application_EndRequest(object sender, EventArgs e)
{
ISession session = ContainerProvider.RequestLifetime.Resolve<ISession>();
}
我以这种方式注册:
builder.Register(x =&gt; x.Resolve()。OpenSession())。As()。InstancePerHttpRequest(); }
答案 0 :(得分:6)
Autofac通过名为Autofac.Integration.Web.ContainerDisposalModule的HttpModule进行会话处理。
你需要
OR
private void Application_EndRequest(object sender, EventArgs e) { ISession session = ContainerProvider.RequestLifetime.Resolve(); //cleanup transaction etc... ContainerProvider.EndRequestLifetime(); }
OR
public class SessionManager : IDisposable
{
private readonly ISession _session;
private ITransaction _transaction;
public SessionManager(ISession session)
{
_session = session;
}
public void BeginRequest()
{
_transaction = _session.BeginTransaction();
}
#region Implementation of IDisposable
///
/// Dispose will be called automatically by autofac when the lifetime ends
///
public void Dispose()
{
//commit rollback, whatever
_transaction.Commit();
}
#endregion
}
您必须确保初始化会话管理器。
protected void Application_BeginRequest(object sender, EventArgs e)
{
SessionManager manager = _containerProvider.RequestLifetime.Resolve();
manager.BeginRequest();
}