How to return the original primary program domain after the Assembly is loaded and uninstalled through the appdomain of the application domain

Source: Internet
Author: User
Tags subdomain

Objective: To dynamically load the DLL, uninstall it at any time after execution, and replace the DLL to update the class in the DLL during running.

It is actually through the ApplicationProgramLoad and uninstall the assembly in the domain appdomain.

In this regard, Microsoft has an articleArticleComrade prattle also has an article on http://www.cnblogs.com/wayfarer/archive/2004/09/29/47896.html.

It is not difficult to implement it. My problem is:It can be loaded and uninstalled, but instances in different domains are called through a proxy (this is the basis for dynamic loading/uninstallation). It is okay to call subdomains from the primary domain, on the contrary, how does one allow classes in subdomains to access instances in the primary domain?

Let's talk about dynamic loading and uninstallation first. The previous two articles have made a wonderful discussion, just a few key points.

1: Create a subdomain in the primary domain. The loaded DLL is from another folder.

Appdomain svcdomain = NULL;
Try
{
Appdomainsetup setup = new appdomainsetup ();
Setup. applicationbase = appdomain. currentdomain. basedirectory;
Setup. privatebinpath = setup. applicationbase;
Setup. applicationname = asmname. fullname;
Setup. shadowcopydirectories = setup. applicationbase + @ "DLLs "; // DLL is not from the bin directory (it is strange why many people think that the DLL must be in the bin directory)
Setup. shadowcopyfiles = "true ";
Svcdomain = appdomain. createdomain (asmname. fullname, null, setup );
}
Catch (exception ex)
{
Log. Write ("test", "can not createdomain ")
Return;
}

// Get remote service Handler
Serviceproxy SVC = NULL;
Try
{// Key points
SVC = (serviceproxy) svcdomain. createinstancefromandunwrap (
Svcdomain. basedirectory + "\ Services. dll", "maservices. serviceproxy ");
}
Catch (exception ex)
{
Appdomain. Unload (svcdomain );
Log. Write ("test", "can not create proxy ")
Return;
}

2: proxy definition,It must inherit from marshalbyrefobject, And the passed data object must be serializable.

Public class serviceproxy: marshalbyrefobject, idisposable

3: Load DLL in proxy

Public bool loadservice (string assemblyname)
{
Assembly = NULL;
Try
{Assembly = assembly. loadfrom (assemblyname );
Iservice sev = (iservice) Assembly. createinstance (typename, true );

This. Services [assemblyname] = sev;
}
4: The proxy calls the specific class:Note: the specific call must not be in the primary domain.

Public icontext runservice (string servicename, icontext CTX)
{
Object o = This. Service [servicename];
(Iservice) O). runservice (CTX );
Return CTX;
}

Finally, you can directly call

Proxy. runservice (XX, XX)

That's all. In this way, the program can load a DLL at any time (any location), reflect it to get the class, run it, and this dll can be replaced at any time.

Key points:
The proxy should not return the reflected instance, and the transmitted data should be serializable. Be sure to set shadowcopydirectories.

Now let's get down to the truth and talk about the problem.

Assume that the primary domain M, subdomain C1, C2. After proxy, m can call instances in C1 and C2. In turn, how can C1 call instances in M? Or C1 calls an instance in C2?

TCP communication is a method, but the system consumes too much. If you use it frequently, it will be too bad.

Who has an offer?

Another problem: when I read the above article, I found that prxoy is an instance of the class to be proxy returned. I am skeptical about this.If the DLL is not in a directory, the instance cannot be accepted locally. Therefore, a local dll must exist to accept the returned results, and then deserialize the result based on the structure of the class defined in the local DLL. In this way, no matter how the proxy is used, the defaultappdomain actually references the specific classes and DLL, so the DLL is occupied by the current program and cannot be replaced in any case.

Welcome to the discussion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.