Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Pass An Object to a Server By Reference?

This example demonstrates how to create an object that derives from MarshalByRefObject on the client that is then passed as a parameter to the server. The server calls a method on the remote object it receives. The first step is to create the object you need to pass.

		
using System;
namespace RemotingSamples {

    public class ForwardMe : MarshalByRefObject {

        public void CallMe(String text) {
            Console.WriteLine(text);
        }
    }
}
C#

Since the compiler requires the metadata for this object for both the client and server applications, you will compile the class and create a separate assembly for it.

		
csc /debug+ /target:library /out:share.dll share.cs
C#

On the server side, because we're passing a custom type (ForwardMe), a custom formatter for the TcpChannel sink chain with TypeFilterLevel set to Full must be used. If we were passing a primative type instead, TypeFilterLevel could be set to Low. See the Automatic Deserialization in .NET Remoting section of the .NET Framework documentation for more information about deserialization in .NET Remoting.

		
// creating a custom formatter for your TcpChannel sink chain.
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
C#

On the client side, you can create an instance of ForwardMe and pass it as a parameter when you call HelloMethod. The only catch in this example is the way you create the TCP channel - you have to give it a port number. This port number should be different from the one you are already using to communicate with the server. When the client channel is registered, the framework will start listening on this port for clients to connect, and establish a connection with the remote object. This allows bi-directional communication between the client and the server where the client forwards parameters to the server on one port and the server forwards parameters to the client using the other port.

When you run the example, the client should print the text "Regards from the server" to the console window.

VB Passing by Reference
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\HowTo\Samples\Remoting\byreference\]




Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.