简体   繁体   English

结合使用Microsoft.Bcl.Async和代码分析会导致错误

[英]Using Microsoft.Bcl.Async with Code Analysis causes errors

I'm trying to use Microsoft.Bcl.Async and Code Analysis, but when I run Code Analysis I get one or more errors. 我试图使用Microsoft.Bcl.Async和代码分析,但是运行代码分析时遇到一个或多个错误。

I'm using Visual Studio 2012 with Update 2. 我正在将Visual Studio 2012与Update 2配合使用。

This is easy for me to reproduce: 这对我来说很容易重现:

  1. Create a new default Console App that targets .Net 4 . 创建一个针对.Net 4的新默认控制台应用程序。
  2. Right click References then select Manage NuGet Packages... 右键单击References然后选择Manage NuGet Packages...
  3. Click Online and type async into the Search Online box. 单击“ Online然后在Search Online框中键入async
  4. You should see Async for .Net Framework 4 ... . 您应该看到Async for .Net Framework 4 ... Click Install and accept all questions. 单击Install并接受所有问题。
  5. Add to Main() a line that says: TaskEx.Delay(1000); Main()添加一行,内容为: TaskEx.Delay(1000); and a using System.Threading.Tasks; using System.Threading.Tasks;
  6. Go to project properties, Code Analysis section and tick Enable Code Analysis on Build . 转到项目属性的“代码分析”部分,然后勾选Enable Code Analysis on Build
  7. Compile the program. 编译程序。

I get two Code Analysis errors: 我收到两个代码分析错误:

CA0052 Error Running Code Analysis CA0052 : No targets were selected. CA0052错误运行代码分析CA0052:未选择目标。 [Errors and Warnings] (Global) [错误和警告](全局)

CA0055 Error Running Code Analysis CA0055 : Could not load ConsoleApplication2.exe. CA0055运行代码分析错误CA0055:无法加载ConsoleApplication2.exe。 The following error was encountered while reading module 'ConsoleApplication2': Could not resolve member reference: [Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]System.Threading.Tasks.TaskEx::Delay. 读取模块'ConsoleApplication2'时遇到以下错误:无法解析成员引用:[Microsoft.Threading.Tasks,版本= 1.0.12.0,Culture =中性,PublicKeyToken = b03f5f7f11d50a3a] System.Threading.Tasks.TaskEx :: Delay。 [Errors and Warnings] (Global) [错误和警告](全局)

I get different code analysis errors for other test programs. 对于其他测试程序,我得到了不同的代码分析错误。 A basic Windows Forms app I tried gives me: 我尝试过的基本Windows Forms应用程序为我提供了:

CA0001 Error Running Code Analysis CA0001 : The following error was encountered while reading module 'AsyncForNet4': Could not resolve member reference: [Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]System.Threading.Tasks.TaskEx::Delay. CA0001错误,运行代码分析CA0001:读取模块'AsyncForNet4'时遇到以下错误:无法解析成员引用:[Microsoft.Threading.Tasks,版本= 1.0.12.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a] System.Threading。 Tasks.TaskEx :: Delay。 [Errors and Warnings] (Global) [错误和警告](全局)

Two questions: 两个问题:

  1. Can anyone else reproduce this problem? 任何人都可以重现此问题吗?
  2. Does anyone have a solution for it? 有人对此有解决方案吗?

As mentioned by Nicole, this occurs because Code Analysis/FxCop is enforcing that strong names including versions match exactly. 正如Nicole提到的,发生这种情况是因为Code Analysis / FxCop强制要求强名称(包括版本)完全匹配。 This behavior makes sense for .NET Framework, until you start to factor in binding redirects (or other platforms such as Store, Phone & Silverlight which always allow later versions of an assembly to match an earlier version), which FxCop does not respect. 对于.NET Framework,此行为才有意义, 直到您开始考虑绑定重定向(或其他平台,例如Store,Phone和Silverlight,它们始终允许程序集的更高版本与早期版本匹配)之前,FxCop均不考虑。

I wrote this original behavior in FxCop, and it was over optimizing for correctness vs real world. 我在FxCop中编写了此原始行为,并且在针对正确性和实际情况进行了过度优化。 At the time, we didn't have an opt out other than via the App.Config. 当时,除了通过App.Config之外,我们没有其他选择。 However, luckily after I left the team, some smart person on the team added one both via the command-line and within Visual Studio. 但是,幸运的是,在我离开团队后,团队中的一些聪明人通过命令行和在Visual Studio中添加了一个。

Via the command-line: 通过命令行:

FxCopCmd.exe /assemblycomparemode:StrongNameIgnoringVersion ...

Via Visual Studio: 通过Visual Studio:

  1. Right-click on the project in Solution Explorer and choose Unload 解决方案资源管理器中右键单击该项目,然后选择“ 卸载”
  2. Right-click on the project in Solution Explorer and choose Edit 解决方案资源管理器中右键单击该项目,然后选择“ 编辑”。
  3. Within the first <PropertyGroup> element, add the following: <CodeAnalysisAdditionalOptions> /assemblycomparemode:StrongNameIgnoringVersion</CodeAnalysisAdditionalOptions> 在第一个<PropertyGroup>元素内,添加以下内容: <CodeAnalysisAdditionalOptions> /assemblycomparemode:StrongNameIgnoringVersion</CodeAnalysisAdditionalOptions>
  4. Right-click on the project in Solution Explorer , choose Reload saving the changes when prompted. 解决方案资源管理器中右键单击项目,在出现提示时选择“ 重新加载以保存更改”。

This will only work in Visual Studio 2012 and higher. 这仅在Visual Studio 2012及更高版本中有效。

This happens because the versions of the dependencies declared in Bcl.Async assemblies don't match those available at analysis time. 发生这种情况是因为Bcl.Async程序集中声明的依赖项版本与分析时可用的那些版本不匹配。 The simplest workaround is to adjust FxCop's AssemblyReferenceResolveMode as described at http://davesbox.com/archive/2008/06/14/reference-resolutions-changes-in-code-analysis-and-fxcop-part-2.aspx . 最简单的解决方法是按照http://davesbox.com/archive/2008/06/14/reference-resolutions-changes-in-code-analysis-and-fxcop-part-2.aspx所述调整FxCop的AssemblyReferenceResolveMode

Having the same problem, and looking for a solution. 有同样的问题,并寻找解决方案。 The only mention I've found is in the comments of the bcl blog post - Microsoft.Bcl.Async is Now Stable (page 3 of comments) where Immo Landwerth's response to someone having the same issue is; 我发现的唯一提及是在bcl博客文章的评论中-Microsoft.Bcl.Async现在稳定 (评论的第3页),其中Immo Landwerth对具有相同问题的人的回复是:

We're looking into it. 我们正在调查。 At first glance it seems like a unification issue in the VS static code analysis feature (FxCop). 乍一看,这似乎是VS静态代码分析功能(FxCop)中的一个统一问题。 We've contacted the owners of it. 我们已经联系了它的所有者。 Unfortunately, I don't think there is a workaround other than disabling code analysis for those projects :-( 不幸的是,除了为那些项目禁用代码分析之外,我认为没有其他解决方法:-(

The response is dated 26th April 2013, whether there have been any developments since then. 回复日期为2013年4月26日,此后是否有任何进展。

So for now I guess the workarounds are: 所以现在我想解决方法是:

  • Disable code analysis 禁用代码分析
  • Rewrite your code not to use TaskEx.Delay() 重写代码以不使用TaskEx.Delay()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM