2

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

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

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

用户代码未处理 ArgumentException

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

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

提前致谢。

4

3 回答 3

3

使用 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 项目所需的版本。

我正在使用统一,但下面的博客文章帮助我直截了当地解决了这个问题。

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

于 2012-08-21T19:58:35.210 回答
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
于 2012-05-25T14:59:56.963 回答
0

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

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

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

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

于 2013-02-21T03:39:06.503 回答