使用Microsoft.Bcl.Async与代码分析导致错误 [英] Using Microsoft.Bcl.Async with Code Analysis causes errors

查看:2142
本文介绍了使用Microsoft.Bcl.Async与代码分析导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 Microsoft.Bcl.Async 和代码分析,但是当我运行代码分析,我得到一个或多个错误。



我在使用Visual Studio 2012年更新2。



这是容易的,我复制:




  1. 创建一个新的默认控制台应用程序的目标 .NET 4中

  2. 右键点击参考然后选择管理的NuGet包...

  3. 点击在线键,键入异步在线搜索框。

  4. 您应该看到异步用于.NET Framework 4 ... 。点击安装并接受所有的问题。

  5. 添加到的Main() A行,上面写着:;采用System.Threading.Tasks 和; TaskEx.Delay(1000)

  6. 转至项目属性,代码分析部分,并勾选上构建启用代码分析

  7. 编译程序。



我得到两个代码分析错误:




CA0052错误运行代码分析CA0052:未选定的目标。 [错误和警告](全球)



CA0055错误运行代码分析CA0055:无法加载ConsoleApplication2.exe。同时读取模块ConsoleApplication2时遇到了以下错误:无法解析成员引用:[Microsoft.Threading.Tasks,版本= 1.0.12.0,文化=中性公钥= b03f5f7f11d50a3a] System.Threading.Tasks.TaskEx ::延迟。 [错误和警告](全球)




我得到的不同的的代码分析其他测试程序中的错误。一个基本的Windows窗体应用程序,我试图给我:




CA0001错误运行代码分析CA0001:遇到下列错误而读取模块AsyncForNet4 :无法解析成员引用:[Microsoft.Threading.Tasks,版本= 1.0.12.0,文化=中性公钥= b03f5f7f11d50a3a] System.Threading.Tasks.TaskEx ::延迟。 [错误和警告](全球)




两个问题:




  1. 其他人能重现此问题?

  2. 有没有人有吗?


解决方案

正如妮可提到的,这是因为代码分析/ FxCop的是执行,强有力的品牌,其中包括版本完全匹配。这种行为很有道理的.NET框架的直到的你开始约束力重定向因子(或其他平台,如商店,电话和放大器;的Silverlight总是允许组件的更高版本,以配合早期版本) ,其中的FxCop不尊重。



在我写的FxCop这种原始的行为,一切都结束了正确性VS现实世界的优化。当时,我们没有一个选择了除通过在app.config。然而,在幸运的是我离开球队,对球队的一些聪明人既可以通过命令行和Visual Studio中增加了一个。



通过该命令 - 行:

  FxCopCmd.exe / assemblycomparemode:StrongNameIgnoringVersion ... 

通过Visual Studio的:




  1. 在项目中右键单击解决方案资源管理器并选择
    卸载

  2. 在项目中右键单击解决方案资源管理器并选择修改

  3. 在最初的<的PropertyGroup> 元素中,添加以下内容:< CodeAnalysisAdditionalOptions> / assemblycomparemode:StrongNameIgnoringVersion< / CodeAnalysisAdditionalOptions>

  4. 在项目中右键单击解决方案资源管理器下,选择刷新提示时保存更改。



这只会在Visual Studio 2012和更高的工作。


I'm trying to use Microsoft.Bcl.Async and Code Analysis, but when I run Code Analysis I get one or more errors.

I'm using Visual Studio 2012 with Update 2.

This is easy for me to reproduce:

  1. Create a new default Console App that targets .Net 4.
  2. Right click References then select Manage NuGet Packages...
  3. Click Online and type async into the Search Online box.
  4. You should see Async for .Net Framework 4 .... Click Install and accept all questions.
  5. Add to Main() a line that says: TaskEx.Delay(1000); and a using System.Threading.Tasks;
  6. Go to project properties, Code Analysis section and tick 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. [Errors and Warnings] (Global)

CA0055 Error Running Code Analysis CA0055 : Could not load 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. [Errors and Warnings] (Global)

I get different code analysis errors for other test programs. A basic Windows Forms app I tried gives me:

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. [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. 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.

I wrote this original behavior in FxCop, and it was over optimizing for correctness vs real world. At the time, we didn't have an opt out other than via the 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.

Via the command-line:

FxCopCmd.exe /assemblycomparemode:StrongNameIgnoringVersion ...

Via 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>
  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.

这篇关于使用Microsoft.Bcl.Async与代码分析导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆