I have an application which is throwing an AmbiguousMatchException when I call AppDomain.CreateInstanceAndUnwrap to instantiate a type in another AppDomain. This is happening on a customer's computer which I don't have direct access to. I think the problem is that there are two copies of the same assembly loaded. Is there any way to figure out if this is the case and where the two assemblies are being loaded from? Will enabling the fusion log provide any additional information?
views:
155answers:
1
+1
Q:
Debugging an AmbiguousMatchException thrown when calling AppDomain.CreateInstanceAndUnwrap in .NET
A:
The fusion log might help, but another option might be to hook the AssemblyLoad
event:
AppDomain.CurrentDomain.AssemblyLoad += (s, a) =>
{
Console.WriteLine(a.LoadedAssembly.FullName);
Console.WriteLine(a.LoadedAssembly.CodeBase);
};
There are two main causes of this error:
- coincidental naming - i.e. Foo.dll and Bar.dll both have a
Some.Namespace.Type
type - different versions (mainly GAC) referenced by different components - i.e. your DAL loads v2 of some dll, and your UI/utility code loads v4 of the same dll
Of course, another option is that your AppDomain
s have infected each-other (it is very (too?) easy to accidentally suck a reference over an AppDomain
boundary by exposing it on the API of the MarshalByRef
object).
Marc Gravell
2009-01-07 07:50:48