异常无法加载程序集 Cors

Exception could not load assembly Cors

我正在我的应用程序中开发一个 webApi。我有一个奇怪的问题。我尝试在一个独立的控制台应用程序中执行此项目,并且一切运行良好。我的问题是当我尝试在另一个大型应用程序中加载该 Web api 时。我收到此错误:

Could not load file or assembly 'Microsoft.Owin, Version=3.0.1.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)

这是我使用它的class:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll); //HERE IS THE PROBLEM
        HttpConfiguration configuration = new HttpConfiguration();
        configuration.Routes.MapHttpRoute(
            name: "TestApi",
            routeTemplate: "test/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional}
        );
        app.UseWebApi(configuration);
    }
}

我调试了,我的问题是在使用app.UserCors(CorsOptions.AllowAll)函数的时候。我使用 Nuget 包管理器安装了 Microsoft CORS 包,我尝试更新它但仍然收到此错误。我在这里搜索 但解决方案对我不起作用。

我有这个 app.config 文件:

  <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="3.0.1" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
</assemblyBinding>

也许问题出在System.Web.Cors?我有点迷路了。

在你的配置中你说

<dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.2.0" />
  </dependentAssembly>

这意味着,您使用 Microsoft.Owin 版本 2.0.2.0 但是你的一些代码(我想是 IAppBuilder 的实现)需要 "Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

如果您使用 NuGet,升级包,如果不使用 - 您应该下载 3.0.1.0 版本,更新参考并更改配置文件