Remoting

Explain Remoting in c# with a sample code.

Answer this question

Remoting

  • ching

    I can't believe you actually replied to that post..

    I know I wouldn't have, atleast not that helpfull.

    All I'm gonna say is.

    www.c-sharpcorner.com



  • Panesso

    Did you mean to have a "please" at the end of that

    Anyway, excuse the formatting with the following:

    To call a function in a remote .net application

    Remotable Class:

    Create a library (DLL) project and add a class derived from MarshalByRefObject to use as the remotable object. (A form is indirectly derived from MarshalByRefObject, so is suitable for remoting.)

    We create a library so that the same remoted class can be referenced by both the client and the server.

    Let this class be called 'RemotedClass' for this discussion.

    Any of the public functions in RemotedClass can be called by a remote application.

    Server:

    The server must create an instance of RemotedClass (described above).

    Let the instance be called 'remoted_class'. Then you do this:

    ChannelServices.RegisterChannel( new TcpChannel( 1313 ) );

    // Choose an unused channel number.

    RemotingServices.Marshal( remoted_class, "connection_name" );

    // Connection name can contain spaces.

    Note where you specify a particular channel number and connection name above.

    Also note: You can use TcpChannel( 0 ) to auto-select an unused port. If you do, it seems hard to determine the port number. To get at the port number it picked, it looks like you have to call TcpChannel.GetUrlsForUri() and do something with the returned URL.

    NOTE: For local use, it is better to use IpcServerChannel(), which does not use a port:

    Private const string CONNECTION_NAME = "MyConnectionName";

    ChannelServices.RegisterChannel(new IpcServerChannel(CONNECTION_NAME), false);

    RemotingServices.Marshal(remoted_class, CONNECTION_NAME);

    Client:

    Access the remoted object like this:

    string remoted_url = "tcp://localhost:1313/connection name";

    remoted_object = (RemotedClass)RemotingServices.Connect
    ( typeof(RemotedClass), remoted_url );


    Then client can just call public functions via 'remoted_object'. (Only public functions can be called.)

    'RemotedClass' used by the client must be the same type as 'remoted_class' used by the server.

    If using IpcServerChannel() and IpcClientChannel():

    ChannelServices.RegisterChannel(new IpcClientChannel(CONNECTION_NAME, null), false);

    string uri = string.Format( "ipc://{0}/{1}", CONNECTION_NAME, CONNECTION_NAME );

    remoted_object = (RemotedClass)RemotingServices.Connect(typeof(RemotedClass), uri);

    NOTE:

    If the remoted class is a Control (or anything derived from a Control such as a Form), it should really handle InvokeRequired to ensure no funny bugs occur. See WinForms book page 398 for details. Also see the following example:

    Assume you want to call this function on the remoted form:

    public string RemoteFunction( string arg )

    In the remoted form class's implementation, you must declare a delegate like so:

    public delegate string RemoteFunctionDelegate( string arg );

    And then implement the RemoteFunction() function like so:

    public string RemoteFunction( string arg )
    {
    if ( this.InvokeRequired )
    {
    return (string) this.Invoke
    (
    new RemoteFunctionDelegate( RemoteFunction ),
    new object[] { arg }
    );
    }
    else
    {
    this.tb_receive.Text = arg ;
    return "This is the return value" ;
    }
    }


  • Remoting