Pages

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 : https://www.dropbox.com/s/gk3zenow3ekoe0g/Castle.DynamicProxy.dll

Here's the source code link : https://www.dropbox.com/s/ndx1l55l8dk61n5/Castle.DynamicProxy.zip

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





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

6 comments:

  1. Hello my dear friend ,
    we searched this solution for the last six months,thank you so much for sharing this :)

    ReplyDelete
  2. You require time to create that attractive and in addition real effort to make such a high-quality blog post Match Found error when using IBatis on .NET 4.0 or NHibernate on .NET 4.0. Caused by Castle's Dynamic Proxy.

    ReplyDelete
  3. Hi,

    I tried to use your .dll but I encountered this error instead? Can you help me with it as I am quite new with IBatis.

    Cause: Value cannot be null.
    Parameter name: Argument 'target' can't be null ---> System.ArgumentNullException: Value cannot be null.
    Parameter name: Argument 'target' can't be null
    at Castle.DynamicProxy.AssertUtil.NotNull(Object argument, String argumentName)
    at Castle.DynamicProxy.ProxyGenerator.CreateProxy(Type[] interfaces, IInterceptor interceptor, Object target)
    at IBatisNet.DataAccess.Configuration.DaoProxy.NewInstance(Dao dao)
    at IBatisNet.DataAccess.Configuration.Dao.Initialize(DaoManager daoManager)

    Thank you.

    ReplyDelete
  4. Thanks it resolved my problem!!!

    ReplyDelete
  5. Thank you very much for sharing, it solved the problem I have been troubled for a long time, thanks again!

    ReplyDelete