我在AutoMapper里用的InstancePerRequest作为生命周期,但是会报错
我按照http://docs.autofac.org/en/latest/faq/per-request-scope.html,这里说的实现了ILifetimeScopeProvider接口SimpleLifetimeScopeProvider ,并且在初始化的时候注册了
var provider= new SimpleLifetimeScopeProvider(container);
var resolver = new AutofacDependencyResolver(container, provider);
DependencyResolver.SetResolver(resolver);
依旧不行,这是为什么,应该怎么弄?
------解决思路----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Core.Lifetime;
using Autofac.Integration.Mvc;
namespace MvcApplication9
{
// 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
// 请访问 http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<MyService>().InstancePerRequest();
builder.RegisterType<Class1>().InstancePerRequest();
var container = builder.Build();
var provider = new SimpleLifetimeScopeProvider(container);
var resolver = new AutofacDependencyResolver(container, provider);
DependencyResolver.SetResolver(resolver);
}
}
}
using Autofac;
using Autofac.Core.Lifetime;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication9
{
public class SimpleLifetimeScopeProvider : ILifetimeScopeProvider
{
private readonly IContainer _container;
private ILifetimeScope _scope;
public SimpleLifetimeScopeProvider(IContainer container)
{
this._container = container;
}
public ILifetimeScope ApplicationContainer
{
get { return this._container; }
}
public void EndLifetimeScope()
{
if (this._scope != null)
{
this._scope.Dispose();
this._scope = null;
}
}
public ILifetimeScope GetLifetimeScope(Action<ContainerBuilder> configurationAction)
{
if (this._scope == null)
{
this._scope = (configurationAction == null)
? this.ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag)
: this.ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag, configurationAction);
}
return this._scope;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication9
{
public class MyService
{
public string Test()
{
return "Test";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication9
{
public class Class1
{
public string Test()
{
return "Test";
}
}
}
using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication9.Controllers
{
public class HomeController : Controller
{
private readonly MyService _myservice;
public HomeController(MyService myservice)
{
_myservice = myservice;
}
public ActionResult Index()
{
var _myservice2= AutofacDependencyResolver.Current.RequestLifetimeScope.Resolve<Class1>();
ViewBag.Message = _myservice.Test() + "
------解决思路----------------------
" + _myservice2.Test();
return View();
}
public ActionResult About()
{
ViewBag.Message = "你的应用程序说明页。";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "你的联系方式页。";
return View();
}
}
}