繁体   English   中英

MvcSiteMapProvider和InstancePerRequest的Autofac问题

[英]MvcSiteMapProvider and Autofac issue with InstancePerRequest

我已经安装了坚果包MvcSiteMapProvider.MVC5.DI.Autofac.Modules 我正在尝试将我的DBContext注册为InstancePerRequest。 但是这失败并显示错误

No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested.

如果我将DBContext更改为InstancePerLifetimeScope,一切都很好。 在文件中抛出错误

DI\Autofac\Modules\MvcSiteMapProviderModule.cs    Line: 195

实际上,如果我尝试向InstancePerRequest注册任何自己的类型,则会收到此错误。 我是Autofac的新手,所以真的不了解MvcSiteMapProvider Autofac的nuget包中的很多代码。 当我学习有关Autofac的更多信息时,希望有人可以为如何解决此问题指明正确的方向?

编辑:

从Autofac文档中,出现错误是因为:

Code is running during application startup (e.g., in an ASP.NET Global.asax) that uses dependency resolution when there isn’t an active request yet.

根据MvcSiteMapProvider文档,这行是必需的,所以我可以将其移动到其他地方吗?

// Setup global sitemap loader (required)
MvcSiteMapProvider.SiteMaps.Loader = container.Resolve<ISiteMapLoader>();

编辑2:

protected void Application_Start()
{
    // BEGIN: Autofac Config
    var builder = new ContainerBuilder();

    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    builder.RegisterControllers(typeof(MvcApplication).Assembly);
    builder.RegisterSource(new ViewRegistrationSource());

    // Register context and unit of work here
    IoC.Dependencies.Register.RegisterTypes(builder);

    builder.RegisterModule(new MvcSiteMapProviderModule());
    builder.RegisterModule(new MvcModule());

    var container = builder.Build();

    MvcSiteMapProvider.SiteMaps.Loader = container.Resolve<ISiteMapLoader>();

    GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    // END: Autofac Config

    Helpers.Log4NetManager.InitializeLog4Net();
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);


    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
}

您正在使用Autofac两次注册控制器。

// This registers the all of the controllers in the application
builder.RegisterControllers(typeof(MvcApplication).Assembly);

// And so does this...
builder.RegisterModule(new MvcModule());

MvcModule内容:

public class MvcModule
    : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        var currentAssembly = typeof(MvcModule).Assembly;

        builder.RegisterAssemblyTypes(currentAssembly)
            .Where(t => typeof(IController).IsAssignableFrom(t))
            .AsImplementedInterfaces()
            .AsSelf()
            .InstancePerDependency();
    }
}

UPDATE

这里

该错误几乎说明了一切-您注册中的某个内容具有一个依赖项,该依赖项已注册为InstancePerRequest,并且正在Web请求外部进行解析。


我不确定将DBContext注册为InstancePerRequest是否是一个好主意。 MvcSiteMapProvider会在创建任何请求上下文之前加载,因此,如果您使用的是动态节点提供程序或访问数据库的自定义ISiteMapNodeProvider实现,则它将无法正常工作。 更好的选择是使用不依赖于HttpContext InstancePerDependency

每个依赖实例

在其他容器中也称为“瞬态”或“工厂”。 使用每个依赖关系范围,每个服务请求都将返回一个唯一实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM