"Remoting-5 Code Implementation"

Source: Internet
Author: User

Service side

Class remotingservicehelper{private static string M_protocoltype;    private static String urlstring = "{0}://{1}:{2}/{3}"; <summary>//Register IChannel Channel///</summary>/<param name= "type" ></param>//< returns></returns> public static bool RegisterChannel (Channeltype type) {try {IC            Hannel Channel;            IDictionary tables=new Hashtable ();            tables["port"] = 9011;                     Switch (type) {case channeltype.httpchannel:tables["name"] = "HttpChannel"; Channel=new HttpChannel (Tables,new soapclientformattersinkprovider (), New soaps                    Erverformattersinkprovider ());                    M_protocoltype = "http";                Break                    Case channeltype.tcpchannel:tables["name"] = "TcpChannel"; Channel=new TcpChannel (tables,new binaryclientformattersinkProvider (), New BinaryServerFormatterSinkProvider ());                    M_protocoltype = "TCP";                Break                    Case channeltype.ipcchannl:tables["name"] = "IpcChannel"; Channel=new IpcChannel (Tables,new binaryclientformattersinkprovider (), New Binaryserverformattersin                    Kprovider ());                    M_protocoltype = "IPC";                Break                    Default:channel = null;            return false;            } channelservices.registerchannel (Channel,false);            Console.WriteLine ("Service Registration successful! Port number: {0}, channel type {1}", tables["Port"],type);        return true;          } catch (Exception e) {Console.WriteLine (e);            channel = null;        return false; }} public static bool Activatedserviceobject (Activatedtype type) {Remotingconfiguration.applicationname = "RemotIngservice ";        String Url=string.empty; String Objuri = type.        ToString ();                Switch (type) {case ActivatedType.Client:Console.WriteLine ("Activation mode: Client Activation");                On the server side, specify that the remote service object Remotingconfiguration.registeractivatedservicetype (typeof (Serviceobj)) be activated using client-activated mode; url = string.             Format (URLString, M_protocoltype, Ipaddress.loopback, 9011, remotingconfiguration.applicationname);            Break                Case ActivatedType.ServiceSingleCall:Console.WriteLine ("Activation mode: server-side Servicesinglecall activation"); RemotingConfiguration.RegisterWellKnownServiceType (typeof (Serviceobj), "Servicesinglecall",                WellKnownObjectMode.SingleCall); url = string. Format (urlstring+ "/{4}", M_protocoltype, Ipaddress.loopback, 9011, Remotingconfiguration.applicationnam                E, Objuri);            Break Case ActivatedType.ServiceSingleton:Console.                WriteLine ("Activation mode: server-side Servicesingleton activation"); RemotingConfiguration.RegisterWellKnownServiceType (typeof (Serviceobj), "Servicesingleton",                Wellknownobjectmode.singleton); url = string. Format (urlstring + "/{4}", M_protocoltype, Ipaddress.loopback, 9011, Remotingconfiguration.applicationna               Me, Objuri);            Break        Default:return false;        } Console.WriteLine ("The URL address of the remote object is: {0}", url);    return true; }}enum channeltype{TcpChannel, HttpChannel, Ipcchannl,}enum activatedtype{Client, Servicesingleton, Se  Rvicesinglecall,}

Class program   {       static void Main (string[] args)       {           Console.WriteLine ("Register IChannel Channel");           if (Remotingservicehelper.registerchannel (Channeltype.httpchannel))           {              //Console.WriteLine ("Channel registered successfully, channel type is {0} ", Channeltype.tcpchannel);               if (Remotingservicehelper.activatedserviceobject (Activatedtype.servicesingleton))               {                   Console.WriteLine (" Remote Service object activation succeeded! ");                   Console.readkey ();}}}          

Client:

"Note the reference to or detach from the service assembly"

Class Clientproxy {private String urlstring = "{0}://{1}:{2}/{3}";       Private Serviceobj _serviceobj;       private string M_protocol;       private string M_appname;       Private Activatedtype M_activatedtype;           Public Clientproxy (String protocol, String appName, Activatedtype activatedtype) {m_protocol = protocol;           M_appname = AppName;       M_activatedtype = Activatedtype;               Public Serviceobj Serviceobj {get {if (Activatorserviceobj ())               {return _serviceobj;                   } else {Console.WriteLine ("Failed to activate remote object");               return null;           }}} private bool Activatorserviceobj () {String url = geturlstring ();           String url = "";  try {//_serviceobj = (serviceobj) Activator.GetObject (typeof (Serviceobj), URL);             Switch (m_activatedtype) {case activatedtype.client:/                       /Activate the remote object first, then call the constructor to create the object Remotingconfiguration.registeractivatedclienttype (typeof (Serviceobj), URL);                       _serviceobj = new Serviceobj ();                   Break Case Activatedtype.servicesinglecall: _serviceobj = (serviceobj) remotingservices.connect (typeof (Serv                       iceobj), URL);                   Break Case Activatedtype.servicesingleton: _serviceobj = (serviceobj) Activator.GetObject (typeof (Serviceobj                       ), URL);               Break           } return true;               } catch (Exception e) {Console.WriteLine (e);           return false; }} private String Geturlstring () {string url = string.           Empty;      Switch (m_activatedtype) {         Case ActivatedType.Client:url = string.                   Format (URLString, M_protocol, Ipaddress.loopback, 9011, m_appname);               Break Case ActivatedType.ServiceSingleCall:url = string.                   Format (urlstring + "/{4}", M_protocol, Ipaddress.loopback, 9011, M_appname, "Servicesinglecall");               Break Case ActivatedType.ServiceSingleton:url = string.                   Format (urlstring + "/{4}", M_protocol, Ipaddress.loopback, 9011, M_appname, "Servicesingleton");           Break       } return URL; }} enum Channeltype {TcpChannel, HttpChannel, IPCCHANNL,} enum Activatedtype {Clie  NT, Servicesingleton, Servicesinglecall,}

 

Class Program {static void Main (string[] args) {Clientproxy proxy = new Clientproxy ("http", "Remotin          Gservice ", Activatedtype.servicesingleton); Serviceobj obj = proxy.          Serviceobj; if (obj! = null) {obj.              Printappdomain (); Obj.              Printappdomain (); Obj.              Showcount ("Zhang San"); Console.WriteLine (obj.              Getsum (10, 12)); Console.WriteLine (obj.              Getstrinfo ("Zhang San")); Console.WriteLine ("Count:{0}", obj.          Count);          } Console.WriteLine ();          Clientproxy Proxy2 = new Clientproxy ("http", "Remotingservice", Activatedtype.servicesingleton); Serviceobj obj2 = Proxy2.          Serviceobj; if (obj2! = null) {obj2.              Printappdomain (); Obj2.              Printappdomain (); Obj2.              Showcount ("Zhang San"); Console.WriteLine (obj.              Getsum (332, 12)); Console.WriteLine (obj.              Getstrinfo ("John Doe")); Console.WriteLine ("Count:{0}"Obj.          Count);      } console.readkey ();  }  }

Remote Service object

public class Serviceobj:marshalbyrefobject,iserviceobj {private int count = 0;            Public Serviceobj () {Console.WriteLine ("constructor of the service object");        Console.WriteLine ();        } public int Count {get {return Count;}            } public void Showcount (string name) {count++;        Console.WriteLine ("{1} for 0},count", Name,count);            } public void Printappdomain () {//AppDomain CurrentDomain = Appdomain.currentdomain;        AppDomain CurrentDomain = Thread.getdomain ();//Gets the application domain Console.WriteLine (currentdomain.friendlyname) where the current thread resides;            } public int Getsum (int a, int b) {Console.WriteLine ("A+b={0}", a+b);        return a + B;            public string Getstrinfo (String arg) {Console.WriteLine (ARG); return string.        Format ("Return a string plus parameter {0}", Arg); }    }

"Remoting-5 Code Implementation"

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.