Saturday, February 26, 2011

Could not load file or assembly 'System.ServiceModel.Web, Version=2.0.5.0

Currently I am working on a Unit Test project based on Microsoft Testing Framework for .NET 4.0 which is testing a project based on Silverlight 4.0. So far everything has been fine but today I wrote a test that was exercising the serialization of a class to JSON and ran into the following exception. The class is using System.ServiceModel library and its fields are annotated.

Here is the error I was getting when running the unit test in Visual Studio 2010:
System.IO.FileNotFoundException: Could not load file or assembly 'System.ServiceModel.Web, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].


I tried to get both the Project Under Test, and the Test Project to use the same System.ServiceModel library but was not successful. Visual Studio would not allow the Silverlight Project to use the same libraries as the .NET 4.0 Framework and it would also not allow the Unit Test Project to use the same libraries as the Silverlight Project. After trying different options, I found a very hacky solution.

The hacky fix is to specify the reference paths for the Unit Test project. Here is what you need to do:



  1. In Visual Studio 2010, right click on the Unit Test project and select Properties
  2. Click on Reference Paths in the menu on the left
  3. Add the Silverlight Reference path. For me this was: 
    <C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\>.
    To find out your specific path, look at the path of the System.ServiceModel reference being used by the Silverlight Project. 
  4. Save and rebuild the Unit Test project. 
  5. Run the test again and this time it should run successfully.
Visual Studio shows a few warnings about duplicate libraries, but that's a small price to pay for getting the tests to run. If you know of a better way to fix this problem, please share your fix on a comment below.

3 comments:

  1. thank you , helped me a lot. i was breaking my head with this issue :)

    ReplyDelete
  2. I had the same problem with Visual Studio 2012 and Silverlight 5.
    Your solution is working very well, without any warnings.
    Thank you.
    Cristian

    ReplyDelete
  3. I had the similar problem.. My solution was copying dll to bin folder and reference them from bin folder.. I hope it helps..

    ReplyDelete