无法使程序集绑定在 Azure 云服务中工作

本文关键字:服务 工作 Azure 程序集 绑定 | 更新日期: 2023-09-27 18:36:25

我需要以下绑定:

我的设置如下。

一个网站项目。引用网站的云服务 Web 角色。从 webrole 启动方法调用并启动 Owin 自承载服务的类库。

    var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["CompositeServiceEndpoint"];
    string baseUri = string.Format("{0}://{1}",
        endpoint.Protocol, endpoint.IPEndpoint);
    Trace.TraceInformation(String.Format("Starting OWIN at {0}", baseUri));
    _app = WebApp.Start(new StartOptions(url: baseUri), (appbuilder) => new Startup().Configuration(appbuilder, CompositeWebRole.DependencyResolver));

这就是失败的原因,它尝试加载 4.0。

Plugin Initialization 'Composite.WindowsAzure.Management.Plugins.CompositeManagementPlugin': System.IO.FileLoadException: Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

我尝试在网站项目的 app.config 和 web.config 中添加程序集绑定。这不是我应该放置文件的地方吗?

更新

我已经验证了登录到远程桌面,f:/approot/具有内部绑定的 web.config。

完整的错误是:

Plugin Initialization 'Composite.WindowsAzure.Management.Plugins.CompositeManagementPlugin': System.IO.FileLoadException: Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at Composite.WindowsAzure.Management.Startup.Configuration(IAppBuilder app, IWebroleDependencyResolver dpr)
   at Microsoft.Owin.Hosting.Engine.HostingEngine.ResolveApp(StartContext context)
   at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
   at Composite.WindowsAzure.Management.Plugins.CompositeManagementPlugin.InitializePlugin()
   at Composite.WindowsAzure.WebRole.Websites.WebsiteManager.InitializeManager()
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM'Software'Microsoft'Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM'Software'Microsoft'Fusion!EnableLog].
; TraceSource 'WaIISHost.exe' event

当我在模拟器中运行它时,没有问题。

无法使程序集绑定在 Azure 云服务中工作

根据您使用的 SDK,我在使用 Azure SDK 2.3 时遇到了这个问题好几天。

由于现在使用完整 IIS,因此 WebRole 无法看到常规的 Web.config 文件或 app.config 文件。Visual Studio应该这样做,自动将文件放在bin文件夹中,但由于某些原因,这对我不起作用。如果是这种情况,请执行以下操作:

创建一个新的 .config 文件,将其命名

"yourprojectname".dll.config

放入项目的根目录,并为所需的所有引用插入程序集绑定代码,例如:

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
  <runtime>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
         <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
       </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

然后部署项目,角色将看到新的配置文件。希望这对某人有所帮助。

我找不到解决真正问题的方法。

我最终从Unity WebAPI boostrapper中获取了代码,因为这是对webapi的一些4.0.0.0引用的依赖项。踢出它,只是在我自己的dll中实现,而不是使用Unity Web api引导器解决了我的问题。

// Generated by .NET Reflector from C:'dev'c1'Source'WindowsAzure'packages'Unity.AspNet.WebApi.3.0.1304.0'lib'Net45'Microsoft.Practices.Unity.WebApi.dll
namespace Microsoft.Practices.Unity.WebApi
{
    using Microsoft.Practices.Unity;
    using System;
    using System.Collections.Generic;
    using System.Web.Http.Dependencies;
    public sealed class UnityDependencyResolver : IDependencyResolver, IDependencyScope, IDisposable
    {
        private IUnityContainer container;
        private SharedDependencyScope sharedScope;
        public UnityDependencyResolver(IUnityContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            this.container = container;
            this.sharedScope = new SharedDependencyScope(container);
        }
        public IDependencyScope BeginScope()
        {
            return this.sharedScope;
        }
        public void Dispose()
        {
            this.container.Dispose();
            this.sharedScope.Dispose();
        }
        public object GetService(Type serviceType)
        {
            try
            {
                return this.container.Resolve(serviceType, new ResolverOverride[0]);
            }
            catch (ResolutionFailedException)
            {
                return null;
            }
        }
        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return this.container.ResolveAll(serviceType, new ResolverOverride[0]);
            }
            catch (ResolutionFailedException)
            {
                return null;
            }
        }
        private sealed class SharedDependencyScope : IDependencyScope, IDisposable
        {
            private IUnityContainer container;
            public SharedDependencyScope(IUnityContainer container)
            {
                this.container = container;
            }
            public void Dispose()
            {
            }
            public object GetService(Type serviceType)
            {
                return this.container.Resolve(serviceType, new ResolverOverride[0]);
            }
            public IEnumerable<object> GetServices(Type serviceType)
            {
                return this.container.ResolveAll(serviceType, new ResolverOverride[0]);
            }
        }
    }
}