InvalidProgramException with shims in Visual Studio 2015

人盡茶涼 提交于 2019-12-08 18:22:43

问题


An almost similar question has already been asked a while ago but not answered yet. And since my setup is a little different, I start a new try:

I demonstrate a shortened version of my code, but even this short version produces the error.

I have a method that returns an instance of a System.Printing.LocalPrintServer:

public class PrintServerProvider
{
    public LocalPrintServer Provide()
    {
        return new LocalPrintServer(new string[0], PrintSystemDesiredAccess.EnumerateServer);
    }
}

For this method I implemented a unit test using shims:

[TestClass]
public class PrintServerProviderTests
{
    [TestMethod]
    public void Provide_Success()
    {
        using (ShimsContext.Create())
        {
            bool constructorCalled = false;
            ShimLocalPrintServer.ConstructorStringArrayPrintSystemDesiredAccess = (instance, props, access) =>
            {
                constructorCalled = true;
            };

            PrintServerProvider provider = new PrintServerProvider();
            LocalPrintServer server = provider.Provide();
            Assert.IsNotNull(server);
            Assert.IsTrue(constructorCalled);
        }
    }
}

In Visual Studio 2013 this test works fine.
But in Visual Studio 2015 (Update 1), when the tested method calls new LocalPrintServer(...) an

InvalidProgramException at System.Printing.LocalPrintServer..ctor(String[] propertiesFilter, PrintSystemDesiredAccess desiredAccess) at DemoProject.PrintServerProvider.Provide() in C:\Projects\DemoProject\Program.cs:Line 10. ...

is raised. (The normal productive call works fine. The exception is only raised when running the test method)

The referenced assemblies are:

  • System.Printing (v4.0.30319) from C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Printing.dll
  • System.Printing.4.0.0.0.Fakes
  • Microsoft.QualityTools.Testing.Fakes (v2.0.50727) from C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PublicAssemblies\Microsoft.QualityTools.Testing.Fakes.dll

System.Printing.Fakes

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="System.Printing" Version="4.0.0.0"/>
  <StubGeneration>
    <Clear/>
  </StubGeneration>
  <ShimGeneration>
    <Clear/>
    <Add FullName="System.Printing.LocalPrintServer!"/>
  </ShimGeneration>
</Fakes>

Both projects (productive and test) use .NET Framework 4.5.

I tried resetting the framework as well as the references. I tried different versions of the referenced assemblies. And I tried 64bit and 32bit in all variations.

In the real code, I use shims of other classes too. LocalPrintServer is the only class where the exception occurs.

Can anybody explain what causes this InvalidProgramException and how to solve it?

I set a bounty on this question, which will probably be solved by the same answer. So feel free to post there too if you think your answer is bounty-worth.

来源:https://stackoverflow.com/questions/35231888/invalidprogramexception-with-shims-in-visual-studio-2015

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!