Postsharp OWIN 版本不匹配

Postsharp OWIN Version Mismatch

我在我的项目中使用 PostSharp 4.2.22.0 和 Owin 3.0.1。

编译时出现以下错误:

Unhandled exception (4.2.22.0, postsharp-net40-x86-srv.exe, CLR
4.0.30319.394271, Release): PostSharp.Sdk.CodeModel.AssemblyLoadException: Cannot find assembly 'microsoft.owin.security, version=2.1.0.0, culture=neutral, publickeytoken=31bf3856ad364e35'. [Version mismatch]

但是 PostSharp 与 Owin 有关系吗?为什么 Owin 版本对 PostSharp 很重要,这是两个完全不同的包。

此错误通常出现在引用旧版本程序集的项目中,该程序集在您的 web.config 文件中具有绑定重定向。我猜你的 web.config:

中有这样的东西
<dependentAssembly>
  <assemblyIdentity name="microsoft.owin.security" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0"/>
</dependentAssembly>

PostSharp 默认不处理 web.config/app.config 文件。通过将 PostSharpHostConfigurationFile MSBuild 属性 设置为 web.config 文件的路径,可以强制 PostSharp 加载绑定重定向。

您可以通过将这些行添加到您的 csproj 文件来实现:

<PropertyGroup>
   <PostSharpHostConfigurationFile>web.config</PostSharpHostConfigurationFile>
</PropertyGroup>

这种不便应该已经在 PostSharp 4.3 中修复,无需设置 PostSharpHostConfigurationFile 属性。但目前 4.3 还不是稳定版本。

编辑:代码已更新(缺少结尾“>”)