The information in this article help to:
|
What is .NET Remoting?
Netremoting replaces DCOM. Web Services that uses remoting can run in anyApplication type i.e. Console Application, Windows Form Applications,Window Services etc. In CLR Object Remoting we can call objectsacross network. |
.NET Remoting Architecture?
|
How does .NET Remoting work? .NET remoting involves sending messages along channels. Two of thestandard channels are HTTP and TCP. TCP is intended for LANs only -HTTP can be used for LANs or WANs (internet). Support is provided for multiple message serializarion formats.Examples are SOAP (XML-based) and binary. By default, the HTTP channeluses SOAP (via the .NET runtime Serialization SOAP Formatter), and theTCP channel uses binary (via the .NET runtime Serialization BinaryFormatter). But either channel can use either serialization format. There are a number of styles of remote access: SingleCall: Eachincoming request from a client is serviced by a new object. The objectis thrown away when the request has finished. This (essentiallystateless) model can be made stateful in the ASP.NET environment byusing the ASP.NET state service to store application or session state. Singleton: All incoming requests from clients are processed by a single server object. Client-activated object: Thisis the old stateful (D)COM model whereby the client receives areference to the remote object and holds that reference (thus keepingthe remote object alive) until it is finished with it. |
.NET Remoting Specific Advantage? |
Lease-Based Lifetime : Distributed garbage collection of objects is managed by a system called 'leased based lifetime'. Each object has a lease time, and when that time expires the object is disconnected from the .NET runtime remoting infrastructure. Net Remoting takes a Lease-base Lifetime of the object that is scaleable Call Context : Distributed Identities : |
Advantage over Web Services? |
Advantage over COM/DCOM? 1.It does not have extra interface language (IDL) 2.It Works using purely managed code 3.It's using Common Type System.. No Safearrays etc |
Disadvantages 1.It is not an open standard like web services. 2.It is not as widespread and established ad DCOM. 3.Less support for transactions,load balancing compared with DCOM. |
Now, Let's have look at remote object, client and server interactions. |
Remote Objects |
Aremote object is implemented in a class that derives fromSystem.MarshalByRefObject. A client doesn't call the methods directly; instead a proxy object is used to invoke methods on the remote object.Every public method that we define in the remote object class isavailable to be called from clients. |
To create MyRemoteObject.dll Take .NET IDE New Project->YourName.Samples(for example, MohamedAshraf.Samples(Type Class Library).
By Default.,Class1.cs will come. Change that file name MyRemoteObject.cs. Copy and paste this file there. |
MyRemoteObject.cs |
using System; using System.Runtime.Remoting.Lifetime; namespace MohamedAshraf.Samples { public class MyRemoteObject:System.MarshalByRefObject { public MyRemoteObject() { Console.WriteLine("MyRemoteObject Constructor Called"); } public override object InitializeLifetimeService() { return null; } public string Hello() { return "Hello, Welcome to .Net Remoting !"; } } } |
Build the assembly MyRemoteObject from the class MyRemoteObject |
Server |
The remote object needs a server process where it will be instantiated. This server has to create a channel and put it into listening mode so that clients can connect to this channel. For creating server, Take .NET IDE-> New Project named "Simple Server".This is console application. |
Server Configuration File |
InConfiguration file channel, endpoint, name etc. can be changed withouta recompile. When the client connects to the remote object it needs toknow the URI of the object, i.e. the name of the host where the remoteobject is running, the protocol and port number to connect to, the nameof the server, and the name of the remote object. With the exception ofthe host name we have to specify all these items in the configurationfile. tcp://localhost:9000/SimpleServer/MyRemoteObject In SimpleServer.exe.config All of the remoting configurations must be added as child elements to <system.runtime.remoting>. The <application> element specifies the name of the server withthe name attribute. The application offers a service and requires theconfiguration of channels for the service. Wehave the <service> and <channel>elements. The service thatis offered from the application must be listed as a child of<service>. This is the remote object itself. The remote object is specified with the <wellknown> element. Forinstantiating the object the framework requires the name of theassembly to know where the type of this object can be loaded from. Wecan set this information with the XML attributetype.type="MohamedAshraf.Samples. MyRemoteObject,MohamedAshraf.Samples" defines that the type of the remote object isMyRemoteObject in the namespace SimpleServer, and it can be found inthe assembly MyRemoteObject. The mode attribute is set to SingleCall.
|
How to create app.config |
|
app.config file <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.runtime.remoting> <application> <channels> <channel ref="tcp" port="8000" /> </channels> <service> <wellknown mode="Singleton" type="MohamedAshraf.Samples.MyRemoteObject,MohamedAshraf.Samples" objectUri="MyRemoteObject" /> </service> </application> </system.runtime.remoting> </configuration> |
The Server is implemented in the console application. RemotingConfiguration.Configure() reads the configuration file to configure SimpleServer.exe.config to configure and activate the channels. We have to keep the process alive. |
Using Console Application as Server |
SimpleServer.cs
using System; } |
Using Window Services as Server
span> For creating server, Take .NET IDE-> New Project named "RemotingServerWebService". Type windows Service. |
SimpleServer.cs
using System; // The main entry point for the process /// <summary> /// <summary> /// <summary> /// <summary> |
Project Installer |
With the installer class, it's possible to build transaction based installations. With a transaction based installation it's possible to go back to the previous state if the installation fails. It has Install(), Commit(), Rollback() and Uninstall() methods. |
How to create installer class. Right click " RemotingServerWebService" project add new item->Installer Class and copy the following lines. |
ProjectInstaller.cs |
using System; using System.Collections; using System.ComponentModel; using System.Configuration.Install; namespace RemotingServerWebService { [RunInstaller(true)] public class ProjectInstaller : System.Configuration.Install.Installer { private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; private System.ServiceProcess.ServiceInstaller serviceInstaller1; private System.ComponentModel.Container components = null; public ProjectInstaller() { InitializeComponent(); } #region Component Designer generated code this.serviceProcessInstaller1, this.serviceInstaller1}); Build it.It will create RemotingServerWebService.exe |
How to run window Service?
I. Build it. It will create RemotingServerWebService.exe II. Install the Window Service using InstallUtil.exe installutil RemotingServerWebService.exe Installutil.exeAllows you to install and uninstall server resources by executing theinstaller components of a specified assembly. II.Check Our Window services in stalled properly. Take MyComputer -> Manage -> Services and Applications -> Services . Here you can see the service name "MohamedAshrafRemotingService". Now the window service is running correctly.. II. Uninstall the window service installutill /u RemotingServerWebService.exe. Now, Let's have a look at how to to create a client |
Client
For cre ating client, Take .NET IDE-> New Project named "SimpleClient".This is console application. |
Client Configuration File The client configuration file SimpleClient.exe.config uses the XML<client> element to specify the URL of the server usingprotocol://hostname:port/application. We use tcp as the protocol, andthe server runs on localhost with the port number 9000. The applicationname of the server is defined with the name attribute of the<application> element in the server configuration file. The<wellknown> element specifies the remote object we want toaccess.
|
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.runtime.remoting> <application name = "SimpleClient"> <client> <wellknown type="MohamedAshraf.Samples.MyRemoteObject,MohamedAshraf.Samples" url="tcp://localhost:8000/MyRemoteObject"/> </client> <channels> <channel ref="tcp" port="0"/> </channels> </application> </system.runtime.remoting> </configuration> |
Client Application Activate the client channel by calling RemotingConfiguration.Configure(). Using configuration files we can simply use new to create the remote object. Next we call the method Hello() of this object. |
Using Console Application as Client |
SimpleClient.cs
using System; Build it you will get SimpleClient.exe |
Using Web Application as Client we can embed config file with webconfig file |
Using Web Application as Client
For creating web application as client, Take .NET IDE-> New Project named "RemotingWebClient" Type ASP.NET Web application. Take web.config and replace this file. |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation <customErrors <trace <sessionState <globalization </system.web> |
RemotingWebClient.cs |
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Runtime.Remoting; using MohamedAshraf.Samples; namespace RemotingWebClient } #region Web Form Designer generated code /// <summary> |
↧
How to Use .Net Remoting Using C#
↧