unitycontainer / aspnet-webapi

Unity.AspNet.WebApi package
Apache License 2.0
9 stars 11 forks source link

未能加载文件或程序集“Unity.AspNet.WebApi, Version=5.0.1.9, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”或它的某一个依赖项 #1

Closed ghd258 closed 6 years ago

ghd258 commented 6 years ago

未能加载文件或程序集“Unity.AspNet.WebApi, Version=5.0.1.9, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040) [FileLoadException: 未能加载文件或程序集“Unity.AspNet.WebApi, Version=5.0.1.9, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)] VOCs.WebAPI.App_Start.UnityWebApiActivator.Start() in H:\VOCs\03_Code\00_SourceCode\VOCsV2\06_Web\WebAPI\App_Start\UnityWebApiActivator.cs:20

[TargetInvocationException: 调用的目标发生了异常。] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +114 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146 WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() +81 WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +699 WebActivatorEx.ActivationManager.Run() +123

[InvalidOperationException: 针对类型 WebActivatorEx.ActivationManager 的应用程序预启动初始化方法 Run 引发了异常,显示下列错误消息: 调用的目标发生了异常。。] System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection1 methods, Func1 setHostingEnvironmentCultures) +902 System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +169 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +173 System.Web.Compilation.BuildManager.ExecutePreAppStart() +172 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +912

[HttpException (0x80004005): 针对类型 WebActivatorEx.ActivationManager 的应用程序预启动初始化方法 Run 引发了异常,显示下列错误消息: 调用的目标发生了异常。。] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +534 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +718

ENikS commented 6 years ago

Would you be so kind and give an example of how you use the API? Something I can run, like this:

            using (var container = new UnityContainer())
            {
                container.RegisterInstance<IFoo>("instance1", new Foo { TestProperty = "value1" });
                container.RegisterInstance<IFoo>("instance2", new Foo { TestProperty = "value2" });
                using (var resolver = new UnityDependencyResolver(container))
                {
                    var actual = resolver.GetServices(typeof(IFoo)).Cast<IFoo>().ToList();
                    Assert.IsTrue(actual.Any(x => x.TestProperty == "value1"));
                    Assert.IsTrue(actual.Any(x => x.TestProperty == "value2"));
                }
            }
ghd258 commented 6 years ago

public static class UnityWebApiActivator { ///

Integrates Unity when the application starts. 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 resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());

        GlobalConfiguration.Configuration.DependencyResolver = resolver;
    }

    /// <summary>Disposes the Unity container when the application is shut down.</summary>
    public static void Shutdown()
    {
        var container = UnityConfig.GetConfiguredContainer();
        container.Dispose();
    }
}
ghd258 commented 6 years ago

using System.Web.Http; using Unity.AspNet.WebApi;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(VOCs.WebAPI.App_Start.UnityWebApiActivator), "Start")] [assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(VOCs.WebAPI.App_Start.UnityWebApiActivator), "Shutdown")]

namespace VOCs.WebAPI.App_Start { ///

Provides the bootstrapping for integrating Unity with WebApi when it is hosted in ASP.NET public static class UnityWebApiActivator { /// Integrates Unity when the application starts. 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 resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());

        GlobalConfiguration.Configuration.DependencyResolver = resolver;
    }

    /// <summary>Disposes the Unity container when the application is shut down.</summary>
    public static void Shutdown()
    {
        var container = UnityConfig.GetConfiguredContainer();
        container.Dispose();
    }
}

}

axelgenus commented 6 years ago

PR #2 should fix this issue.

ENikS commented 6 years ago

Could you verify if it works and if it does, I'll deploy new package to NuGet later today

ENikS commented 6 years ago

Published v5.0.2