尝试获取类型实例时发生激活错误

发布于 2025-01-07 20:48:44 字数 1115 浏览 0 评论 0原文

我将 Unity 与 Asp.net MVC 3 应用程序一起使用。这是在 Application_Start 中运行的一些代码...

        UnityContainer container = new UnityContainer();

        new UnityMappings(container).RegisterTypes();

        DependencyResolver.SetResolver(new UnityServiceLocator(container));

控制器像这样注册到 UnityMappings 实例中...

IEnumerable<Type> controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                            where typeof (IController).IsAssignableFrom(t)
                                            select t;

    foreach (Type t in controllerTypes)
    {
        container.RegisterType(t);
    }

当我请求页面时,我收到以下错误...(最后一个错误特定于所请求的视图。)

尝试获取类型实例时发生激活错误 IControllerFactory,键“”

尝试获取类型实例时发生激活错误 IControllerActivator,键“”

尝试获取类型实例时发生激活错误 IViewPageActivator,键“”

尝试获取类型实例时发生激活错误 ModelMetadataProvider,键“”

然后奇怪的是,我可以单击所有异常,并且页面工作得绝对正常!所有其他依赖项都得到了很好的解决。

这不是 Visual Studio 问题,因为它是在不同计算机的不同实例中执行的。我必须关闭所有异常的中断,这样我才能完成任何事情。

有什么想法吗?

I'm using Unity with an Asp.net MVC 3 app. Here is some code running in Application_Start...

        UnityContainer container = new UnityContainer();

        new UnityMappings(container).RegisterTypes();

        DependencyResolver.SetResolver(new UnityServiceLocator(container));

The controllers are registered with the UnityMappings instance like so...

IEnumerable<Type> controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                            where typeof (IController).IsAssignableFrom(t)
                                            select t;

    foreach (Type t in controllerTypes)
    {
        container.RegisterType(t);
    }

When I request a page I get the following errors... (The last one is specific to the view being requested.)

Activation error occured while trying to get instance of type
IControllerFactory, key ""

Activation error occured while trying to get instance of type
IControllerActivator, key ""

Activation error occured while trying to get instance of type
IViewPageActivator, key ""

Activation error occured while trying to get instance of type
ModelMetadataProvider, key ""

And then strangely, I can click through all the exceptions and the page works absolutely fine! All the other dependencies are resolved just fine.

Its not a Visual Studio issue because it does it in different instances from different machines. I've had to turn off breaking on all exceptions so that I can get anything done.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥︴琐丶欲为 2025-01-14 20:48:44

当我使用这个时,这个错误似乎已经消失了...

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

...其中 UnityDependencyResolver 定义为...

public class UnityDependencyResolver : IDependencyResolver
{
    private readonly IUnityContainer container;

    public UnityDependencyResolver(IUnityContainer container)
    {
        this.container = container;
    }

    #region IDependencyResolver Members

    public object GetService(Type serviceType)
    {
        try
        {
            return container.Resolve(serviceType);
        }
        catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return container.ResolveAll(serviceType);
        }
        catch
        {
            return new List<object>();
        }
    }

    #endregion
}

在我使用在“Microsoft.Practices.Unity”中定义的“UnityServiceLocator”之前,如下...

DependencyResolver.SetResolver(new UnityServiceLocator(container));

是“ UnityServiceLocator' 不应该以这种方式使用?

This error seems to have gone away when I use this...

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

... where UnityDependencyResolver is defined as...

public class UnityDependencyResolver : IDependencyResolver
{
    private readonly IUnityContainer container;

    public UnityDependencyResolver(IUnityContainer container)
    {
        this.container = container;
    }

    #region IDependencyResolver Members

    public object GetService(Type serviceType)
    {
        try
        {
            return container.Resolve(serviceType);
        }
        catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return container.ResolveAll(serviceType);
        }
        catch
        {
            return new List<object>();
        }
    }

    #endregion
}

Before I was using 'UnityServiceLocator' which is defined in 'Microsoft.Practices.Unity' like this...

DependencyResolver.SetResolver(new UnityServiceLocator(container));

Is 'UnityServiceLocator' not supposed to be used in this way?

独留℉清风醉 2025-01-14 20:48:44

我在使用 Unity 和使用编译视图的 MVC3 站点时遇到了类似的注入问题。

事实证明,在 Application_Start 中注册事物意味着它们在应用程序启动过程中发生得太晚了。

我按照解释使用了 PreApplicationStartMethod 属性 此处 运行在 Application_Start 事件之前运行的单独静态方法中注册类型的所有代码。

我没有得到完全相同的错误,但它是在同一区域。测试不需要花费太多精力,因此值得一试。

编辑(因为评论字段太小)

好的。根据我的经验,因为假设而排除调查途径通常会导致我浪费大量时间。当然,您应该始终首先尝试最简单或最明显的解释,但您似乎已经这样做了。

代码本身可能没问题,只是由于应用程序中存在其他一些细微的差异,注入请求的调用时间可能比您从其他体验中预期的要早。

就我而言,相同的代码在另一个应用程序中运行得很好,但在这个应用程序中却失败了,因为我使用的是编译视图……我当时认为这是完全不相关的。

它可能是许多事情之一。您列出的所有接口都与 System.Web.MVC 命名空间相关,因此查找(正确的)程序集可能存在问题。如果您断言这不是代码,那么它仅出现在该应用程序中但出现在多台计算机上这一事实表明,这可能是其中一个 web.config 文件中的问题,可能与程序集引用有关。抛出的异常将提供更多信息,特别是 InnerException 应该提供有关实际导致激活错误的更多详细信息。如果失败,您可以使用 Fusion 日志查看器深入了解程序集加载。

如果不了解有关应用程序或错误本身的更多信息,可能很难提供更简洁的建议。

I had similar injection problems using Unity with an MVC3 site that utilizes compiled views.

It turned out registering things in Application_Start meant they happened too late in the application start-up process.

I used the PreApplicationStartMethod attribute as explained here to run all the code for registering types in a separate static method that runs before the Application_Start event.

I wasn't getting exactly the same errors, but it was in the same area. It shouldn't take too much effort to test, so it might be worth a try.

EDIT (because the comment field is too small)

OK. In my experience the excluding an avenue of investigation because of an assumption usually leads to me wasting a lot of time. Granted you should always try the simplest or most obvious explanations first, however you seem to have already done that.

The code itself may be fine, it may just be that the injection request is being called on earlier then you would expect from your other experience because of some other subtle difference in your application.

In my case, the same code had worked just fine in another application but was failing in this one because I was using a compiled view... something I thought at the time was completely irrelevant.

It could one of many things. All the interfaces you listed are related to the System.Web.MVC namespace, so it may be an issue with finding the (correct) assembly. If as you assert it’s not the code, the fact that it occurs in this app only but on multiple machines suggests it may be an issue in one of the web.config files, possibly to do with assembly references. The exception thrown would provide more info, in particular the InnerException should give more detail on what actually caused the activation error. Failing that you could use the Fusion log viewer to drill into assembly loading.

Without knowing more about the application or error itself, it’s probably going to be difficult to offer a more concise suggestion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文