问题
I have 3/4 projects all using their own Unity, with a bootstrap.cs file, these are from the Unity Mvc5 nuget package.
When I start project 1, namespace (MvcAdmin) it is firing the bootstrap for project 2 (namespace MvcApi), which is causing an exception!
I've checked and confirmed it's not caused by iis routes or anything. Also checked, all WebActivatorEx's are using different namespaces etc.
This class below is what is firing, but it shouldn't be:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MvcApi.App_Start.UnityWebApiActivator), "Start")]
namespace MvcApi.App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with WebApi when it is hosted in ASP.NET</summary>
public static class UnityWebApiActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
// Use UnityHierarchicalDependencyResolver if you want to use a new child container for each IHttpController resolution.
// var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());
var container = UnityConfig.GetConfiguredContainer();
DepResolver.SetResolver(new DepResolver(container));
DepResolver.ExceptionHelper().OnAppInit();
var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
}
}
Why could this be happening?
Here's my callstack:
MvcShared.dll!MvcShared.Helpers.AppSettingsHelper.throwExceptionIfCountIncorrect(int count) Line 129 C#
MvcShared.dll!MvcShared.Helpers.AppSettingsHelper.GetAppSettingsFromDb() Line 109 + 0x3c bytes C#
MvcShared.dll!MvcShared.Helpers.AppSettingsHelper.ReadXmlAndStoreSettings() Line 48 + 0xa bytes C#
> MvcApi.dll!MvcApi.App_Start.UnityConfig.RegisterTypes(Microsoft.Practices.Unity.IUnityContainer container) + 0x774 bytes
MvcApi.dll!MvcApi.App_Start.UnityConfig..cctor.AnonymousMethod__0() + 0x65 bytes
mscorlib.dll!System.Lazy<Microsoft.Practices.Unity.IUnityContainer>.CreateValue() + 0x1c7 bytes
mscorlib.dll!System.Lazy<Microsoft.Practices.Unity.IUnityContainer>.LazyInitValue() + 0x1dc bytes
MvcApi.dll!MvcApi.App_Start.UnityConfig.GetConfiguredContainer() + 0x47 bytes
MvcApi.dll!MvcApi.App_Start.UnityWebApiActivator.Start() + 0x28 bytes
[Native to Managed Transition]
WebActivatorEx.dll!WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() + 0x155 bytes
WebActivatorEx.dll!WebActivatorEx.ActivationManager.RunActivationMethods<WebActivatorEx.PreApplicationStartMethodAttribute>(bool designerMode) + 0x356 bytes
WebActivatorEx.dll!WebActivatorEx.ActivationManager.RunPreStartMethods(bool designerMode) + 0x2f bytes
WebActivatorEx.dll!WebActivatorEx.ActivationManager.Run() + 0xbd bytes
[Native to Managed Transition]
System.Web.dll!System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(System.Collections.Generic.ICollection<System.Reflection.MethodInfo> methods, System.Func<System.IDisposable> setHostingEnvironmentCultures) + 0x1a5 bytes
System.Web.dll!System.Web.Compilation.BuildManager.InvokePreStartInitMethods(System.Collections.Generic.ICollection<System.Reflection.MethodInfo> methods) + 0x67 bytes
System.Web.dll!System.Web.Compilation.BuildManager.CallPreStartInitMethods(string preStartInitListPath) + 0xf0 bytes
System.Web.dll!System.Web.Compilation.BuildManager.ExecutePreAppStart() + 0x98 bytes
System.Web.dll!System.Web.Hosting.HostingEnvironment.Initialize(System.Web.Hosting.ApplicationManager appManager, System.Web.Hosting.IApplicationHost appHost, System.Web.Configuration.IConfigMapPathFactory configMapPathFactory, System.Web.Hosting.HostingEnvironmentParameters hostingParameters, System.Security.Policy.PolicyLevel policyLevel, System.Exception appDomainCreationException) + 0x47f bytes
System.Web.dll!System.Web.Hosting.HostingEnvironment.Initialize(System.Web.Hosting.ApplicationManager appManager, System.Web.Hosting.IApplicationHost appHost, System.Web.Configuration.IConfigMapPathFactory configMapPathFactory, System.Web.Hosting.HostingEnvironmentParameters hostingParameters, System.Security.Policy.PolicyLevel policyLevel) + 0x26 bytes
[Appdomain Transition]
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) + 0x147d bytes
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironmentAndReportErrors(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) + 0x35 bytes
System.Web.dll!System.Web.Hosting.ApplicationManager.GetAppDomainWithHostingEnvironment(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) + 0x92 bytes
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateObjectInternal(string appId, System.Type type, System.Web.Hosting.IApplicationHost appHost, bool failIfExists, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) + 0x6f bytes
System.Web.dll!System.Web.Hosting.ProcessHost.StartApplication(string appId, string appPath, out object runtimeInterface) + 0x1fb bytes
回答1:
So I think ther dll/assemblies from the various projects must be in that sites folder. Part of WebActivatorEx's start-up is to scan all assemblies for the assembly-level WebActivatorEx PreApplicationStartMethod and ApplicationShutdownMethod attributes and fire up any classes they reference.
This could well mean a dll/assembly intended for another project gets started up simply because it's in the wrong place.
Ideally those assemblies just wouldn't be there - they aren't by default in a web publish process - but Azure deployment via git etc. could cause them to be.
来源:https://stackoverflow.com/questions/21078380/webactivatorex-dll-unity-bootstrap-is-firing-on-a-different-project