卸载/重新安装 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