Saturday, June 23, 2012

Ambigous Match Found error when using IBatis on .NET 4.0 or NHibernate on .NET 4.0. Caused by Castle's Dynamic Proxy.

I was working on an important project for my client. My client's application is based on .NET 3.5. However, the framework where the service we are developing needs to be done on .NET 4.0. Worst, the SQL Framework they are using are IBatis. This is an old version of IBatis.

When we tried running the Service, we hit this error : "Ambiguous Match"

I discovered that this is due to the Dynamic Proxy.DLL from Castle. Somehow, this code fails :

gen.Emit(OpCodes.Call, typeof(Monitor).GetMethod("Enter"));

So I modified the code to this call (based on some research) :

Which worked.

The reason for the error is due to Reflection's implementation difference from .NET 2.0 and .NET 4.0. .NET 4.0 requires a second parameter as opposed to the old one.

However, making it work is proven to be difficult as i don't have the signature key that is used for the Dynamic Proxy. Note that I decompiled the DLL to edit the code so I need to recompile it back. It took me few hours to get the original signature so that it would work on my project.

In-case you have the same issue, I have uploaded the source code that I modified as well as the resulting DLL so you can just re-use this without going through the nightmare I had gone thru.

Here's the link :

Here's the DLL link :

Here's the source code link :

Note that the source code was compiled using Visual Studio 2010. Enjoy!!

gen.Emit(OpCodes.Call, typeof(Monitor).GetMethod("Enter"new Type[] { typeof(object) }));