问题
I am unable to generate the Microsoft Fakes assembly for a class library (.NET 4.5) when said class library references a PCL (Portable Class Library) targetting .Net 4.5 and Silverlight 5 under Visual Studio 2013.
This only occurs if the class library (.NET 4.5):
- Declares a type deriving from a type which can both be found in System.dll v4.0.0.0 and v2.0.5.0
- Declares another type deriving from one declared in the PCL library, which in turn derives from a type declared in System.dll v.2.0.5.0
Both the .NET 4.5 class library and the PCL library compiles just fine. However I have a Unit Test project (also .NET 4.5) which needs to generate Fakes for the .NET 4.5 class library. Trying to generate the fakes fails and gives me the following error.
error : assembly Repro.dll failed to load properly Could not resolve assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'. Are you missing an assembly reference?
Trying to add a reference to System.Dll v2.0.5.0 in my Unit Test project fails since a reference to the component 'System' v4.0.0.0 already exists in the project.
I also tried adding an App.Config to my Unit Test project and defining an assembly binding redirection for System.Dll, but it did not work. I suspect assembly binding redirection has zero effect on Fakes generation.
Reproducing the issue is easy.
Create a Portable Class Library named 'Repro.Portable' which has a single interface:
using System.ComponentModel;
namespace Repro.Portable
{
public interface IPortableEntity : INotifyPropertyChanged
{
}
}
Then create a .NET 4.5 Class Library named 'Repro' which references the Portable Class Library and declare the following two interfaces.
using Repro.Portable;
namespace Repro
{
public interface ISpecializedEntity : IPortableEntity
{
}
}
and...
using System.ComponentModel;
namespace Repro
{
public interface IOtherInterface : INotifyPropertyChanged
{
}
}
Finally, create a Unit Test Project referencing both the Repro and Repro.Portable assemblies. Compiling at this point will succeed. However, trying to generate the Microsoft Fakes for the Repro assembly will fail.
As all of this seems like a pretty legitimate thing to do, I am left wondering if I ran into a bug in the Microsoft Fakes generator, or if a workaround exists.
Is there any way to make the Fakes generation work or am I stuck using a different Mocking framework when encountering this specific scenario ?
回答1:
Microsoft provided a workaround for the bug.
To get past the issue, add an XML blob like below in your .fakes file.
<Compilation>
<Reference Path="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile\Profile158\System.dll" FullName="System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"/>
</Compilation>
It does work when building locally, however I have yet to test if this is a viable workaround when using TFS online build server or not.
来源:https://stackoverflow.com/questions/22271705/unable-to-generate-fakes-for-an-assembly-referencing-a-portable-class-library