InstancePerRequest 的 MvcSiteMapProvider 和 Autofac 问题

MvcSiteMapProvider and Autofac issue with InstancePerRequest

我已经安装了 nutget 包 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();
    }
}

更新

来自 here:

The error pretty much says it all - something in your registrations has a dependency that is registered as InstancePerRequest and it's being resolved outside of a web request.


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

InstancePerDependency

Instance Per Dependency

在其他容器中也称为“transient”或“factory”。使用每个依赖范围,每个服务请求都会返回一个唯一的实例。