Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   |   Class Browser
  |   I want my samples in...   

How Do I...? Common Tasks QuickStart Tutorial

Go To...

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 Microsoft.Samples.Remoting.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 primitive 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.

C# Passing by Reference
View Source

[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\HowTo\Samples\Remoting\byreference\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]




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


Hosted by MaximumASP | Found a broken link? | Contact Us | Terms and conditions | Privacy Policy | Advertise with us
� 2000 - 2008  Mindcracker LLC. All Rights Reserved