【问题标题】:Autofac error: Could not load file or assembly 'System.Web.Http, Version=5.2.0.0,...' My project is Owin WebApi2 SelfHostAutofac 错误:无法加载文件或程序集 'System.Web.Http, Version=5.2.0.0,...' 我的项目是 Owin WebApi2 SelfHost
【发布时间】:2015-05-06 17:55:20
【问题描述】:

我有一个使用 Microsoft.AspNet.WebApi.OwinSelfHost 的简单项目。这是一个 Owin 自托管的 WebApi 2 项目。该项目非常类似于:-

https://github.com/attilah/AngularJSAuthentication

我唯一的区别是我的安全模块是在单独的程序集中(一切都类似于上面的项目)。我使用 Autofac 来解决我的依赖关系。

这个项目在没有 Autofac 的情况下工作。但是,一旦我尝试使用相应的安全模块和 Autofac 设置我的项目,我所面临的就是这个错误:-

Could not load file or assembly 'System.Web.Http, Version=5.2.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)

我的配置非常受此线程的启发:-

Dependency injection not working with Owin self-hosted Web Api 2 and Autofac

此外,我发现其他人正在为 Autofac 的这个依赖错误而苦苦挣扎,并且能够通过重新安装 Microsoft.AspNet.WebApi.WebHost 来解决类似的问题:

GlobalConfiguration.Configure() not present after Web API 2 and .NET 4.5.1 migration

但这对我不起作用,我不知道为什么。我尝试从头开始重新安装所有内容也无济于事。除了 Autofac 在重新安装时还有一些权限问题之外。

有人遇到过这个错误吗?请帮忙。谢谢!

【问题讨论】:

    标签: .net asp.net-web-api autofac owin self-hosting


    【解决方案1】:

    有时,.nuget 还原可能会出现问题。尽管在成功签入后,将 .nuget.config 文件中的 disableSourceControlIntegration 设置为 false 可以解决此问题。 添加 key="disableSourceControlIntegration" value="false"

    【讨论】:

      【解决方案2】:

      打开托管的 nuget 包以获得解决方案,并安装最新的 Microsoft Asp.net Web Api 2.2,这解决了我的问题。

      【讨论】:

        【解决方案3】:

        在@user998660 在above thread 中的评论的帮助下,我设法解决了这个依赖问题。

        我所做的是:

        • 我在项目的引用中右键单击了程序集 System.Web.Http。
        • 我注意到我的项目引用的程序集版本是5.2.3.0
        • 从上面的错误中我知道 Autofac 正在尝试引用 System.Web.Http 版本5.2.0.0。我需要一种方法来告诉我的应用使用更新版本的5.2.3.0 而不是5.2.0.0

        这是我需要添加到我的 Web.config 的 <assemblyBinding> 部分的内容:

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

        【讨论】:

        • 我可以确认也为我解决了这个问题
        • 这对我也有帮助。您应该已将此标记为答案。
        • 我所要做的就是将&lt;bindingRedirect ... /&gt; 元素更新为@amitthk 发布的内容。谢谢!
        • 如果您使用 Nuget 包管理器,它会在您再次安装 WebApi 包时为您修复。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        • 2019-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多