Access services from any AppDomain using Jointcode.shuttle

Source: Internet
Author: User

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

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.