0

我需要以下绑定:

我的设置如下。

一个网站项目。引用该网站的云服务 Webrole。从 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

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

4

2 回答 2

2

根据您使用的 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>

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

于 2014-06-03T07:28:31.270 回答
0

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

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

// 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]);
            }
        }
    }
}
于 2013-10-21T16:56:15.707 回答