.Net Remoting
description
Transcript of .Net Remoting
1Nordjyllands Erhvervakademi - 2009
.Net Remoting
• Configuration files– The .config file– Server side– Client side
• Encapsulate Activator.GetObject– A method
• Remote callback– Callback in general– How to do it
2Nordjyllands Erhvervakademi - 2009
The .config file
• Configuration files are widely used in .Net• A configuration file provides an easy way of changing
different kind of setting.• For example:
– Database connection strings– Authorization settings– Remoting settings– Application specific properties
• In Visual Studio: Add a new item using the “Application Configuration File” template
• Leave the name as app.config. It will be renamed to <assemply filename>.config.
• E.g.: MyProgram.exe.config
3Nordjyllands Erhvervakademi - 2009
Server Configuration file
• It is possible to do the most by configuration files.• Below is the configuration file for the server from the wko
example:
<configuration> <system.runtime.remoting> <application> <channels> <channel ref="http" port="1234" /> </channels> <service> <wellknown mode="Singleton"
type="Server.CustomerManager, Server" objectUri="CustomerManager.soap" />
</service> </application> </system.runtime.remoting></configuration>
<namespace>.<class>
Assembly
4Nordjyllands Erhvervakademi - 2009
Server.cs• Read the configuration file
static void Main(string[] args){
Console.WriteLine("Start service");String filename = @"..\..\server.exe.config";
RemotingConfiguration.Configure(filename,false);
Console.WriteLine("Service startet");Console.ReadLine();
}
5Nordjyllands Erhvervakademi - 2009
Config file with security setting<configuration> <system.runtime.remoting> <application> <channels> <channel ref="http" port="1234" > <serverProviders> <provider ref="wsdl" /> <formatter ref="soap" typeFilterLevel="Full" /> <formatter ref="binary" typeFilterLevel="Full" /> </serverProviders> </channel> </channels> <service> <wellknown mode="Singleton"
type="Server.CustomerManager, Server" objectUri="CustomerManager.soap" />
</service> </application> </system.runtime.remoting></configuration>
6Nordjyllands Erhvervakademi - 2009
client.exe.config
• Same information as in the Activator.GetObject
<configuration> <system.runtime.remoting> <application>
<client> <wellknown type="Server.ICustomerManager, Client" url="http://localhost:1234/CustomerManager.soap" /> </client>
</application> </system.runtime.remoting></configuration>
7Nordjyllands Erhvervakademi - 2009
client.cs
String filename = @"..\..\client.exe.config";RemotingConfiguration.Configure(filename);
ICustomerManager mgr = (ICustomerManager)Activator.GetObject(typeof(ICustomerManager), "http://localhost:1234/CustomerManager.soap");
Console.WriteLine("Reference to CustomerManager created"+mgr);Customer cust = mgr.GetCustomer(4711);
Here is a problem, which?
Information repeated in the
Activator.GetObject
8Nordjyllands Erhvervakademi - 2009
.Net Remoting
• Configuration files– The .config file– Server side– Client side
• Encapsulate Activator.GetObject– A method
• Remote callback– Callback in general– How to do it
9Nordjyllands Erhvervakademi - 2009
Better ways to use the configuration file on client side • Problems:
– Activator.GetObject needs a url and a port– using new demands knowledge of the concrete class
• Goal:– The client shall be able to instantiate remote objects from the
interface without knowing the concrete class.– Use configuration files
• Solution:– Encapsulate Activator
Source: Ingo Rammer
10Nordjyllands Erhvervakademi - 2009
Encapsulate Activator.GetObject
Steps:
1. Construct a RemoteHelper class with a method GetObject.
2. Is initialized by looking up in the RemotingConfiguration and store available remote types in a table.
3. GetObject takes a Type as parameter (can be a interface or a class)
4. GetObject returns a remote object by calling Activator.GetObject
The good news:
The construction can be used for general purpose.
It does not depend on a specific application
Source: Ingo Rammer
11Nordjyllands Erhvervakademi - 2009
RemotingHelper
using System;
using System.Collections;
using System.Runtime.Remoting;
class RemotingHelper
{
private static bool _isInit;
private static IDictionary _wellKnownTypes;
public static Object GetObject(Type type)
{
if (!_isInit) InitTypeCache();
WellKnownClientTypeEntry entr = (WellKnownClientTypeEntry)_wellKnownTypes[type];
if (entr == null)
{
throw new RemotingException("Type not found!");
}
return Activator.GetObject(entr.ObjectType, entr.ObjectUrl);
}
Continued……
Make table with remote types
Lookup on the type
Return the reference
12Nordjyllands Erhvervakademi - 2009
RemotingHelperGenerate table
public static void InitTypeCache()
{
_isInit = true;
_wellKnownTypes = new Hashtable();
foreach (WellKnownClientTypeEntry entr in
RemotingConfiguration.GetRegisteredWellKnownClientTypes())
{
if (entr.ObjectType == null)
{
throw new RemotingException("A configured type could not " +
"be found. Please check spelling");
}
_wellKnownTypes.Add(entr.ObjectType, entr);
}
}
}
13Nordjyllands Erhvervakademi - 2009
In the client:
static void Main(string[] args)
{
string path = @"../../Client.exe.config";
RemotingConfiguration.Configure(path);
IRemoteBank mgr = (IRemoteBank)RemotingHelper.GetObject(typeof(IRemoteBank));
…..
}
Here is both goals achieved.
14Nordjyllands Erhvervakademi - 2009
More on design...
• Classes in the data and business components should not inherit from MarshalByRefObject
• Use proxy pattern instead.• In other words: Construct a class (the proxy) with the same
interface as the real class. Redelegate calls to the real class
15Nordjyllands Erhvervakademi - 2009
.Net Remoting
• Configuration files– The .config file– Server side– Client side
• Encapsulate Activator.GetObject– A method
• Remote callback– Callback in general– How to do it
16Nordjyllands Erhvervakademi - 2009
Callback as in delegate or in observer pattern
• Callback means that the called method or service calls a method on the requesting object instead of returning a value.
• The advantage is that the program executing is not blocked, while waiting for a return.
• Event models are in general built on callback principles
• Here: The server is able to call a method on the client.
• The following example shows an remote implementation of observer pattern, where the connected clients are informed every time a new client connects
17Nordjyllands Erhvervakademi - 2009
Observer pattern Also called Observable in some litteratureMight also be a
interface
18Nordjyllands Erhvervakademi - 2009
In the example
• Observable is on the server.• Clients gets a reference to it by calling a get-method on a
factory class (Factory pattern)• Observers are placed on the clients and attached to the
Observable object.• The server calls back by calling the Notify method on the
Observers
19Nordjyllands Erhvervakademi - 2009
• The shared assembly
20Nordjyllands Erhvervakademi - 2009
The remote interface and the observer interfaces
public interface IObserver { void Notify(string msg); }
public interface IObservable { void Attach(IObserver obj); void Update(string msg); }
public interface IObserverableFactory { IObservable GetFactory(); }
21Nordjyllands Erhvervakademi - 2009
• The server
22Nordjyllands Erhvervakademi - 2009
The factory class
public class Factory : MarshalByRefObject, IObserverableFactory { IObservable _obsevable = null; public IObservable GetFactory() { if (_obsevable == null) _obsevable = new Observable(); return _obsevable; }}
23Nordjyllands Erhvervakademi - 2009
The observable class
public class Observable : MarshalByRefObject, IObservable { List<IObserver> _observers = new List<IObserver>();
public void Attach(IObserver obj) { _observers.Add(obj); }
public void Update(string msg) { foreach (IObserver o in _observers) o.Notify(msg); }}
24Nordjyllands Erhvervakademi - 2009
The config file
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.runtime.remoting> <application> <channels> <channel ref="http" port="1234"> <serverProviders> <provider ref="wsdl" /> <formatter ref="soap" typeFilterLevel="Full" /> </serverProviders> </channel> </channels> <service> <wellknown mode="Singleton” type="Server.Factory, Server"
objectUri="observer.soap" /> </service> </application> </system.runtime.remoting></configuration>
25Nordjyllands Erhvervakademi - 2009
• The client
26Nordjyllands Erhvervakademi - 2009
The observer class
[Serializable] public class Observer : MarshalByRefObject, Shared.IObserver { public void Notify(string msg) { Console.WriteLine(msg); } }
27Nordjyllands Erhvervakademi - 2009
The client class
[Serializable] public class Observer : MarshalByRefObject, Shared.IObserver { public void Notify(string msg) { Console.WriteLine(msg); } }
28Nordjyllands Erhvervakademi - 2009
The observer class
class Program { static void Main(string[] args) { RemotingConfiguration.Configure("Client.exe.config",false); IObserverableFactory serverObj = (IObserverableFactory) RemotingHelper.GetObject (typeof(IObserverableFactory));
IObservable observable = serverObj.GetFactory(); observable.Attach(new Observer()); observable.Update(String.Format("{0}: {1}", DateTime.Now, ”Client startet")); Console.ReadLine(); } }
29Nordjyllands Erhvervakademi - 2009
The config file
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.runtime.remoting> <application> <channels> <channel ref="http" port="0"> <serverProviders> <provider ref="wsdl" /> <formatter ref="soap" typeFilterLevel="Full" /> <formatter ref="binary" typeFilterLevel="Full" /> </serverProviders> </channel> </channels> <client> <wellknown type="Shared.IObserverableFactory, Shared" url="http://localhost:1234/observer.soap" /> </client> </application> </system.runtime.remoting></configuration>
30Nordjyllands Erhvervakademi - 2009
Run the example
• Start the server• The client cannot be started from Visual Studio for security
reasons• Therefore it has to be started from the bin directory.• Start the client twice. Look at the out on the first instance.
31Nordjyllands Erhvervakademi - 2009
Exercises:
• Exercise 1:– Change the server and the client in the remoting bank to use
configuration files• Exercise 2:
– Encapsulate the Activator.GetObject in the client• Exercise 3:
– Make a simple chat application by changing the callback example