ASP.NET MVC AutoMapper 的 InstancePerRequest 问题

MangoOzz 2015-07-13 10:29:09
我在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);

依旧不行,这是为什么,应该怎么弄?
...全文
229 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
newtee 2015-07-13
  • 打赏
  • 举报
回复
那里=哪里
newtee 2015-07-13
  • 打赏
  • 举报
回复
自己看看那里写错了吧
newtee 2015-07-13
  • 打赏
  • 举报
回复
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();
        }
    }
}
MangoOzz 2015-07-13
  • 打赏
  • 举报
回复
引用 2 楼 zhuankeshumo 的回复:
自己看看那里写错了吧
郁闷,原来要AutofacDependencyResolver.Current.RequestLifetimeScope.Resolve<T>() 这么用。 谢谢
Manning Publications, 2009 ASP.NET MVC implements the Model-View-Controller pattern on the ASP.NET runtime. It works well with open source projects like NHibernate, Castle, StructureMap, AutoMapper, and MvcContrib. ASP.NET MVC in Action is a guide to pragmatic MVC-based web development. After a thorough overview, it dives into issues of architecture and maintainability. The book assumes basic knowledge of ASP.NET (v. 3.5) and expands your expertise. Some of the topics covered: * How to effectively perform unit and full-system tests. * How to implement dependency injection using StructureMap or Windsor * How to work with the domain and presentation model * How to work with persistence layers like NHibernate The book's many examples are in C#. "Shows how to put all the features of ASP.NET MVC together to build a great application." -From the Foreword by Phil Haack, Senior Program Manager, ASP.NET MVC Team, Microsoft "This book put me in control of ASP.NET MVC." -Mark Monster, Software Engineer, Rubicon "Of all the offerings, this one got it right!" -Andrew Siemer, Principal Architect, OTX Research "Highly recommended for those switching from Web Forms to MVC." -Frank Wang, Chief Software Architect, DigitalVelocity LLC About the Author All three authors are popular bloggers and Alt.Net developers. From Houston and Austin, TX, Jeffrey, Ben, and Jimmy are frequent user group and conference speakers. They are also committers to the open-source projects MvcContrib and CodeCampServer.

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧