31
1 Nordjyllands 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

Net Remoting

  • Upload
    kele

  • View
    19

  • Download
    0

Embed Size (px)

DESCRIPTION

.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. The .config file. Configuration files are widely used in .Net - PowerPoint PPT Presentation

Citation preview

Page 1: 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

Page 2: Net Remoting

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

Page 3: Net Remoting

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

Page 4: Net Remoting

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();

}

Page 5: Net Remoting

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>

Page 6: Net Remoting

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>

Page 7: Net Remoting

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

Page 8: Net Remoting

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

Page 9: Net Remoting

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

Page 10: Net Remoting

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

Page 11: Net Remoting

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

Page 12: Net Remoting

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);

}

}

}

Page 13: Net Remoting

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.

Page 14: Net Remoting

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

Page 15: Net Remoting

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

Page 16: Net Remoting

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

Page 17: Net Remoting

17Nordjyllands Erhvervakademi - 2009

Observer pattern Also called Observable in some litteratureMight also be a

interface

Page 18: Net Remoting

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

Page 19: Net Remoting

19Nordjyllands Erhvervakademi - 2009

• The shared assembly

Page 20: Net Remoting

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(); }

Page 21: Net Remoting

21Nordjyllands Erhvervakademi - 2009

• The server

Page 22: Net Remoting

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; }}

Page 23: Net Remoting

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); }}

Page 24: Net Remoting

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>

Page 25: Net Remoting

25Nordjyllands Erhvervakademi - 2009

• The client

Page 26: Net Remoting

26Nordjyllands Erhvervakademi - 2009

The observer class

[Serializable] public class Observer : MarshalByRefObject, Shared.IObserver { public void Notify(string msg) { Console.WriteLine(msg); } }

Page 27: Net Remoting

27Nordjyllands Erhvervakademi - 2009

The client class

[Serializable] public class Observer : MarshalByRefObject, Shared.IObserver { public void Notify(string msg) { Console.WriteLine(msg); } }

Page 28: Net Remoting

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(); } }

Page 29: Net Remoting

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>

Page 30: Net Remoting

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.

Page 31: Net Remoting

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