Asp.Net MVC Web.config 使用 Unity

笔记用


1.建立项目

2.设定项目

3.按下OK后,会出现下图

4.开启【Package Manage Console】

5.开启后,出现下图

6.安装套件

Install-Package Unity -Version 4.0.1
Install-Package Unity.Interception -Version 4.0.1
Install-Package Unity.Mvc -Version 4.0.1

7.新增UnityConfig.vb、UnityMvcActivator.vb 2个文件于 \App_Start

UnityConfig.vb

Imports System
Imports Microsoft.Practices.Unity
Imports Microsoft.Practices.Unity.Configuration
Imports Microsoft.Practices.Unity.InterceptionExtension
Imports Microsoft.Practices.ServiceLocation
Imports System.Reflection

''' 
''' Specifies the Unity configuration for the main container.
''' 
Public Class UnityConfig
#Region "Unity Container"
    Private Shared ReadOnly _Container As Lazy(Of IUnityContainer) = New Lazy(Of IUnityContainer)(
        Function()
            Dim container = New UnityContainer()

            'Unityの设定を読み込む
            container.LoadConfiguration(containerName)

            'ServiceLoacatorにUnityを登录する
            Dim service As New UnityServiceLocator(container)
            ServiceLocator.SetLocatorProvider(Function() service)

            RegisterTypes(container)
            Return container
        End Function)

    ''' 
    ''' Gets the configured Unity container.
    ''' 
    Public Shared Function GetConfiguredContainer() As IUnityContainer
        Return _Container.Value
    End Function
    
#End Region

    ''' Registers the type mappings with the Unity container.
    ''' The unity container to configure.
    ''' There is no need to register concrete types such as controllers or API controllers (unless you want to 
    ''' change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.
    Public Shared Sub RegisterTypes(container As IUnityContainer)

    End Sub


End Class

UnityMvcActivator.vb

Imports System.Linq
Imports System.Web.Mvc
Imports Microsoft.Practices.Unity.Mvc
Imports Microsoft.Web.Infrastructure.DynamicModuleHelper

 
 

''' Provides the bootstrapping for integrating Unity with ASP.NET MVC.
Public NotInheritable Class UnityMvcActivator

    ''' 
    ''' This is static class.
    '''     
    Private Sub New()
    End Sub


    ''' 
    ''' Integrates Unity when the application starts.
    ''' 
    Public Shared Sub Start()
        Dim container = UnityConfig.GetConfiguredContainer()

        FilterProviders.Providers.Remove(FilterProviders.Providers.OfType(Of FilterAttributeFilterProvider)().First())
        FilterProviders.Providers.Add(New UnityFilterAttributeFilterProvider(container))

        DependencyResolver.SetResolver(New UnityDependencyResolver(container))

        DynamicModuleUtility.RegisterModule(GetType(UnityPerRequestHttpModule))
    End Sub

    ''' 
    ''' Disposes the Unity container when the application is shut down.
    ''' 
    Public Shared Sub Shutdown()
        Dim container = UnityConfig.GetConfiguredContainer()
        container.Dispose()
    End Sub

End Class

8.添加 System.Runtime.Version=4.0.0.0 Culture=neutral

9.Web.config档中添加

10.添加Unity.Local.config

Unity.Local.config



  
  

  
  
  

  
    
    
      
      
      
        
          
            
            
          
        
      
    
  

10 测试

HomeController.vb中添加

Dim lt As IList(Of String) = ServiceLocator.Current.GetInstance(Of IList(Of String))("CreateTypeListA")

11.出现下图即完成