.net – System.Security.Cryptography.X509Certificates.X509Certificate2在目标框架版本中不可用

前端之家收集整理的这篇文章主要介绍了.net – System.Security.Cryptography.X509Certificates.X509Certificate2在目标框架版本中不可用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我安装了VS2015 RTM和VS2013 Update 5 RTM.现在我的解决方案没有构建,因为我的接口具有返回类型X509Certificate2.现在我的假货不是建造的.我还创建了一个测试项目,我遇到了同样的问题,所以这不是我的解决方案.我收到的消息是:

Cannot generate stub for ClassLibrary1.Interfaces.ICertificateProvider: method System.Security.Cryptography.X509Certificates.X509Certificate2 ClassLibrary1.Interfaces.ICertificateProvider.getbla() unstubbable: method is abstract and could not be stubbed,type System.Security.Cryptography.X509Certificates.X509Certificate2 is not available in the target framework version.

现在我卸载了VS2015 RTM,但问题仍然存在.当我用返回类型注释掉该方法时,证书一切正常.当我取消注释时,问题出在那里.

更新1
我刚在另一个系统上测试了这个.首先,我尝试使用VS2013 Update 4和VS2015 RC.有了这个设置一切都很好.然后我在该系统上安装了Update 5 RTM,然后它不再工作了.所以Update 5一定是问题所在!
结束更新

重现:
使用.Net Framework 4.5.1创建包含2个类库和1个测试项目的解决方案.
在类库1中创建一个接口.

  1. namespace ClassLibrary1.Interfaces
  2. {
  3. public interface ICertificateProvider
  4. {
  5. // Comment this line so you can build your fakes assembly...
  6. X509Certificate2 getbla();
  7. }
  8. }

在第二个类库中创建一个类.实现接口并添加对第一个类库的引用.

  1. namespace ClassLibrary1
  2. {
  3. public class CertificateProvider : ClassLibrary1.Interfaces.ICertificateProvider
  4. {
  5. public X509Certificate2 getbla()
  6. {
  7. throw new NotImplementedException();
  8. }
  9. }
  10. }

为unittest项目中的接口项目添加fakes程序集.在测试中通过以下代码

  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using ClassLibrary1.Interfaces.Fakes;
  4.  
  5. namespace UnitTestProject1
  6. {
  7. [TestClass]
  8. public class UnitTest1
  9. {
  10. [TestMethod]
  11. public void TestMethod1()
  12. {
  13. StubICertificateProvider provider = new StubICertificateProvider();
  14. }
  15. }
  16. }

现在您的项目将无法构建.如果您在项目将构建的界面中注释该方法.
在.fakes文件中启用诊断以获取错误消息.

谁有解决方案?

更新2
更改使用.Net Framework 4.6的解决方案.更改为4.5.2不起作用.

更新3
链接到Github的官方错误
https://github.com/dotnet/coreclr/issues/1303

我刚刚将测试项目更新到.NET 4.6,它运行良好.在那之后我有几个内部类,这些假货是不可见的,所以使用

[assembly:InternalsVisibleT

在AssemblyConfig.cs中最好让它最终运行.

猜你在找的Windows相关文章