Remoting and WebService
Remoting and Webservices
- What is an application domain?
Previously “PROCESS” where used as security boundaries. One process has its own virtual memory and does not over lap the other process virtual memory; due to this one process can not crash the other process. So any problem or error in one process does not affect the other process. In .NET they went one step ahead introducing application domains. In application domains multiple applications can run in same process with out influencing each other. If one of the application domains throws error it does not affect the other application domains. To invoke method in a object running in different application domain .NET remoting is used.
- What is .NET Remoting?
.NET remoting is replacement of DCOM. Using .NET remoting you can make remote object calls which lie in different Application Domains. As the remote objects run in different process client calling the remote object can not call it directly. So the client uses a proxy which looks like a real object.
When client wants to make method call on the remote object it uses proxy for it. These method calls are called as “Messages”. Messages are serialized using “formatter” class and sent to client “channel”. Client Channel communicates with Server Channel. Server Channel uses as formatter to deserialize the message and sends to the remote object.
- Which class does the remote object has to inherit?
All remote objects should inherit from System.MarshalbyRefObject.
- What are two different types of remote object creation mode in .NET?
There are two different ways in which object can be created using Remoting:
- SAO (Server Activated Objects) also called as Well-Known call mode.
- CAO (Client Activated Objects).
SAO has two modes “Single Call” and “Singleton”. With Single Call object the object is created with every method call thus making the object stateless. With Singleton the object is created only once and the object is shared with all clients.
CAO are stateful as compared to SAO. In CAO the creation request is sent from client side. Client holds a proxy to the server object created on server.
- Describe in detail Basic of SAO architecture of Remoting?
Remoting has at least three sections :
- Common Interface which will be shared between them.
- Server.
- Client.
Here is an explanation of the process:
- First important section is the common interface between Server and Client. For sample project interface
is very simple with only two methods : SetValue and GetValue.Public Interface InterFaceRemoting
Sub SetValue(ByVal value As String)
Function GetValue() As String
End Interface
- Second important section is the server. In this sample server is using HTTP channel and the server object is singleton.
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Imports InterFaceRemoting
Public Class RemotingServer Inherits MarshalByRefObject
Implements InterFaceRemoting.InterFaceRemoting
Private strData As String
Public Function GetValue() As String Implements
InterFaceRemoting.InterFaceRemoting.GetValue
Return strData
End Function
Sub New()
strData = "testing.."
End Sub
Public Sub SetValue(ByVal value As String) Implements
InterFaceRemoting.InterFaceRemoting.SetValue
strData = value
End Sub
End Class
Module ModuleRemotingStartUp
Sub Main()
Dim objHttpChannel As HttpChannel
Console.WriteLine("Server Started...")
objHttpChannel = New HttpChannel(1234)
ChannelServices.RegisterChannel(objHttpChannel)
RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingServer),
"RemoteObject", WellKnownObjectMode.Singleton)
Console.WriteLine("Server registered and listening waiting for clients...")
Console.ReadLine()
End Sub
End Module
In the code above, Channel object is created and registered. Server then hosts the object so that client can connect to it. This is the time when we specify what mode the server object will be created i.e. Singleton or SingleCall. This is done by the following below given code. Note in sample we are hosting the server object in singleton mode that means that the same object
will be shared between all clients. Also note the server object is implementing “InterFaceRemoting” and inheriting from “MarshalByRefObject”.
RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingServer),
"RemoteObject", WellKnownObjectMode.Singleton)
- In the last section the client will connect to this hosted remoting object.
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Imports InterFaceRemoting
Module ModuleStartClient
Sub Main()
Dim objHttpChannel As New HttpChannel
Dim objRemoting As InterFaceRemoting.InterFaceRemoting
ChannelServices.RegisterChannel(objHttpChannel)
objRemoting =
CType(Activator.GetObject(GetType(InterFaceRemoting.InterFaceRemoting),
"http://localhost:1234/RemoteObject"),
InterFaceRemoting.InterFaceRemoting)
Console.WriteLine("Referenced the main object.... Now displaying Data")
Console.WriteLine("Value on server :- " & objRemoting.GetValue.ToString())
Console.WriteLine("Press enter to Terminate")
Console.ReadLine()
End Sub
End Module
- Finally you need to run server and then client.
- What are the situations you will use singleton architecture in remoting ?
If all remoting clients have to share the same data singleton architecture will be used.
- What is fundamental of published or precreated objects in Remoting?
In scenarios of singleton or single call the objects are created dynamically. But in situations where you want to precreate object and publish it you will use published object scenarios.
Dim obj as new objRemote
obj.Initvalue = 100
RemotingServices.Marshal(obj,"RemoteObject")
As shown in above sample following changes will be needed on server side.
RemotingConfiguration.RegisterWellKnownServiceType is replaced by RemotingServices.Marshal(obj,”RemoteObject”) where “obj” is the precreated objected on the server whose value is initialized to 100.
- What are the ways in which client can create object on server in CAO model?
There are two ways by which you can create Client objects on remoting server :
- Activator.CreateInstance()
- By Keyword “New”.
- Are CAO stateful in nature?
Yes. In CAO remoting model client creates a instance on server and instance variable set by client on server can be retrieved again with correct value.
10. In CAO model for client objects to be created by “New” keyword what should we do?
Remoting Clients and Remoting Server can communicate because they share a common contract by implementing Shared Interface or Base Class (As seen in previous examples). But according to OOP’s concept we can not create a object of interface or Base Classes (Abstract Class). Shipping the server object to client is not a good design practice. In CAO model we can use SOAPSUDS utility to generate Metadata DLL from server which can be shipped to client, clients can then use this DLL for creating object on server. Run the SOAPSUDS utility from visual studio command prompt for syntax see below :
soapsuds -ia:RemotingServer -nowp -oa:ClientMetaData.dll
Where RemotingServer is your server class name.
ClientMetaData.dll is the DLL name by which you will want to create the metadll.
Server code will change as follows:
ChannelServices.RegisterChannel(objHttpChannel)
RemotingConfiguration.ApplicationName = "RemoteObject"
RemotingConfiguration.RegisterActivatedServiceType(GetType(InterFaceRemoting.InterFaceRemoting))
So we have to provide ApplicationName and register the object as ActivatedServiceType.
On client side we have to reference the generated ClientMetaData.dll from SOAPSUDS utility. Below are changes which are needed to be incorporated at the Remoting Client:
RemotingConfiguration.RegisterActivatedClientType(typeof(RemoteObject),"http://
localhost:1234/MyServer")
Dim objRemoteObject as new RemoteObject()
RemoteObject is class which is obtained from ClientMetaData.dll which we created using SOAPSUDS utility. Now you can reference the object as normal object.
11. Is it a good design practice to distribute the implementation to Remoting Client?
It’s never advisable to distribute complete implementation at client, due to following reasons:
- Any one can use ILDASM or similar utility and get access to your code.
- It’s a bad architecture move to have full implementation as client side as any changes in implementation on server side you have to redistribute it again.
So the best way is to have a interface or SOAPSUDS generated meta-data DLL at client side rather than having full implementation.
12. What are LeaseTime, SponsorshipTime, RenewonCallTime and LeaseManagerPollTime?
In normal .NET environment objects lifetime is managed by garbage collector. But in remoting environment remote clients can access objects which are out of control of garbage collector. Garbage collector boundary is limited to a single PC on which framework is running; any remote client across physical PC is out of control of GC (Garbage Collector).
This constraint of garbage collector leads to a new way of handling lifetime for remoting objects, by using concept called as “LeaseTime”. Every server side object is assigned by default a &qout;LeaseTime” of five minutes. This leasetime is decreased at certain intervals. Again for every method call a default of two minutes is assigned. When i say method call means every call made from client. This is called as “RenewalOnCallTime”.
Let’s put the whole thing in equation to make the concept more clear.
Total Remoting object life time = LeaseTime + (Number of method calls) X
(RenewalTime).
If we take NumberOfMethodCalls as one.
Then default Remote Object Life Time = 5 + (1) X 2 = 10 minutes (Everything is in
minutes)
When total object lifetime is reduced to zero, it queries the sponsor that should the object be destroyed. Sponsor is an object which decides should object Lifetime be renewed. So it queries any registered sponsors with the object, if does not find any then the object is marked for garbage collection. After this garbage collection has whole control on the object lifetime. If we do not foresee how long a object will be needed specify the “SponsorShipTimeOut” value. SponsorShipTimeOut is time unit a call to a sponsor is timed out. “LeaseManagerPollTime” defines the time the sponsor has to return a lease time extension.
13. Which config file has all the supported channels/protocol?
Machine.config file has all the supported channels and formatter supported by .NET remoting.Machine.config file can be found at “C:\WINDOWS\Microsoft.NET\Framework\vXXXXX\CONFIG” path. Find <system.runtime.remoting> element in the Machine.config file which has the channels and the formatters. Below is a figure shown which can give a clear idea of how the file looks like.
14. How can you specify remoting parameters using Config files?
Both remoting server and remoting client parameters can be provided through config files. Below is a sample of server config file which provides all remoting parameter values which we where providing through code.
<configuration>
<system.runtime.remoting>
<application name="Server">
<service>
<wellknown mode="SingleCall" type="Server.ClsServer, Server"
objectUri="RemoteObject" />
</service>
<channels>
<channel ref="tcp server" port="9000" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
Later this config file can be loaded using the following code.
RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ApplicationBase
& "Server.config")
Same way we also have client.config file for loading the client remoting parameters.
<configuration>
<system.runtime.remoting>
<application name="Client">
<client url="tcp://localhost:9000/RemoteObject">
<wellknown type="CommonInterface.Icommon, Icommon"
url = "tcp://localhost:9000/Server/RemoteObject"/>
</client>
<channels>
<channel ref="tcp client" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
client remoting can then load the configuration file by using :
Dim IobjCommon As CommonInterFace.Icommon
Dim StrData As String
Dim objServiceEntries As WellKnownClientTypeEntry()
RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ApplicationBase
& "Client.config")
objServiceEntries = RemotingConfiguration.GetRegisteredWellKnownClientTypes()
IobjCommon = Activator.GetObject(GetType(Icommon), objServiceEntries(0).ObjectUrl.ToString())
StrData = IobjCommon.GetValue()
Console.WriteLine(" Serve side Data is " & StrData)
Console.ReadLine()
15. Can Non-Default constructors be used with Single Call SAO?
Non-Default constructors cannot be used with single call objects as object is created with every method call, there is no way to define Non-default constructors in method calls. It’s possible to use Non-Default constructor with Client activated objects as both methods “NEW” keyword and “Activator.CreateInstance” provide a way to specify Non-Default constructors.
16. How can we call methods in remoting Asynchronously?
We can make Asynchronous method calls by using delegates.
17. What is Asynchronous One-Way Calls?
One-way calls are a different from asynchronous calls from execution angle that the .NET Framework does not guarantee their execution. In addition, the methods used in this kind of call cannot have return values or out parameters. One-way calls are defined by using [OneWay()] attribute in class.
18. What is marshalling and what are different kinds of marshalling?
Marshaling is used when an object is converted so that it can be sent across the network or across application domains. Unmarshaling creates an object from the marshaled data. There are two ways to do marshalling:
- Marshal-by-value (MBV): In this the object is serialized into the channel, and a copy of the object is created on the other side of the network. The object to marshal is stored into a stream, and the stream is used to build a copy of the object on the other side with the unmarshalling sequence.
- Marshaling-by-reference (MBR): Here it creates a proxy on the client that is used to communicate with the remote object. The marshaling sequence of a remote object creates an ObjRef instance that itself can be serialized across the network.
19. What is ObjRef object in remoting?
All Marshal() methods return ObjRef object.The ObjRef is serializable because it implements the interface ISerializable, and can be marshaled by value. The ObjRef knows about location of the remote object, host name, port number and object name.
20. What is a Web Service?
Web Services are business logic components which provide functionality via the Internet using standard protocols such as HTTP. Web Services uses Simple Object Access Protocol (SOAP) in order to expose the business functionality.SOAP defines a standardized format in XML which can be exchanged between two entities over standard protocols such as HTTP. SOAP is platform independent so the consumer of a Web Service is therefore completely shielded from any implementation details about the platform exposing the Web Service. For the consumer it is simply a black box of send and receive XML over HTTP. So any web service hosted on Windows can also be consumed by UNIX and LINUX platform.
21. What is UDDI?
Full form of UDDI is Universal Description, Discovery and Integration. It is a directory that can be used to publish and discover public Web Services. If you want to see more details you can visit the http://www.UDDI.org .
22. What is DISCO?
DISCO is the abbreviated form of Discovery. It is basically used to club or group common services together on a server and provides links to the schema documents of the services it describes may require.
23. What is WSDL?
Web Service Description Language (WSDL)is a W3C specification which defines XML grammar for describing Web Services.XML grammar describes details such as where we can find the Web Service (its URI), what are the methods and properties that service supports, data type support and supported protocols.
Clients can consume this WSDL and build proxy objects that clients use to communicate with the Web Services. Full WSDL
specification is available at http://www.w3.org/TR/wsdl.
24. What the different phase/steps of acquiring a proxy object in Webservice?
Following are the different steps needed to get a proxy object of a webservice at the client side :
- Client communicates to UDDI node for webservice either through browser or UDDI’s public web service.
- UDDI node responds with a list of webservices.
- Every service listed by webservice has a URI pointing to DISCO or WSDL document.
- After parsing the DISCO document, we follow the URI for the WSDL document related to the webservice which we need.
- Client then parses the WSDL document and builds a proxy object which can communicate with Webservice./li>
25. What is file extension of Webservices?
.ASMX is extension for Webservices.
26. Which attribute is used in order that the method can be used as WebService?
WebMethod attribute has to be specified in order that the method and property can be treated as WebService.
27. What are the steps to create a webservice and consume it?
In Visual Studio you can create a new project using template or just create asmx-file using any text editor. After webservice is implemented, you need either add a webreference to it in Visual Studio, or to create a proxy for the client manually using wsdl.exe utility and build a client program using created proxy class, so that using it client can consume the webservice
28. Do webservice have state?
No.
29. How can we maintain State in Webservices ?
Webservices as such do not have any mechanism by which they can maintain state. Webservices can access ASP.NET intrinsic objects like Session, application and so on if they inherit from “WebService” base class.
<%@ Webservice %>
Imports System.Web.Services
Public class TestWebServiceClass
Inherits WebService
<WebMethod> Public Sub SetSession(value As String)
session("Val") = Value
End Sub
...
'End Class
Comments
Post a Comment