WebActivatorEx.dll (Unity bootstrap)正在另一个项目上触发。

6 浏览
0 Comments

WebActivatorEx.dll (Unity bootstrap)正在另一个项目上触发。

我有3/4个项目,它们都使用自己的Unity,并带有一个bootstrap.cs文件,这些文件来自Unity Mvc5 nuget包。

当我启动项目1时,命名空间为(MvcAdmin),

它触发了项目2 (命名空间为MvcApi)的bootstrap,导致了一个异常!

我已经检查并确认这不是由于iis路由或其他原因引起的。还检查了所有WebActivatorEx使用不同的命名空间等。

下面的这个类是触发的,但不应该:

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MvcApi.App_Start.UnityWebApiActivator), "Start")]
namespace MvcApi.App_Start
{
    /// 提供将Unity与ASP.NET中托管的WebApi集成的引导
    public static class UnityWebApiActivator
    {
        /// 应用程序启动时集成Unity
        public static void Start() 
        {
            // 如果要为每个IHttpController解析使用一个新的子容器,请使用UnityHierarchicalDependencyResolver。
            // 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;
        }
    }
}

为什么会发生这种情况?

这是我的调用堆栈:

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.CreateValue() + 0x1c7 bytes 
    mscorlib.dll!System.Lazy.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(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 methods, System.Func setHostingEnvironmentCultures) + 0x1a5 bytes 
    System.Web.dll!System.Web.Compilation.BuildManager.InvokePreStartInitMethods(System.Collections.Generic.ICollection 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 

0
0 Comments

(WebActivatorEx.dll (Unity bootstrap) is firing on a different project)这个问题的出现原因是WebActivatorEx的启动过程会扫描所有的程序集,查找程序集级别的WebActivatorEx PreApplicationStartMethod和ApplicationShutdownMethod属性,并启动与它们相关联的任何类。这可能意味着一个本应该属于另一个项目的dll/程序集被错误地启动了,只是因为它放错了位置。

解决方法是确保错误的dll/程序集不会出现在项目中。在Web发布过程中,默认情况下是不会包含这些程序集的。但是,通过Azure部署,例如使用git等工具,可能会导致这些程序集被包含进来。因此,需要检查和清理项目目录,确保只有正确的程序集存在。

以下是解决该问题的示例代码:

// 在AssemblyInfo.cs文件中添加以下代码
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourNamespace.WebActivator), "PreStart")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(YourNamespace.WebActivator), "Shutdown")]
namespace YourNamespace
{
    public static class WebActivator
    {
        public static void PreStart()
        {
            // 在此处添加启动时的逻辑
        }
        public static void Shutdown()
        {
            // 在此处添加关闭时的逻辑
        }
    }
}

确保在AssemblyInfo.cs文件中添加了正确的PreApplicationStartMethod和ApplicationShutdownMethod属性,并在相应的WebActivator类中实现启动和关闭时的逻辑。这样就可以确保只有正确的项目会被启动,避免了(WebActivatorEx.dll (Unity bootstrap) is firing on a different project)这个问题的发生。

0