Blue Theme Orange Theme Green Theme Red Theme
 
Mindcracker MVP Summit 2012
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » VB.NET » .NET Remoting using VB.NET

.NET Remoting using VB.NET

This article gives brief description about Dot Net Remoting. .NET Remoting offers much more complex functionality, including support for passing objects by value or by reference, callbacks, and multiple-object activation and lifecycle management policies.

Page Views : 74918
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Introduction:

Over a period of time the perception of building applications is changing very rapidly whatever it may be either desktop applications, web applications or distributed applications. Now a days it has become the practice to build applications as set of components that are distributed across a network of machines and work together as if all the sets of components are available from the single machine. Traditionally, distributed application logic known for DCOM, CORBA or RMI, laid reliable and scalable platform to meet the growing needs of applications.

These component-based technologies work very well in an intranet environment. Only thing is we cannot use this technology over the internet because the technologies do not interoperate.

A pinch over Webservices Vs Remoting.

Browser-based Web applications, in contrast, are loosely coupled and remarkably interoperable. They communicate using HTTP to exchange MIME-typed data in a wide range of formats. Web services adapt the traditional Web programming model for use from all sorts of applications, not just browser based ones. They exchange SOAP messages using HTTP and other Internet protocols. Because web services rely on industry standards, including HTTP, XML, SOAP and WSD, to expose applications functionality on the Internet, they are independent of programming language, platform and device.

Thanks to Microsoft inventing of ASP.NET Web services and .NET Remoting. Web services infrastructure provides a simple API for Web services based on mapping SOAP messages to method invocations. This is achievable by providing a very simple programming model based on mapping SOAP message exchanges to individual method invocations. The clients of Web services do not have to know anything about the platform, object model, or programming language used to build them and vice versa (i.e. the services also unaware of the clients sending them messages). Only thing is both the parties should follow a protocol on the format of the SOAP message being produced and consumed.

Remoting Architecture:

.NET Remoting provides an infrastructure for distributed objects. It exposes full object semantics of .NET to remote processes using plumbing that is both flexible and extensible. .NET Remoting offers much more complex functionality, including support for passing objects by value or by reference, callbacks, and multiple-object activation and lifecycle management policies. In order to use .NET Remoting, a client needs to be built using .NET.

To put in simple words using object references to communicate between server objects and clients is the heart of Remoting. The Remoting architecture, however, provides the programmer with an even simpler procedure. If anyone configures the client properly, we need only to create a new instance of the remote object using new keyword, then client receives a reference to the server object and rest of the things are as usual ( like invoking methods) as the object were in your process though it is running on a separate computer.

Suppose we have an application running on one computer, and we want to use the functionality exposed by a type that is stored on another computer, below depicted is typical Remoting scenario:



(
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnetremotingarchitecture.asp)

Data Marshalling:

How the data is getting marshaled. We discuss the same in below:

Serialization and Metadata:

All distributed communication plumbing ultimately does two things:

1. marshals instances of programmatic data types into messages that can be sent across the network is accomplished using some form of serialization engine or marshaller.

2. provides a description of what those messages look like is achieved through some form of metadata.
For instance, for most DCOM interfaces, the serialization engine was the Type Library Marshaler and type libraries provides the metadata.

The key difference between ASP.NET web services and .NET Remoting is in how they serialize data into messages and the format they choose for metadata.

How .NET Remoting Marshals Data

.NET Remoting relies on the pluggable implementations of the IFormat interface used by the System.Runtime.Serialization engine to marshal data to and from messages. .NET Framework provides two standard formatters:-

1. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.
2. System.Runtime.Serialization.Formatters.Soap.SoapFormatter.

The BinaryFormatter and SoapFormatter as the name suggest marshal types in binary and SOAP format respectively. For metadata .NET Remoting relies on the CLR assemblies, which contain all the relevant information about the data types they implement and expose it via reflection. The reliance on the assemblies for metadata makes it easy to preserve the full runtime type-system reliability. As a result, when the .NET Remoting marshals data, it includes all of a class's public and private members.

However, as we mentioned above relying on runtime metadata for marshalling also limits the reach of a .NET Remoting system - as Client has to understand .NET constructs in order to communicate with a .NET Remoting endpoint.

Channels.

As an addition to the flavor, the .NET Remoting layer supports pluggable channels how messages are sent. There are two standard channels for the message transfer, independent of format (i.e. Binary format or Soap format) both TCP Channel and HTTP Channel provides an implementation for a sender-receiver channel that uses the HTTP protocol to transmit messages.

.NET Remoting Objects.

As mentioned above in the state management options, there are three types of objects that can be configured to serve as .NET remote objects. Choose the type of object depending on the requirement of the application.

  • Single Call : Single Call objects service one and only one request coming in. Single Call objects are useful in scenarios where the objects are required to do a limited amount of work. Single Call object are not required to store state information, in fact they cannot hold state information between method calls.

  • Singleton Objects : These objects service multiple clients and hence share data by storing state information between client invocations. They are useful in cases in which data needs to be shared explicitly between clients.

  • Client-Activated Objects : These objects are server-side objects that are activated upon request from the client. When the client submits a request for a server object using "new" operator, an activation request message is sent to the remote application. The server then creates an instance of the requested class and returns an ObjRef back to the client by using which proxy is then created. These objects can store state information between method calls for its specific client. Each invocation of "new" returns a proxy to an independent instance of the server type.

Life Time of Remote Object.

In typical, for the objects that have object references that are transported outside the application, a lease is created. The lease has a lease time; when the lease reaches zero it expires and the object is disconnected from the .NET Remoting Framework. Once all the references to the object within the AppDomain have been freed, the object will be collected when the next garbage collection occurs. The lease controls the lifetime of the object.

To sum up points:

  • .NET Remoting favors the runtime type system and provides a more complex programming model with much more limited reach.

  • .NET Remoting gives the flexibility to host remote objects in any type of application including a Windows Form, a managed Windows Service, a console application or the ASP.NET worker process. Both the channels (TCP and HTTP) provide communication between sending and receiving processes using sockets.

  • .NET Remoting infrastructure is extensible. It can be possible to filter inbound and outbound message, control aspects of type marshaling and metadata generation. It is possible to implement custom formatters and channels using .NET Remoting.

  • .NET Remoting, hosted in IIS with ASP.NET can leverage all the security features available to ASP.NET Web Services. If we use TCP or HTTP channel hosted in processes other than aspnet_wp.exe, we have to implement authentication, authorization and privacy mechanisms by our own.

  • .NET Remoting supports a range of state management options (depends on object lifetime scheme SingleCall or Singleton objects).

  • In terms of performance .NET Remoting provides the fastest communication when we use TCP channel and the binary formatter.

Building the sample application:

In the below example we consider an example, the remote object which exposes two methods adding and subtracting given two numbers.

Building an application that uses .NET Remoting to communicate across application domain boundaries is very straightforward:

1. You must have an implementation of a remotable type.
2. A listening or host application domain.
3. A client or calling application domain.
4. And you must configure the remoting system in each application domain to use remote activation for the remotable type.

The above process applies no matter how complex or simple the remoting scenario becomes.

We discuss each of the above in following:

  • Building Remotable Type: We discuss in brief about how to build the remotable type. To enable objects in other application domains to use an instance of the class, the class must inherit from MarshalByRefObjet. The following code example shows a simple object that can be created and invoked from objects executing in another application domain.

MathLibrary.cs.

Imports System
Public Class MathLibrary : Inherits
MarshalByRefObject
Private result As Integer

Public
Function AddTwoNumber(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
result = num1 + num2
Return
result
End
Function
Public Function SubtractTwoNumber(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
result = num1 - num2
Return
result
End FunctionEnd
Class

Store the above file as MathLibrary.cs in your own directory. Compile this file as dll from the command prompt as below:

csc /noconfig /t:library MathLibrary.cs.

Building a Host Application.

Our job is not over by simply creating MathLibrary. To create instances of this object remotely, you must build a host or listener application which does two things:

  • Choose and register a channel, which is an object that handles the networking protocols and serialization formats.

  • Register your type with the .NET Remoting system so that it can use your channel to listen for requests for your type.

Since remote configuration is done on a per-application-domain basis, the application domain must be running to listen for requests.

One important thing is that unlike COM, Remoting does not start the host or server application by its own.

The following code implements a simple MathLibrary host application domain that uses a configuration file.

Listener.cs.

Imports System
Imports
System.Runtime.Remoting
Public Class Listener
Public Shared Sub Main()
RemotingConfiguration.Configure("Listener.exe.config")
Console.WriteLine("Listening for requests. Press Enter to exit")
Console.ReadLine()
End
Sub
End
Class

Store the above code as Listener.cs in the same directory as where MathLibrary.dll is created. Compile Listener.cs with reference to MathLibrary.dll as below:

csc /noconfig /r:MathLibrary.dll Listener.cs.

Since the above code snippet uses Listener.exe.config file to listen to it's remotable type we need to create the Listener.exe.config file in the same directory where we created the Listener.exe.

Listener.exe.config:

<configuration
>
<system.runtime.remoting
>
<
application
>
<
service
>
<
wellknown mode="Singleton" type="MathLibrary, MathLibrary" objectUri="MathLibrary.rem"
/>
</
service
>
<
channels
>
<
channel ref="http" port
="8989"/>
</
channels
>
</
application
>
</
system.runtime.remoting
>
</
configuration
>

Building a Client Application:

Till now we have created MathLibrary, Host Application for the Remoting. Our application must register itself as a client for the remote object and then invoke it as residing in the client application domain. The .NET Remoting system intercepts the client calls, forward them to the remote object, and return the results to your client.

Client.cs.

Imports System
Imports
System.Runtime.Remoting
Public Class
Client
Public Shared Sub
Main()
RemotingConfiguration.Configure("Client.exe.config")
'INSTANT VB NOTE: The local variable lib was renamed since it is a keyword in VB:
Dim lib_Renamed As MathLibrary = New
MathLibrary
Console.WriteLine("Enter Number1:")
Dim num1 As String
= Console.ReadLine()
Console.WriteLine("Enter Number2:")
Dim num2 As String
= Console.ReadLine()
Console.WriteLine(lib_Renamed.AddTwoNumber(Convert.ToInt16(num1), Convert.ToInt16(num2)).ToString())
End
Sub
End
Class

Store the above code as Client.cs in the same directory as where Client.exe is created. Compile Client.cs with reference to MathLibrary.dll as below:

csc /noconfig /r:MathLibrary.dll Listener.cs.

Since the above code snippet uses Client.exe.config file to listen to its remotable type, we need to create the Client.exe.config file in the same directory where we created the Client.exe.

Client.exe.config.

<
configuration>
<
system.runtime.remoting
>
<
application
>
<
client
>
<
wellknown type="MathLibrary, MathLibrary" url="http://localhost:8989/MathLibrary.rem"
/>
</
client
>
</
application
>
</
system.runtime.remoting
>
</
configuration
>

Now every thing is ready to run your application:

From the command prompt(from the directory where you created the Client.exe and Listener.exe) type Listener.exe.



From the command prompt(from the directory where you created the Client.exe and Listener.exe) type Client.exe.

Make sure that while you are running your Client, Listener should be running to accept the requests from the client.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
ksasikumar
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
.net Remoting by Yogesh On July 18, 2006
More about .net Remoting
Reply | Email | Modify 
Re: .net Remoting by Divyesh On January 4, 2008
Hi, I am working with vb.net remoting. Now before calling server's method I want to sense/listen whether server is available/responding or not so it will not throw me an error. Help me out. thank you
Reply | Email | Modify 
getting remote system info by kishore On March 26, 2007
could you help me out by providing the code for developing an application for collecting information (ex. physical memory, Cpu Usage, Os version, Logical Drives) from remote servers using Asp.net and code behind is VB.NET help me on solving this problem? Thanks in advance... kishore
Reply | Email | Modify 
.net remoting by Chandrika On August 26, 2008
could you help me out by providing the code for developing an application for collecting information (ex. physical memory, Cpu Usage, Os version, Logical Drives) from remote servers using VB.NET help me on solving this problem? Thanks in advance... Chandrika
Reply | Email | Modify 
.NET Remoting Remotobject problem by addy On September 1, 2008
I have created application for Client, Server and Remotobject as mention in post, Now if I whish to call remoteobject first i had to add reference of remot object in Client application, so by creating instant of remot object I can easliy Invoke all Method and access member of the remot object then why I have to create and channel and connect to server to performing same solution
Reply | Email | Modify 
.NET Remoting Remotobject problem by addy On September 1, 2008
I have created application for Client, Server and Remotobject as mention in post, Now if I whish to call remoteobject first i had to add reference of remot object in Client application, so by creating instant of remot object I can easliy Invoke all Method and access member of the remot object then why I have to create and channel and connect to server to performing same solution
Reply | Email | Modify 
Client'sCPU - Memmory - Clock Speed etc by S On November 16, 2009

Hello - I am trying to write a code that will enable be to retrieve CPU serial, memmory Id and other Hardware information of the Client who access my Server over the Internet.

Can anyone help please ...

Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.