2
votes

I'm getting this error when trying to get instanse of class in separated Application Domain. Here is code:

string assemblyName = Assembly.GetExecutingAssembly().FullName;
string typeName = "Namespace.ClassName";

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
SecurityZone zone = SecurityZone.MyComputer;

// Set up the Evidence
Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
Evidence evidence = new Evidence(baseEvidence);
evidence.AddAssembly(assemblyName);
evidence.AddHost(new Zone(zone));

AppDomain app = AppDomain.CreateDomain("Processor AppDomain", evidence, setup);
core = (Core)app.CreateInstanceAndUnwrap(assemblyName, typeName);

Both classes (calling and called) are in same assemby (COM dll).

So does anybody know what is the reason ot this exception? Thanks for any response.

1
Are you sure typeName refers to the Core type? What does the debugger show when you look at the result of CreateInstanceAndUnwrap() without casting? - svick
Thanks for response, svick. typeName realy refers to Core, otherwise I'll get other exception of type FileNotFoundException or TypeLoadException. When I changed last string of this example to Object core = app.CreateInstanceAndUnwrap(assemblyName, typeName); and debugged it, everything was going right scenario - fields of Core class was initialized, constructor called and object was returned. But this returned object is System.Runtime.Remoting.Proxies.__TransparentProxy by type for calling class. and looks like I could not cast this object to Core type. - Shelest
I'm not sure you can do what you're trying to achieve. The purpose of creating an object in a separate AppDomain is to isolate parts of your application. By casting to the implementation that resides in the second AppDomain, you're trying to break the marshalling and the isolation, aren't you ? - Seb
Nice assumption Seb, but there are some conditions to think that AppDomain should provide a convenient methods for working with related Objects, and CreateInstanceAndUnwrap method is on of them. You can see that I'm trying to do the same thing as posted on msdn.microsoft.com/en-us/library/3c4f1xde.aspx (see an Example). - Shelest
Is your type you are trying to create in new app domain marked with Serializable or does your type inherit MarshalByRefObject type? - psulek

1 Answers

0
votes

How is your application referencing the com dll? Is this also a .net dll? If it is being referenced as a project by your application and is also registered as a com dll, then there is a chance that your app is refering to two different copies of the dll in which case it will consider your Core classes to be two separate classes as they are in different dlls. You could consider hooking up the appdomains AssemblyLoad event to debug this and check the location of the assembly being loaded.