Autofac错误:无法加载文件或汇编系统。我的项目是Owin WebApi2 SelfHost

时间:2022-02-05 13:30:51

I have a simple project which uses Microsoft.AspNet.WebApi.OwinSelfHost. It is a Owin self hosted WebApi 2 project.The project is pretty much similar to:-

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

https://github.com/attilah/AngularJSAuthentication

https://github.com/attilah/AngularJSAuthentication

The only difference I have is my Security module is in separate Assembly (everything similar to above project). I use Autofac to resolve my dependencies.

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

This project worked when there's no Autofac. But once I am trying to setup my project with the corresponding security modules and Autofac all I am facing is this error:-

这个项目在没有自动控制的情况下成功了。但是,一旦我试图用相应的安全模块和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)

My configuration is very much inspired from this thread:-

我的配置是非常鼓舞从这个线程:-

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

依赖注入不能与Owin自托管Web Api 2和Autofac一起工作。

Moreover, I found someone else struggled with this dependency bug of Autofac and was able to get similar issue resolved by re-installing Microsoft.AspNet.WebApi.WebHost:

此外,我还发现有人在与Autofac的依赖缺陷做斗争,并能够通过重新安装Microsoft.AspNet.WebApi.WebHost来解决类似的问题:

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

在Web API 2和.NET 4.5.1迁移之后,没有出现GlobalConfiguration.Configure()

But this doesn't work for me I don't know why. I've tried re-installing everything from scratch also doesn't help. Besides the fact that Autofac has several permission issues while re-installing as well.

但这对我没用我不知道为什么。我试过从头重新安装所有东西也没有帮助。此外,Autofac在重新安装时也存在几个权限问题。

Anybody has any experience with this error? please help. Thanks!

有人有过犯这个错误的经历吗?请帮助。谢谢!

3 个解决方案

#1


20  

I managed to fix this dependency issue with help from comment by @user998660 in above thread.

在上面的线程中,我通过@user998660的注释帮助修复了这个依赖关系问题。

What I did is:

我所做的是:

  • I right clicked the assembly System.Web.Http in my Project's references.
  • 我右键单击assembly System.Web。Http在我项目的引用中。
  • I noticed that the version of assembly referenced by my project is 5.2.3.0
  • 我注意到我的项目引用的汇编版本是5.2.3.0
  • From the error above I know that Autofac is trying to reference System.Web.Http version 5.2.0.0. I needed a means to tell my app to use the newer version 5.2.3.0 instead of 5.2.0.0.
  • 从上面的错误中,我知道Autofac试图引用System.Web。5.2.0.0 Http版本。我需要一种方法来告诉我的应用使用较新的版本5.2.3.0而不是5.2.0.0。

This is what I needed to add to my Web.config's <assemblyBinding> section:

这是我需要添加到我的网络的东西。配置的< 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>

#2


0  

Open managed nuget packages for solution, and install the latest Microsoft Asp.net Web Api 2.2 and that solved my problem.

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

#3


0  

Some times, the .nuget restore could give an issue.. though after successful checkin, setting disableSourceControlIntegration to false in .nuget.config file, would resolve this issue. add key="disableSourceControlIntegration" value="false"

有时,nuget恢复可能会带来问题。虽然成功签入后,将disableSourceControlIntegration设置为false in .nuget。配置文件,将解决这个问题。添加关键= " disableSourceControlIntegration " value =“false”

#1


20  

I managed to fix this dependency issue with help from comment by @user998660 in above thread.

在上面的线程中,我通过@user998660的注释帮助修复了这个依赖关系问题。

What I did is:

我所做的是:

  • I right clicked the assembly System.Web.Http in my Project's references.
  • 我右键单击assembly System.Web。Http在我项目的引用中。
  • I noticed that the version of assembly referenced by my project is 5.2.3.0
  • 我注意到我的项目引用的汇编版本是5.2.3.0
  • From the error above I know that Autofac is trying to reference System.Web.Http version 5.2.0.0. I needed a means to tell my app to use the newer version 5.2.3.0 instead of 5.2.0.0.
  • 从上面的错误中,我知道Autofac试图引用System.Web。5.2.0.0 Http版本。我需要一种方法来告诉我的应用使用较新的版本5.2.3.0而不是5.2.0.0。

This is what I needed to add to my Web.config's <assemblyBinding> section:

这是我需要添加到我的网络的东西。配置的< 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>

#2


0  

Open managed nuget packages for solution, and install the latest Microsoft Asp.net Web Api 2.2 and that solved my problem.

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

#3


0  

Some times, the .nuget restore could give an issue.. though after successful checkin, setting disableSourceControlIntegration to false in .nuget.config file, would resolve this issue. add key="disableSourceControlIntegration" value="false"

有时,nuget恢复可能会带来问题。虽然成功签入后,将disableSourceControlIntegration设置为false in .nuget。配置文件,将解决这个问题。添加关键= " disableSourceControlIntegration " value =“false”