Code Bye

ASP.NET MVC AutoMapper 的 InstancePerRequest 问题

我在AutoMapper里用的InstancePerRequest作为生命周期,但是会报错

引用

No scope with a Tag matching “”AutofacWebRequest”” is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.

我按照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);

依旧不行,这是为什么,应该怎么弄?

40分

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();
        }
    }
}
 
自己看看那里写错了吧
 
那里=哪里
 
引用 2 楼 zhuankeshumo 的回复:

自己看看那里写错了吧

郁闷,原来要AutofacDependencyResolver.Current.RequestLifetimeScope.Resolve<T>() 这么用。
谢谢


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明ASP.NET MVC AutoMapper 的 InstancePerRequest 问题