Jointcode.shuttle is a service architecture for intra-process AppDomain communication (not supported across processes).
This article mainly describes how to access the services of any AppDomain through Jointcode.shuttle.
When we are making cross-AppDomain calls, we typically have classes (service classes) that need to operate across the AppDomain to inherit from MarshalByRefObject, and then create the target in the calling AppDomain (the parent AppDomain) AppDomain (Child AppDomain), and then directly create the required service object through a reference to the child AppDomain, and invoke the related methods of the service object. The code is like this:
1 namespaceJoitCode.Shuttle.SimpleSample2 {3 Public classMyservice:marshalbyrefobject4 {5 Public voidDo () {}6 }7 8 class Program9 {Ten Static voidMain (string[] args) One { A //Create a child AppDomain in the default AppDomain - varServicedomain = Appdomain.createdomain ("Servicedomain",NULL,NULL); - the varMyService =(myservice) servicedomain.createinstanceandunwrap -(typeof(MyService). Assembly.fullname, - "JoitCode.Shuttle.SimpleSample.MyService"); - + myservice.do (); - + Console.read (); A } at } -}
Obviously, in this way, we have no way to access the parent AppDomain directly from the child AppDomain. Of course, there are some workarounds for bidirectional communication, such as the following:
Public classMarshalbyrefcrossaccess1:marshalbyrefobject { Public voidRun () {Console.Write ("Now , we is running in AppDomain [{0}]!", AppDomain.CurrentDomain.FriendlyName); Console.WriteLine (); } } Public classMarshalbyrefcrossaccess2:marshalbyrefobject { Public voidRun (MarshalByRefCrossAccess1 Arg) {Console.WriteLine ("Currently, we is running in AppDomain [{0}]:", AppDomain.CurrentDomain.FriendlyName); Arg. Run (); } } classProgram {Static voidMain (string[] args) { varRemotedomain = Appdomain.createdomain (Guid.NewGuid (). ToString (),NULL,NULL); varAccess2 =(MARSHALBYREFCROSSACCESS2) remotedomain.createinstanceandunwrap (typeof(MARSHALBYREFCROSSACCESS2). Assembly.fullname,typeof(MARSHALBYREFCROSSACCESS2). FullName); varAccess1 =NewMarshalByRefCrossAccess1 (); Access2. Run (ACCESS1); Console.read (); } }
Although you can work with two-way communication, such approaches are always less natural. More importantly, this is two-way communication, the communication between the two sides hold each other, so we can achieve the purpose.
But what if the object we want to communicate with is not in our control at all (i.e. not holding its reference)?
For example, we created another two AppDomain B and C in an AppDomain a, and now what if AppDomain B is going to access AppDomain C?
Jointcode.shuttle is born to solve this problem.
We've written an example for this. The following is the part of the sample output:
If you are interested in sample source, please click here to download (example name: Shuttledomain any domain access).
Access services from any AppDomain using Jointcode.shuttle