Page view counter

These tutorials demonstrate selected features in ASP.NET version 2.0, but they are compatible with later versions of ASP.NET as well. For the current documentation, see the ASP.NET portal on the MSDN Web site.

 

 

   Welcome   |   ASP.NET   |   Web Services   |   Class Browser   
  |   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.

		
Imports System
Namespace Microsoft.Samples.Remoting.RemotingSamples

  Public Class ForwardMe : Inherits MarshalByRefObject

        Sub CallMe(String text)
            Console.WriteLine(text)
        End Sub
    End Class
End Namespace
VB

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.

		
vbc /debug+ /target:library /out:share.dll share.vb
VB

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.
Dim provider As New BinaryServerFormatterSinkProvider()
provider.TypeFilterLevel = TypeFilterLevel.Full
VB

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 D:\quickstarts.asp.net\QuickStartv20\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.