Unity.Mvc - UnityDependencyResolver问题

时间:2012-04-04 16:02:03

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

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

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

该行正在运行,现在我收到以下错误:

  

用户代码未处理ArgumentException

     

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

有人遇到这个吗?有什么想法或建议吗?

提前致谢。

3 个答案:

答案 0 :(得分: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/

答案 1 :(得分:1)

卸载/重新安装Unity MVC软件包无法解决问题。我最终不得不在项目中向Microsoft.EnterpriseLibrary.Common库中添加一个硬引用,这似乎将它清理了一两个星期。 然后我开始看到错误再次出现,完整的解决方案干净,并在一段时间内解决它。它会工作,然后突然恢复到错误信息。

所以我今天早上终于放弃了Unity.Mvc,而是实施了Autofac。一旦我为Autofac设置了bootstrapper文件,它就可以在第一次编译时工作,并且整个上午一直稳定。

以下是Autofac的boostrapper文件,以防任何人需要样本。注意:我使用WebActivator使所有的bootsrapping类在启动前运行,而不是在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 :(得分:0)

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

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

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

我花了一段时间来解决这个问题,所以我希望这可以帮助别人解决问题。