Question

i have a advanced question which is related to remoting dlls in MEMORY. i have wcf service that take Dlls(Starter.DLL entryType) Also there is a Process dll which is referenced to wpf application . i have some steps:

1) wcf takes Dlls (add to stream memory..)

2) Process dll read from wcf

3) Process dll adds dll to currentdomain


 foreach (byte[] binary in deCompressBinaries)
                        {
                            AppDomain.CurrentDomain.Load(binary);
                        }

4) wpf accesses Process' CreateApplication method


  this._btnStartApp.Click += (s, args) => 
            {
               Process appMngr = new Process ();
                appMngr.CreateApplication();
            };

CreateApplication method :


  object obj = appLoader.CreateInstance(appLoader.EntryType);
            MethodInfo minfo = obj.GetType().GetMethod("Execute", BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance);

but i can not see Execute method in getMethos result, if QuickWatching,How to run Execute method in another currentdomain ? this Dlls are adding Process Dll's currewntdomain not wpf currentdomain.

enter image description here

Était-ce utile?

La solution

AppDomains are tricky to work with then dynamicly loading DLLs.

First; if you cannot see the method then verify you can get the type of the object. If you can see the type the object should be laoded into the current domain. Then you can itterate over the methods and inspect those to see if something is missmatched with the GetMethod call, wich could make it fail to show up. The CreateInstance flag might be the issue, I beleive that one is used for constructors.

Second; If you want things to run in a seperate appdomain (very important when you want to unload the dll later) then you have to build 3 things.

  1. create a set of classes that can handle all the loading and execution of the of the dynamic DLL.

  2. create an interface that allows your main code to talk to this set of classes; this iterface cannot return any types that came from the dynamic DLL; if it does you either laod that DLL or you crash. I recall marshal by ref may be needed, but uncertain, it has been a while.

  3. Attach event handlers to your main Appdomain that notify you when you load a DLL into that appdomain. This will be how you can debug if you are inadvertidly loading the DLL into your current AppDomain. http://msdn.microsoft.com/library/system.appdomain.assemblyload.aspx

Now you can create a new Appdomain, call your loading code in there and get results trough the interface. If you can complete the call and you had no load events trigered you got it working. you can dispaose of the AppDomain when you are done and the loaded DLL goes along with it.

Good luck, this was tricky to figure out and get working correctly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top