如何使用unity将参数注入构造函数

时间:2015-01-30 10:25:16

标签: vb.net unity-container asp.net-web-api

您好我在WebAPI 1.0中使用unity并在Global.asax.vb文件下注册,如下所示

Dim container As IUnityContainer = New UnityContainer()

container.RegisterType(Of Car)(New InjectionConstructor(GetType(Integer)))

现在我如何使用

将客户端应用程序传递的整数值传递给汽车类型
Public Sub New(ByVal intUserId As Int32)
    objCar = DependencyResolver.Current.GetService(Of car)()     

End Sub

在DependencyResolver中找不到任何可以使用的内容(ParameterOverride(" intUserId",intUserId)

1 个答案:

答案 0 :(得分:1)

与使用ASP.NET MVC注册Unity容器非常相似。虽然web api有不同的执行管道线。请按照以下步骤操作。

注意:我已使用转换器将此代码从C#转换为VB。所以我希望它在语法上是正确的。虽然它在c#中是准确的。

1)实现 IDependencyResolver (确保在此处解析正确的命名空间。 IDependencyResolver 应来自 System.Web.Http.Dependencies 。记下导入的命名空间。

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports Microsoft.Practices.Unity
Imports System.Web.Http.Dependencies

Namespace YourNamespace.Framework
    Public Class UnityApiDependencyResolver
        Implements IDependencyResolver

        Private _container As IUnityContainer = Nothing

        Public Sub New(container As IUnityContainer)
            Me._container = container
        End Sub

        Public Function GetService(serviceType As Type) As Object
            Try
                Return _container.Resolve(serviceType)
            Catch generatedExceptionName As Exception

                Return Nothing
            End Try
        End Function

        Public Function GetServices(serviceType As Type) As IEnumerable(Of Object)
            Try
                Return _container.ResolveAll(serviceType)
            Catch generatedExceptionName As Exception

                Return Nothing
            End Try
        End Function

        Public Function BeginScope() As IDependencyScope
            Return Me
        End Function

        Public Sub Dispose()
            '
        End Sub
    End Class
End Namespace

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

2)通过配置或代码配置容器。

3)在Global.asax.vb文件中注册您的容器

Dim container = New UnityContainer()
Dim section As UnityConfigurationSection = TryCast(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)

section.Configure(container, "UnitySection")

'api dependency resolver
GlobalConfiguration.Configuration.DependencyResolver = New UnityApiDependencyResolver(container)

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

多数民众赞成。 现在,您可以在任何API控制器中声明您的依赖关系,它将由Unity注入

Public Sub New(repositoryFactory As IRepositoryFactory, serviceFactory As IServiceFactory)
    Me.repositoryFactory = repositoryFactory
    Me.serviceFactory = serviceFactory
End Sub

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

您的类型注册也是错误的。您必须将接口或抽象类指定为依赖项,并将其具体实现指定为映射。

e.g container.RegisterType(Of IContext,Context)()

我不明白通过将整数值映射到car来实现什么。您是否希望基于参数中的整数值加载汽车对象?