IISRESET后统一注册失败 [英] Unity registration fails after iisreset

查看:265
本文介绍了IISRESET后统一注册失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注册依赖的负载像这样。

I am registering a load of dependencies like so.

container.RegisterTypes(AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default,
overwriteExistingMappings: true);

这事登记罚款和Web API端点配置正确。
如果我做 IISRESET 或只是等待一下,然后事情失败,

This registers things fine and the web api endpoints are properly configured. If I do iisreset or simply wait for a bit then things fail with

该错误信息是不是非常有帮助的。

The error message is not terribly helpful

"exceptionMessage": "An error occurred when trying to create a controller of type 'ApiController'. Make sure that the controller has a parameterless public constructor.",

这当然控制器不和不应该有。

Which of course the controller does not, and should not, have.

我不知道是什么要去......但如果​​我明确地注册依赖

I am not sure what is going on... but if I register the dependencies explicitly

 container.RegisterType<IDoAThing, Domain.Things.DoAThing>(new HierarchicalLifetimeManager());

然后,它所有的作品。

Then it all works.

我怎样才能获得按照惯例报名继续工作?

How can I get the 'by convention' registration to keep working?

推荐答案

看起来像在 IISRESET ,大会尚未通过时加载到应用程序域您的统一登记踢

Looks like after the iisreset, the assemblies have not been loaded yet into the app domain by the time your Unity registration kicks in.

由于您使用的辅助方法 AllClasses.FromLoadedAssemblies(),也不会在尚未加载的程序集注册类。

Since you are using the helper method AllClasses.FromLoadedAssemblies(), it will not register classes in assemblies that were not yet loaded.

尝试获得引用的程序集与<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.getreferencedassemblies.aspx\"相对=nofollow> BuildManager.GetReferencedAssemblies ,这将获得在bin文件夹组件列表。然后执行使用这些组件和 AllClasses.FromAssemblies 辅助方法,你的统一注册。

Try getting the referenced assemblies with BuildManager.GetReferencedAssemblies, which will obtain a list of assemblies in the bin folder. Then perform your Unity registrations using those assemblies and the AllClasses.FromAssemblies helper method.

.RegisterTypes(
    AllClasses.FromAssemblies(
             BuildManager.GetReferencedAssemblies().Cast<Assembly>()),
    WithMappings.FromMatchingInterface,
    WithName.Default,
    overwriteExistingMappings: true);   

在最坏的情况下,你可以使用一个众所周知的类从您的每一个组件,通过反射得到它的组装和这个简化的示例中使用它为您的注册为:

In the worst case you could use a well-known class from each of your assemblies, getting its assembly via reflection and use it for your registration as in this simplified sample:

.RegisterTypes(
    AllClasses.FromAssemblies(typeof(Foo).Assembly),
    WithMappings.FromMatchingInterface,
    WithName.Default,
    overwriteExistingMappings: true);

这篇关于IISRESET后统一注册失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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