【问题标题】:Unity.Mvc - UnityDependencyResolver issueUnity.Mvc - UnityDependencyResolver 问题
【发布时间】:2012-04-18 09:16:21
【问题描述】:

当前项目是一个 Mvc4 应用程序,我让 Ioc 工作,最近它刚刚停止。

DependencyResolver.SetResolver(New UnityDependencyResolver(RegisterIocServices()))

那条线正常工作,现在我收到以下错误:

ArgumentException 未被用户代码处理

Unity.Mvc3.UnityDependencyResolver 类型似乎没有 实施 Microsoft.Practices.ServiceLocation.IServiceLocator。 参数名称:commonServiceLocator

有人遇到这种情况吗?有什么想法或建议吗?

提前致谢。

【问题讨论】:

  • 很可能,您缺少 Using 某处,或者可能是参考。
  • 我卸载了 Unity.Mvc NuGet 包,然后重新安装它似乎解决了问题。所以你可能是对的。真是奇怪的行为。

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

卸载/重新安装 Unity MVC 包并没有解决问题。我最终不得不在项目中对 Microsoft.EnterpriseLibrary.Common 库进行硬引用,这似乎在一两个星期内清理干净。 然后我开始看到错误间歇性地再次出现,做一个完整的解决方案清理并偶尔构建解决它。它会起作用,然后突然恢复到错误消息。

所以今天早上我终于放弃了 Unity.Mvc,转而使用 Autofac。一旦我为 Autofac 设置了我的引导程序文件,它在第一次编译时就工作了,并且整个早上都很稳定。

这是 Autofac 的 boostrapper 文件,以防有人需要样本。注意:我使用 WebActivator 让我的所有引导类在启动前运行,而不是在 Global.asax.vb 文件中放置一堆代码。

#Region "Imports"

Imports System.Reflection
Imports Autofac
Imports Autofac.Integration.Mvc
Imports MyCompany.Data.Repositories
Imports MyCompany.Services
Imports MyCompany.Web.Mvc.Public.Bootstrap
Imports MyCompany.Web.Mvc.Public.Services

#End Region

#Region "Assembly Meta"

' This tells the app to run the "Start" method prior to running the App_Start method in Global.asax
<Assembly: WebActivator.PreApplicationStartMethod(GetType(AutofacDI), "Initialize")> 

#End Region

Namespace MyCompany.Web.Mvc.Public.Bootstrap

    ''' <summary>
    ''' Class to setup dependency injection and register types/services.
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class AutofacDI

        ''' <summary>
        ''' Method to register the Unity dependency injection component.
        ''' </summary>
        ''' <remarks>
        ''' This line of code below could alternatively be placed in Global.asax App_Start(), doing
        ''' so in this manner ensures that this gets run "PreStart".
        ''' </remarks>
        Public Shared Sub Initialize()

            ' Create Unity dependency container.
            Dim dependencyContainer = BuildIocContainer()

            ' Set DI resolver
            DependencyResolver.SetResolver(New AutofacDependencyResolver(dependencyContainer))

        End Sub

        ''' <summary>
        ''' Registers the IOC types/services.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Shared Function BuildIocContainer() As Autofac.IContainer

            Dim builder = New ContainerBuilder

            With builder
                ' Register Controllers
                .RegisterControllers(Assembly.GetExecutingAssembly())

                ' Custom MyCompany/Mvc objects
                .RegisterType(Of FormsAuthenticationService)().As(Of IFormsAuthenticationService)().InstancePerHttpRequest()
                .RegisterType(Of AccountMembershipService)().As(Of IMembershipService)().InstancePerHttpRequest()

                '***************************************************************
                '*  MyCompany service objects.
                '***************************************************************
                ' This is auto registration, it replaces all the individual registration lines of code below.
                builder.RegisterAssemblyTypes(GetType(CatalogCodeService).Assembly).
                    Where(Function(t) t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerHttpRequest()

                '***************************************************************
                '*  MyCompany repository objects (used by service objects above)
                '***************************************************************
                ' This is auto registration, it replaces all the individual registration lines of code below.
                builder.RegisterAssemblyTypes(GetType(CatalogCodeRepository).Assembly).
                    Where(Function(t) t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerHttpRequest()
            End With

            Return builder.Build()

        End Function

    End Class

End Namespace

【讨论】:

    【解决方案2】:

    使用 MVC 4.0

    我遇到的问题是 IDependencyResolver 有两个版本

     //using System.Web.Http.Dependencies;  //wrong version for me
    
     using System.Web.Mvc; //correct version for me.
    

    使用 System.Web.Mvc 是我的 Web API 项目所需的版本。

    我正在使用 unity,但下面的博客文章帮助我解决了这个问题。

    http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/

    【讨论】:

    • 感谢尼克的提醒。我最终将我的 DI 切换到 AutoFac 而不是 Unity。
    【解决方案3】:

    虽然不是这个特定问题的答案,但我也收到了上述错误消息,这个问题是谷歌搜索该消息时的第一个问题。我遇到了这个问题,因为我愚蠢地继承了System.Web.Mvc.DependencyResolver,而不是仅仅实现接口,例如:

    // wrong
    public class MyDependencyResolver : System.Web.Mvc.DependencyResolver
    { ... }
    
    // correct
    public class MyDependencyResolver : System.Web.Mvc.IDependencyResolver
    { ... }
    

    我花了一段时间才解决这个问题,所以我希望这可以为其他人省去麻烦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多