System.Security.Cryptography.X509Certificates.X509Certificate2 is not available in the target framework version
Asked Answered
M

1

7

I installed VS2015 RTM en VS2013 Update 5 RTM. Now my solution is not building because I have an interface that has as returntype a X509Certificate2. Now my fakes aren't build. I also created a test project and there I have the same problem so it isn't my solution. The message that I get is:

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.

Now I uninstalled VS2015 RTM but the problem is still there. When I comment out the the method with as return type the certificate everything works. When I uncomment it, the problem is there.

Update 1 I just tested this on another system. First I tried it with VS2013 Update 4 en VS2015 RC. With that setup everything was fine. I then installed Update 5 RTM on that system and then it didn't work anymore. So Update 5 must be the problem! End update

To reproduce: Create a solution with 2 class libraries and 1 test project with .Net Framework 4.5.1. Create an interface in class library 1.

namespace ClassLibrary1.Interfaces
{
    public interface ICertificateProvider
    {
        // Comment this line so you can build your fakes assembly...
        X509Certificate2 getbla();
    }
}

Create a class in the second class library. Implement the interface and add a reference to the first class library.

namespace ClassLibrary1
{
    public class CertificateProvider :   ClassLibrary1.Interfaces.ICertificateProvider
    {
        public X509Certificate2 getbla()
        {
            throw new NotImplementedException();
        }
    }
}

Add a fakes assembly for the interface project in the unittest project. Past the following code in a test:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1.Interfaces.Fakes;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            StubICertificateProvider provider = new StubICertificateProvider();
        }
    }
}

Now your project won't build. If you comment the method in the interface your project will build. Enable the diagnostics in your .fakes file for the error message.

Anyone a solution?

Update 2 Changing the solution to use .Net Framework 4.6 works. Changing to 4.5.2 doesn't work.

Update 3 Link to official bug of Github: https://github.com/dotnet/coreclr/issues/1303

Michellemichels answered 28/7, 2015 at 6:15 Comment(4)
Hi, I got the same error today. Wondering if there has been an update on it?Toussaint
No not yet. If you find a solution for this, please share!Michellemichels
For now I just uninstalled Update 5 and VS2013. After that I installed VS2013 and Update 4 again. That is working. Only uninstalling Update 5 and reinstalling Update 4 didn't work. For now this is our only solution...Michellemichels
Thanks. For me, it was one method in an interface that was causing this issue. I updated the interface to return the object type rather than X509Certificate and handled and handled the clients so that they type cast the return object. Not the ideal solution but only one that worked for me.Toussaint
S
0

I just updated the test project to .NET 4.6 and it works great. After that I had a couple internal classes which are not visible to the fakes so using the

[assembly: InternalsVisibleT

in the AssemblyConfig.cs is also a good idea to get it finally running.

Solemnize answered 14/9, 2015 at 4:34 Comment(1)
Yes upgrading to 4.6 fixes the problem. But that isn't really a fix. We don't have the permission yet to upgrade so we're stuck with a broken solution.Michellemichels

© 2022 - 2024 — McMap. All rights reserved.