ServiceLocator.Current.GetInstance导致呼叫ObjectBuilder2.PolicyList.GetNoDefault数量过多 [英] ServiceLocator.Current.GetInstance causes excessive number of calls to ObjectBuilder2.PolicyList.GetNoDefault

查看:415
本文介绍了ServiceLocator.Current.GetInstance导致呼叫ObjectBuilder2.PolicyList.GetNoDefault数量过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我MVC4,使用Unity IoC容器.NET4.5的Web应用程序,在方法IoCContainerFactory.GetControllerInstance(),我们使用ServiceLocator.Current.GetInstance得到控制器实例:

In my MVC4, .NET4.5 web app using Unity IoC container, in the method IoCContainerFactory.GetControllerInstance() we use ServiceLocator.Current.GetInstance to get the controller instance:

public class IoCControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(
         System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        // snip other code
        // this is the problem line:
        return ServiceLocator.Current.GetInstance(controllerType) as IController;
    }
}

但是,这一直表现得太慢。使用JetBrains公司DotNetTrace产品我发现ServiceLocator.Current.GetInstance似乎调用ObjectBuilder2.PolicyList.GetNoDefault超过一百万次。我怎样才能弄清楚,为什么它正在那么多电话,我能做些什么来解决这个问题?附截图显示来自dotnetTrace输出:

But this has been performing too slowly. Using JetBrains DotNetTrace product I've found that ServiceLocator.Current.GetInstance appears to be calling ObjectBuilder2.PolicyList.GetNoDefault more than one million times. How can I figure out why it is making so many calls, and what can I do to fix this problem? Attached screenshot shows the output from dotnetTrace:

和什么样的政策是试图在方法获得 PolicyList.GetNoDefault ?我我知道它正在试图找到我也许可以改变这种政策,它确实需要检查很多次什么样的政策。

And what policy is it trying to get in the method PolicyList.GetNoDefault? I I knew what policy it was trying to find I could perhaps change that policy so that it does need to be checked so many times.

推荐答案

这个问题一直没有解决,我们结束了赞成LightInject,这是一个显著更快IoC实现的下探团结,因为在这里看到:

This problem was never solved and we ended up dropping Unity in favor of LightInject, which is a significantly faster IoC implementation, as seen here:

IoC容器基准 - 性能比较

我们也有类似的改进,当我们实现LightInject。

We had similar improvements when we implemented LightInject.

这篇关于ServiceLocator.Current.GetInstance导致呼叫ObjectBuilder2.PolicyList.GetNoDefault数量过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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