Jointcode.shuttle is a service architecture for intra-process AppDomain communication (not supported across processes).
This article demonstrates how to use jointcode.shuttle with a simple example.
Jointcode.shuttle's release package
In Jointcode.shuttle's release package, contains two files: JointCode.Shuttle.dll and JointCode.Shuttle.Library.dll, where JointCode.Shuttle.dll is a library file written in a managed language, and JointCode.Shuttle.Library.dll is a component that is dependent on and written in an unmanaged language.
Preparatory work
To use Jointcode.shuttle, we first need to reference the JointCode.Shuttle.dll assembly in the project, and to copy the JointCode.Shuttle.Library.dll to the project after compiling JointCode.Shuttle.dll in the same folder (for example, assuming the project is compiled and JointCode.Shuttle.dll is copied to the c:/projects/sampleproject folder, you will need to manually JointCode.Shuttle.Library.dll copied to this folder).
Start coding
Jointcode.shuttle is interface-oriented programming, so we first need to write a service interface (also called a service contract) and apply the Serviceinterface property to it.
1 [Serviceinterface]2 Public Interface Isimpleservice 3 {4 string getoutput (string input); 5 }
View Code
Then write a service class that implements the contract and apply the ServiceClass property to it.
1[ServiceClass (typeof(isimpleservice), Lifetime =lifetimeenum.transient)]2 Public classSimpleservice:isimpleservice3 {4 Public stringGetOutput (stringinput)5 {6 return string. Format7("Simpleservice.getoutput Says:now, we are running in AppDomain: {0}, and the input passed from the caller is: {1}",8 AppDomain.CurrentDomain.FriendlyName, input);9 }Ten}View Code
Because we want to implement cross-AppDomain communication, here we need to write a class to start the remote service and have the class inherit from MarshalByRefObject.
1 Public classServiceprovider:marshalbyrefobject2 {3 //You must use a field here to hold a reference to the Shuttledomain instance, because if the instance is garbage collected,4 //then all services registered through the instance will also be logged off5 Shuttledomain _shuttledomain;6 7 Public voidregisterservices ()8 {9 //when registering a service group, you need to pass a Guid objectTen varGUID =NewGuid (); One_shuttledomain.registerservicegroup (refGUID, A NewServicetypepair (typeof(Isimpleservice),typeof(SimpleService))); - } - the Public voidCreateshuttledomain () - { - //Create a Shuttledomain -_shuttledomain = Shuttledomainhelper.create ("domain1","domain1"); + } - + Public voidDisposeshuttledomain () A { at _shuttledomain.dispose (); - } -}View Code
Now, you can start using Jointcode.shuttle. For the usage method, see the comment, code as follows:
1 class Program2 {3 Static voidMain (string[] args)4 {5 //to use Jointcode.shuttle, you must first initialize the Shuttledomain. 6 //This initialization is typically performed in the default AppDomain, but can also be performed in other AppDomain, all the same. 7 shuttledomain.initialize ();8 9 //creates a child AppDomain in the default AppDomain and creates a ServiceProvider object in the AppDomain. Ten varServiceend1domain = Appdomain.createdomain ("ServiceEndDomain1",NULL,NULL); One varServiceProvider =(serviceprovider) serviceend1domain.createinstanceandunwrap A(typeof(program). Assembly.fullname,"JoitCode.Shuttle.SimpleSample.ServiceProvider"); - - //in the child AppDomain, create a Shuttledomain instance. the Serviceprovider.createshuttledomain (); - - //in the child AppDomain, register the Isimpleservice service. - serviceprovider.registerservices (); + - //in the default AppDomain, create a shuttledomain. + //In fact, you need to have a Shuttledomain object in each AppDomain of your application. A //This object is used to communicate with Shuttledomain objects in other AppDomain. at varstr =Guid.NewGuid (). ToString (); - varShuttledomain =shuttledomainhelper.create (str, str); - - //In the default AppDomain, gets the service instance registered in the child AppDomain. - //the default lifetime for the current service instance is 1 minutes. The lifetime of the service instance is extended by 30 seconds each time the service method is invoked. - Isimpleservice Service; in if(Shuttledomain.trygetservice ( outservice)) - { to Try + { -Console.WriteLine ("Currently, we is running in AppDomain {0} before calling the remote service method ...", the AppDomain.CurrentDomain.FriendlyName); * $ Console.WriteLine ();Panax Notoginseng //Call the service method of the Isimpleservice service instance registered in the child AppDomain. - varOutput = service. GetOutput (" China"); the Console.WriteLine (output); + A Console.WriteLine (); theConsole.WriteLine ("Tests completed ..."); + } - Catch $ { $ Console.WriteLine (); -Console.WriteLine ("Failed to invoke the remote service method ..."); - } the } - ElseWuyi { the Console.WriteLine (); -Console.WriteLine ("Failed to create remote service instance ..."); Wu } - About //immediately releases the Isimpleservice service instance generated in the child AppDomain without waiting for its lifetime to end. $ //This is optional because the system will also automatically release the instance at the end of its lifetime, even if the Isimpleservice service instance is not released manually - //(if isimpleservice implements IDisposable, it also calls its Dispose method) - Shuttledomain.releaseservice (service); - A //in the child AppDomain, release the cached Shuttledomain instance. This will unregister all services registered through the instance (in this example, the Isimpleservice service). + Serviceprovider.disposeshuttledomain (); the - Console.read (); $ } the}
For full code, please click here to download.
A simple example of using Jointcode.shuttle for cross-AppDomain communication