Wednesday, November 28, 2007

Rhino.Mocks and AutoMockingContainer

I am working on an ASP.Net project based on WCSF (Web Client Software Factory). For test, I downloaded the latest Rhino.Mocks, NUnit and MbUnit libraries. It works fine at the beginning because all the libraries are new.

For the tests, I need to mock some services and views. I remembered that I used AutoMockingContainer class and I had a generic class for mocking purpose. Then I copied the AutoMockingContainer to the project. After that, my mocking objects stop working and an exception was raised. Here is the error message:

[System.IO.FileLoadException] {"Could not load file
or assembly 'Rhino.Mocks, Version=, Culture=neutral,
PublicKeyToken=0b3305902db7183f' or one of its dependencies. The located assembly's
manifest definition does not match the assembly reference. (Exception from HRESULT:
0x80131040)":"Rhino.Mocks, Version=, Culture=neutral, PublicKeyToken=0b3305902db7183f"}

It was very confusing. I thought it might be my Visual Studio or Windows wrong. However, I restarted my Windows and VS again. Still I got the error message. Finally I found that it is the call to AutoMockingContainer class where exception is raised. When I read the error message. I realize that the version of Rhino.Mocks is difference from the same library which is used in the project (ver 3.3.0906).

The problem is that when AutoMockingContainer class is called, CLR is looking for Rhino.Mocks with ver This not-matching caused the exception. It is very interesting. Why does CLR not automatically search for the class or method in a new ver?

Any way, fortunately, I found the source codes of AutoMockingContainer and rebuilt the binary with the same version of Rhino.Mocks. After that, everything works fine. I think that if a library used in a project which depends on old version of assembly, the old version ones have to be installed correctly. It is not as simple as copy library dll files. You have to run deployment or installation to install all the libraries, including dependencies. If both old and new one are used in the same project, the old ones have to be upgraded to new ones.

I think this is an issue of assembly version, and Microsoft has solve the problem. As developers, we have to follow the version management policy to manage version issue. I'll further investigate the issue and try this project with two different versions of Rhino.Mocks library files in one bin directory.