A project needs to call more than 100 existing DLL three first to solve a cross-domain problem for a type

Source: Internet
Author: User

Add similar operations to the same type, as shown in the following figure.Code:

   ///     <Summary>  
/// Really business operation.
/// </Summary>
[Serializable]
Public Class Servicewrapper1: marshalbyrefobject
{
Protected Servicewrapper1 ()
{
}

Public Virtual List < Opaoutput > Getuserdto (opainput input)
{

Console. writeline (appdomain. currentdomain. friendlyname + " Test2 " ); // Show current domain
Opatest Test = New Opatest ();
Return Test. getuserdto (input );
}
}
}

This is a simple opatest class used to call another DLL. It also tests whether the code runs in an independent domain.

The main reason that the constructor changes to protected is that you cannot directly create a wrap instance through the constructor and force the code to be called through defaultinstance (this is the application ).ProgramIsolated)

The reason for not changing to private is that inheritance is required later. If the constructor of the parent class is set to private, the subclass cannot be constructed.

But in the current Code, you should change protected to public to run normally,

To solve the cross-origin problem, the wrap class is inherited from marshalbyrefobject,

Opainput and opaoutput are input and output parameters respectively (defined in the existing DLL to facilitate development, temporarily defined as serializable, real serialization implementation problems will be solved later) The following are their definitions

 Namespace  Classlibrary2
{

Public Class Opatest
{
Public List < Opaoutput > Getuserdto (opainput input)
{
Return New List < Opaoutput > {
New Opaoutput () {date = Datetime. Now },
New Opaoutput () {date = Datetime. Now },
New Opaoutput () {date = Datetime. Now },
};
}
}
[Serializable]
Public Class Opaoutput
{
Public String Name { Get ; Set ;}
Public String Content;
Public Int Type;
Public Datetime Date { Get ; Set ;}
}
[Serializable]
Public Class Opainput
{
Public String Usercode { Get ; Set ;}
Public String Content;
Public Int ? Type;
Public Datetime startdate { Get ; Set ;}
Public Datetime enddate { Get ; Set ;}
}
}

To facilitate access to cross-origin DLL, the following types are implemented:

 [Serializable]
Public Class Basewrapper < T > : Financialbyrefobject
{
/// <Summary>
/// Define a application domain for every T
/// </Summary>
Private Static Appdomain _ currentdomian = Null ;

/// <Summary>
/// Static Constructor
/// </Summary>
Static Basewrapper ()
{
String Domainname = " Application Execution domain " + Typeof (T). fullname;
_ Currentdomian = Appdomain. createdomain (domainname, Null , Null );
}

/// <Summary>
/// Forbid call constructor directly.
/// </Summary>
Protected Basewrapper ()
{
}

/// <Summary>
/// Singleton instance.
/// </Summary>
Public Static T defaultinstance
{
Get
{
Return (T) _ currentdomian. createinstanceandunwrap ( Typeof (T). Assembly. fullname, Typeof (T). fullname );
}
}

}

Cross-origin calls are very simple, and an applicationdomain is maintained for a type T.

The call code is as follows:

 
Basewrapper<Servicewrapper1>. Defaultinstance. getuserdto (NewClasslibrary2.opainput () {content= "123"});

Compared with the original call code, the call code is very simple. The original call code is as follows:

 
Servicewrapper1 s= NewServicewrapper1 ();
S. getuserdto (NewClasslibrary2.opainput () {content= "123"});

The final running result is as follows:

Application Execution domain classlibrary1.servicewrapper1 Test2

This method is executed in the new application domain.

PS: the assumption is that the input and output are deleted and serializable. In fact, not all people declare code with serializable labels. We will solve this problem later.

so far, we have concentrated all the code that calls external DLL into some classes and isolated them from different application domains for execution.

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.