Cloud Service Recycle with error System.IO.FileLoadException:无法加载文件或程序集'System.Web.Http,Version = 4.0.0.0

时间:2017-06-16 14:41:45

标签: .net azure azure-cloud-services

我有一个针对4.6.1框架的web api项目,并将其部署为Azure上的云服务。在部署时获得以下错误。

Web.Config详细信息:

  <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>

来自事件查看器的错误:

无法创建角色入口点:System.TypeLoadException:由于以下异常,无法加载角色入口点: - System.IO.FileLoadException:无法加载文件或程序集'System.Web.Http,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。 (HRESULT异常:0x80131040) 文件名:'System.Web.Http,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'

我的解决方案中没有System.Web.Http,Version = 4.0.0.0的引用。请让我知道我做错了什么。

1 个答案:

答案 0 :(得分:2)

我们可以从blog得到答案。它阐述了根本原因和解决方案。以下是博客的摘录。

<强>解决方案:

  

1)打开位于项目bin文件夹中的.dll.config。

     

2)检查是否存在您需要的BindingRedirect条目。如果没有,请按照以下两个选项之一:        a)复制web.config或app.config内容(考虑这两个配置文件中的一个具有您需要的信息)并将其粘贴到.dll.config文件中。        b)手动创建程序集绑定条目:

     

3)将.dll.config文件添加到您的解决方案(与web.config或app.config相同的级别),并将Copy to Output Directory属性设置为“始终复制”。

根本原因

  

通常,当将新程序集添加到项目中时,Visual Studio将自动在web.config(Web角色)或app.config(Worker Role)中创建bindingRedirect条目,以避免错误的程序集版本问题。 / p>      

但是,在Azure云服务中,由于WaIISHost(Web角色)和WaWorkerHost(工作者角色)无法读取这两个配置,因此来自web.config和app.config的程序集绑定无效。相反,他们读取.dll.config文件,这是汇编绑定配置需要的文件