Autofac失去上的Web.Config编辑注册 [英] Autofac Losing Registrations on Web.Config Edit

查看:546
本文介绍了Autofac失去上的Web.Config编辑注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的ASP.NET MVC 4的WebAPI和其他一些组件构建分层的Web应用程序。我使用Autofac 2.6.2.859的最新版本为我与MVC和的WebAPI整合功能DI容器。我有autofac模块安装在我的不同层次,我使用的是新RegisterAssemblyModules扫描的AppDomain组件的各种模块。

在每次启动的伟大工程。当我编辑web.config文件和应用程序升温,我的注册都将丢失。我得到一个DependencyResolutionException -


  

以公共绑定标志的类型My.Class.Name'发现的建设者都不能在可用的服务和参数来调用:


所以我的注册没有被重新加载。有谁知道如何解决这个问题?我应该把我的初始code别的地方以外的Application_Start()?

更新

下面就是我的code看起来像

 公共静态类的IoC
{
    公共静态无效配置()
    {
        VAR建设者=新ContainerBuilder();
        无功组件= AppDomain.CurrentDomain.GetAssemblies();        //大会模块
        builder.RegisterAssemblyModules< NSUTriviaModuleBase>(组件);        //注册使用汇编扫描API控制器。
        builder.RegisterControllers(组件);        //注册使用汇编扫描API控制器。
        builder.RegisterApiControllers(组件);        变种容器= builder.Build();
        //设置依赖解析器的实现。
        VAR解析器=新AutofacWebApiDependencyResolver(容器);        GlobalConfiguration.Configuration.DependencyResolver =解析器;
        DependencyResolver.SetResolver(新AutofacDependencyResolver(容器));
    }
}


解决方案

我认为这个问题是所有组件加载到AppDomain中第一次启动应用程序时,但是当AppDomain中被IIS回收该组件是那么只装上的需求。

尝试使用 GetReferencedAssemblies 方法上的 System.Web.Compilation.BuildManager 来得到引用的组件列表来代替。

  VAR组件= BuildManager.GetReferencedAssemblies()演员LT;大会>();

这应该强制引用的程序被加载到应用程序域立即使其可用于模块扫描。

I have a layered web application that I built with ASP.NET MVC 4, WebAPI and some other components. I'm using the latest release of Autofac 2.6.2.859 as my DI container along with the MVC and WebAPI integrations. I have autofac modules setup in my different layers and I'm using the new RegisterAssemblyModules to scan the AppDomain assemblies for the various modules.

On startup every works great. When I edit the web.config file and the application warms up, my registrations are lost. I get a DependencyResolutionException -

None of the constructors found with 'Public binding flags' on type 'My.Class.Name' can be invoked with the available services and parameters:

So my registrations are not being reloaded. Does anyone know how to address this issue? Should I put my initialization code somewhere else other than Application_Start() ?

UPDATE

Here's what my code looks like

 public static class IoC
{
    public static void Configure()
    {
        var builder = new ContainerBuilder();
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();

        //Assembly Modules            
        builder.RegisterAssemblyModules<NSUTriviaModuleBase>(assemblies);

        // Register API controllers using assembly scanning.
        builder.RegisterControllers(assemblies);

        // Register API controllers using assembly scanning.
        builder.RegisterApiControllers(assemblies);

        var container = builder.Build();
        // Set the dependency resolver implementation.
        var resolver = new AutofacWebApiDependencyResolver(container);

        GlobalConfiguration.Configuration.DependencyResolver = resolver;
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }
}

解决方案

I think the problem is that all assemblies are loaded into the AppDomain when the application first starts, but when the AppDomain is recycled by IIS the assemblies are then only loaded on demand.

Try using the GetReferencedAssemblies method on System.Web.Compilation.BuildManager to get a list of the referenced assemblies instead.

var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

That should force the referenced assemblies to be loaded into the AppDomain immediately making them available for module scanning.

这篇关于Autofac失去上的Web.Config编辑注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆