【问题标题】:MVC3 & Unity.MvcMVC3 & Unity.Mvc
【发布时间】:2012-04-05 23:39:53
【问题描述】:

我的 MVC3 项目遇到了奇怪的问题。我遵循了一个使用 Unity.Mvc 的简单 IOC 示例。

如果我的对象及其接口位于 Web 项目本身(在本例中为 IMessageService 和 MessageService),则它工作正常。他们显然在工作,没有问题。

但是当我尝试从外部程序集注册我的业务服务对象时,它们永远不会设置为任何内容(始终为空)。 App-Start注册时没有错误等。

有人有什么想法吗?在这里绝望....

更新:

#Region "Imports"

Imports MyProject.Services
Imports MyProject.Services.Interfaces
Imports MyProject.Web.Mvc.Bootstrap
Imports MyProject.Web.Mvc.Services
Imports Microsoft.Practices.Unity
Imports Unity.Mvc3

#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(UnityDI), "Start")> 

#End Region

Namespace MyProject.Web.Mvc.Bootstrap

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

        ''' <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 Start()

            ' Set DI resolver
            ' NOTE: ECD - The UnityDependencyResolver below is part of the Unity.Mvc3 assembly
            DependencyResolver.SetResolver(New UnityDependencyResolver(RegisterIocServices()))

        End Sub

        ''' <summary>
        ''' Registers the IOC types/services.   
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Shared Function RegisterIocServices() As IUnityContainer

            ' Create Unity dependency container
            Dim dependencyContainer As IUnityContainer = New UnityContainer

            ' Register the relevant types/services for the container here through classes or configuration
            With dependencyContainer
                .RegisterType(Of IFormsAuthenticationService, FormsAuthenticationService)()
                .RegisterType(Of IContactService, ContactService)()
                .RegisterType(Of IProductItemService, ProductItemService)()
                .RegisterType(Of ICustomerProductItemService, CustomerProductItemService)()
                .RegisterType(Of ISystemTableItemService, SystemTableItemService)()
                .RegisterType(Of ICatalogCodeService, CatalogCodeService)()
                .RegisterType(Of IOrderService, OrderService)()

                ' TEST: This one is in the MVC project and works, the above are an external library
                .RegisterType(Of IMessageService, MessageService)()
            End With

            Return dependencyContainer

        End Function

    End Class

End Namespace

上面的代码是我的注册过程。我也尝试过将它直接放在 global.asax 中并具有相同的行为。

更新 2

找出我的问题。

在我的控制器中,我有以下属性:

<Dependency()>
Private Property CatalogCodeService As ICatalogCodeService
<Dependency()>
Private Property ContactService As IContactService
<Dependency()>
Private Property CustomerProductItemService As ICustomerProductItemService

但是在操作方法中访问服务总是会抛出一个错误,即没有任何这些对象的实例。所以我认为这是我发布的原始代码或我注册时的内容。

原来不是我的注册码,这很好。

问题?

属性必须是“公共的”!! Private、Protected、Friend 都不适用于 IOC。一旦我将这些属性更改为“公共”,一切都开始正常工作。

我还没有验证这在 C# 中是否相同,所以如果有人可以添加他们的两分钱,请务必这样做。

去看看……

【问题讨论】:

  • 您能展示您的配置/注册示例吗?
  • 这可能取决于您注册的方式/地点(例如在代码中或通过配置文件)。请按照 Steven 的建议提供更多信息。
  • 见上面的更新2,与注册无关。它实际上要简单得多。

标签: asp.net-mvc dependency-injection


【解决方案1】:

更新二:

上述实现使用公共属性,遵循依赖注入的“属性注入”模式。

我已经切换到更合适的“构造函数注入”方法。

通过此更改,我的控制器使用的我的服务的属性可以是私有和只读的。

Mark Seemann 的“.NET 中的依赖注入”,好书。我强烈建议任何实施依赖注入或只是想了解更多信息的人使用它。

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多