.NET Remoting

18
Łukasz Zawadzki .NET Remoting .NET Remoting

description

.NET Remoting. Plan prezentacji. Co to jest .NET Remoting Definicja AppDomain Typy zdalnych obiektów Sposoby przekazywania obiektów(MBV i MBR) Formatery serializacji Dwa sposoby tworzenia aplikacji(konfigruacja z XML i konfiguracja z poziomu kodu) Zarządzanie stanem zdalnego obiektu. - PowerPoint PPT Presentation

Transcript of .NET Remoting

Page 1: .NET Remoting

Łukasz Zawadzki

.NET Remoting.NET Remoting

Page 2: .NET Remoting

Plan prezentacjiPlan prezentacji Co to jest .NET RemotingCo to jest .NET Remoting Definicja AppDomainDefinicja AppDomain Typy zdalnych obiektówTypy zdalnych obiektów Sposoby przekazywania obiektów(MBV i Sposoby przekazywania obiektów(MBV i

MBR)MBR) Formatery serializacjiFormatery serializacji Dwa sposoby tworzenia Dwa sposoby tworzenia

aplikacji(konfigruacja z XML i aplikacji(konfigruacja z XML i konfiguracja z poziomu kodu)konfiguracja z poziomu kodu)

Zarządzanie stanem zdalnego obiektuZarządzanie stanem zdalnego obiektu

Page 3: .NET Remoting

Co to jest .NET Co to jest .NET Remoting??Remoting??

Sposób zdalnego wywoływania Sposób zdalnego wywoływania metod i dostępu do obiektów metod i dostępu do obiektów znajdujących się w różnych znajdujących się w różnych AppDomain, procesach, AppDomain, procesach, komputerachkomputerach

Page 4: .NET Remoting

Co to jest AppDomain??Co to jest AppDomain??

Rodzaj abstrakcyjnego obiektu Rodzaj abstrakcyjnego obiektu reprezentującego domenę aplikacji reprezentującego domenę aplikacji (zbiór procesów aplikacji)(zbiór procesów aplikacji)

Page 5: .NET Remoting

Typy zdalnych obiektówTypy zdalnych obiektów

SinglecallSinglecall SingletonSingleton COA( Client Activated Object)COA( Client Activated Object)

Page 6: .NET Remoting

Sposoby przekazywania Sposoby przekazywania obiektówobiektów

MBV( Marshal by value)MBV( Marshal by value) MBR( Marshal by reference)MBR( Marshal by reference) serializableserializable

Page 7: .NET Remoting

Formatery serializacjiFormatery serializacji

Do czego służą formatery??Do czego służą formatery?? Interfejs IRemotingFormatterInterfejs IRemotingFormatter Dwa rodzaje formaterów : Dwa rodzaje formaterów :

SoapFormatter( oparty na Simple SoapFormatter( oparty na Simple Object Access Protocol) i Object Access Protocol) i BinaryFormatterBinaryFormatter

Page 8: .NET Remoting

KonfiguracjaKonfiguracja

Klasa obiektu zdalnego:Klasa obiektu zdalnego:namespace ServerClassnamespace ServerClass

{{ public class MainClass :  public class MainClass : System.MarshalByRefObjectSystem.MarshalByRefObject { {  public int add(int a, int b)  public int add(int a, int b)  {    {         return a+b; return a+b;   }  } } }

}}

Page 9: .NET Remoting

Konfiguracja XMLKonfiguracja XML Nazwa pliku konfiguracyjnego : „*.cfg”Nazwa pliku konfiguracyjnego : „*.cfg” Po stronie serwera :Po stronie serwera :Config.cfgConfig.cfg<configuration><configuration>

 <system.runtime.remoting> <system.runtime.remoting>    <application><application>      <service><service>        <wellknown mode="SingleCall" <wellknown mode="SingleCall"

objectUri="MyServer" objectUri="MyServer"             type="ServerClass.MainClass, type="ServerClass.MainClass, ServerClass„/>ServerClass„/>      </service></service>      <channels><channels>        <channel ref="tcp" port="8881"/><channel ref="tcp" port="8881"/>      </channels></channels>    </application></application> </system.runtime.remoting> </system.runtime.remoting>

</configuration></configuration>

Page 10: .NET Remoting

Konfiguracja XMLKonfiguracja XML Kod aplikacji serwera:Kod aplikacji serwera:public static void Main()public static void Main(){{

  try  try  {  {      RemotingConfiguration.Configure("C:\\      RemotingConfiguration.Configure("C:\\config.cfg");config.cfg");      Console.ReadLine();      Console.ReadLine();  }  }  catch(System.Runtime.Remoting.RemotingExceptio  catch(System.Runtime.Remoting.RemotingException e)n e)  {  {    Console.WriteLine(e.StackTrace);    Console.WriteLine(e.StackTrace);    Console.WriteLine("WYJATEK!!!");    Console.WriteLine("WYJATEK!!!");    Console.ReadLine();    Console.ReadLine();  }  }

}}

Page 11: .NET Remoting

Konfiguracja XMLKonfiguracja XML Po stronie klientaPo stronie klienta<configuration><configuration>

 <system.runtime.remoting> <system.runtime.remoting>    <application><application>      <channels><channels>        <channel ref="tcp"><channel ref="tcp">            <clientProviders><clientProviders>              <formatter <formatter ref="binary"/>ref="binary"/>            </clientProviders></clientProviders>        </channel></channel>      </channels></channels>      <client><client>  <wellknown  url="tcp://localhost:8881/MyServer"   type="S<wellknown  url="tcp://localhost:8881/MyServer"   type="ServerClass.MainClass,  ServerClass"/>erverClass.MainClass,  ServerClass"/>      </client></client>    </application></application> </system.runtime.remoting> </system.runtime.remoting>

</configuration></configuration>

Page 12: .NET Remoting

Konfiguracja XMLKonfiguracja XML Kod aplikacji klienckiej:Kod aplikacji klienckiej:static void Main(string[] args)static void Main(string[] args){{

              //konfiguracja klienta z pliku c:\client.cfg              //konfiguracja klienta z pliku c:\client.cfg              RemotingConfiguration.Configure("C:\\              RemotingConfiguration.Configure("C:\\client.cfg");client.cfg");    int a, b, c;    int a, b, c;              a=3;              a=3;    b=4;    b=4;              //pośrednik reprezentujący obiekt serwerowy              //pośrednik reprezentujący obiekt serwerowy    ServerClass.MainClass add=new     ServerClass.MainClass add=new ServerClass.MainClass();ServerClass.MainClass();    c=add.add(a, b);    c=add.add(a, b);    Console.WriteLine(c);    Console.WriteLine(c);

}}

Page 13: .NET Remoting

Konfiguracja z poziomu Konfiguracja z poziomu kodukodu

Serwer:Serwer:public static void Main()public static void Main(){{

  try  try  {  {

int port = 8881;int port = 8881;             TcpChannel chnl = new TcpChannel(port);TcpChannel chnl = new TcpChannel(port);

ChannelServices.RegisterChannel(chnl);ChannelServices.RegisterChannel(chnl);RemotingConfiguration.RegisterWellKnownServiceType(RemotingConfiguration.RegisterWellKnownServiceType(typeof(MainClass), „MyServer", typeof(MainClass), „MyServer",

WellKnownObjectMode.SingleCall);WellKnownObjectMode.SingleCall);              Console.ReadLine();Console.ReadLine();

  }  }  catch(System.Runtime.Remoting.RemotingException e)  catch(System.Runtime.Remoting.RemotingException e)  {  {         Console.WriteLine(e.StackTrace);Console.WriteLine(e.StackTrace);         Console.WriteLine("WYJATEK!!!");Console.WriteLine("WYJATEK!!!");         Console.ReadLine();Console.ReadLine();  }  }

}}

Page 14: .NET Remoting

Konfiguracja z poziomu Konfiguracja z poziomu kodukodu

Klient:Klient:static void Main(string[] args)static void Main(string[] args){{

    MainClass mk = (MainClass)     MainClass mk = (MainClass) Activator.GetObject( Activator.GetObject( typeof(MainClass),typeof(MainClass),

„tcp://host:port/MyServer"); ");„tcp://host:port/MyServer"); ");    int a, b, c;    int a, b, c;    a=3;    a=3;    b=4;    b=4;    c= mk.add(a, b);    c= mk.add(a, b);    Console.WriteLine(c);    Console.WriteLine(c);

}}

Page 15: .NET Remoting

Zarządzanie stanemZarządzanie stanem

InitialLeaseTime(czas przy InitialLeaseTime(czas przy inicjalizacji) inicjalizacji)

SponsorshipTimeout ( przedawnienie SponsorshipTimeout ( przedawnienie po nieudanym połączeniu)po nieudanym połączeniu)

RenewOnCallTime( o jaki czas RenewOnCallTime( o jaki czas przedłużamy życie obiektu przy przedłużamy życie obiektu przy każdym odwołaniu się do niego) każdym odwołaniu się do niego)

Page 16: .NET Remoting

PrzykładPrzykład

// metoda wywoływana na rzecz zdalnego obiektu do // metoda wywoływana na rzecz zdalnego obiektu do inicjalizacji charakterystyk czasowych jego życiainicjalizacji charakterystyk czasowych jego życia

public override object InitializeLifetimeService()public override object InitializeLifetimeService(){{

   ILease lease = (ILease)base.InitializeLifetimeService();   ILease lease = (ILease)base.InitializeLifetimeService();   if (lease.CurrentState == LeaseState.Initial)   if (lease.CurrentState == LeaseState.Initial)  {  {       lease.InitialLeaseTime = TimeSpan.FromMinutes(1);lease.InitialLeaseTime = TimeSpan.FromMinutes(1);       lease.SponsorshipTimeout        lease.SponsorshipTimeout =TimeSpan.FromMinutes(2);=TimeSpan.FromMinutes(2);       lease.RenewOnCallTime =        lease.RenewOnCallTime = TimeSpan.FromSeconds(2);TimeSpan.FromSeconds(2); } }

  return lease; return lease; }}

Page 17: .NET Remoting

Inicjalizacja samodzielnaInicjalizacja samodzielna

// kod już bezspośrednio po stronie // kod już bezspośrednio po stronie klientaklienta

ILease lease = ILease lease = (ILease)RemotingServices.GetLifetimeService(ZdalnyObiek(ILease)RemotingServices.GetLifetimeService(ZdalnyObiekt);t);

TimeSpan expireTime = TimeSpan expireTime = lease.Renew(TimeSpan.FromSeconds(60));lease.Renew(TimeSpan.FromSeconds(60));

Page 18: .NET Remoting

PodsumowaniePodsumowanie

Codeguru.plCodeguru.pl http://msdn.microsoft.com/library/http://msdn.microsoft.com/library/

default.asp?url=/library/en-us/cpguide/default.asp?url=/library/en-us/cpguide/html/cpconnetremotingoverview.asphtml/cpconnetremotingoverview.asp