1 / 18

.NET Remoting

.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.

claude
Download Presentation

.NET Remoting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. .NET Remoting Łukasz Zawadzki

  2. 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

  3. Co to jest .NET Remoting?? • Sposób zdalnego wywoływania metod i dostępu do obiektów znajdujących się w różnych AppDomain, procesach, komputerach

  4. Co to jest AppDomain?? • Rodzaj abstrakcyjnego obiektu reprezentującego domenę aplikacji (zbiór procesów aplikacji)

  5. Typy zdalnych obiektów • Singlecall • Singleton • COA( Client Activated Object)

  6. Sposoby przekazywania obiektów • MBV( Marshal by value) • MBR( Marshal by reference) • serializable

  7. Formatery serializacji • Do czego służą formatery?? • Interfejs IRemotingFormatter • Dwa rodzaje formaterów : SoapFormatter( oparty na Simple Object Access Protocol) i BinaryFormatter

  8. Konfiguracja • Klasa obiektu zdalnego: namespace ServerClass { public class MainClass : System.MarshalByRefObject {  public int add(int a, int b)  {      return a+b;   } } }

  9. Konfiguracja XML • Nazwa pliku konfiguracyjnego : „*.cfg” • Po stronie serwera : Config.cfg <configuration> <system.runtime.remoting>  <application>   <service>    <wellknown mode="SingleCall" objectUri="MyServer"       type="ServerClass.MainClass, ServerClass„/>   </service>   <channels>    <channel ref="tcp" port="8881"/>   </channels>  </application> </system.runtime.remoting> </configuration>

  10. Konfiguracja XML • Kod aplikacji serwera: public static void Main() {  try  {      RemotingConfiguration.Configure("C:\\config.cfg");      Console.ReadLine();  }  catch(System.Runtime.Remoting.RemotingException e)  {    Console.WriteLine(e.StackTrace);    Console.WriteLine("WYJATEK!!!");    Console.ReadLine();  } }

  11. Konfiguracja XML • Po stronie klienta <configuration> <system.runtime.remoting>  <application>   <channels>    <channel ref="tcp">      <clientProviders>       <formatter ref="binary"/>      </clientProviders>    </channel>   </channels>   <client>  <wellknown  url="tcp://localhost:8881/MyServer"   type="ServerClass.MainClass,  ServerClass"/>   </client>  </application> </system.runtime.remoting> </configuration>

  12. Konfiguracja XML • Kod aplikacji klienckiej: static void Main(string[] args) {              //konfiguracja klienta z pliku c:\client.cfg              RemotingConfiguration.Configure("C:\\client.cfg");    int a, b, c;              a=3;    b=4;              //pośrednik reprezentujący obiekt serwerowy    ServerClass.MainClass add=new ServerClass.MainClass();    c=add.add(a, b);    Console.WriteLine(c); }

  13. Konfiguracja z poziomu kodu • Serwer: public static void Main() {  try  { int port = 8881;        TcpChannel chnl = new TcpChannel(port); ChannelServices.RegisterChannel(chnl); RemotingConfiguration.RegisterWellKnownServiceType( typeof(MainClass), „MyServer", WellKnownObjectMode.SingleCall);        Console.ReadLine();  }  catch(System.Runtime.Remoting.RemotingException e)  {     Console.WriteLine(e.StackTrace);     Console.WriteLine("WYJATEK!!!");     Console.ReadLine();  } }

  14. Konfiguracja z poziomu kodu • Klient: static void Main(string[] args) {    MainClass mk = (MainClass) Activator.GetObject( typeof(MainClass), „tcp://host:port/MyServer"); ");    int a, b, c;    a=3;    b=4;    c= mk.add(a, b);    Console.WriteLine(c); }

  15. Zarządzanie stanem • InitialLeaseTime(czas przy inicjalizacji) • SponsorshipTimeout ( przedawnienie po nieudanym połączeniu) • RenewOnCallTime( o jaki czas przedłużamy życie obiektu przy każdym odwołaniu się do niego)

  16. Przykład // metoda wywoływana na rzecz zdalnego obiektu do inicjalizacji charakterystyk czasowych jego życia public override object InitializeLifetimeService() {   ILease lease = (ILease)base.InitializeLifetimeService();   if (lease.CurrentState == LeaseState.Initial)  {    lease.InitialLeaseTime = TimeSpan.FromMinutes(1);       lease.SponsorshipTimeout =TimeSpan.FromMinutes(2);       lease.RenewOnCallTime = TimeSpan.FromSeconds(2); }  return lease; }

  17. Inicjalizacja samodzielna // kod już bezspośrednio po stronie klienta ILease lease = (ILease)RemotingServices.GetLifetimeService(ZdalnyObiekt); TimeSpan expireTime = lease.Renew(TimeSpan.FromSeconds(60));

  18. Podsumowanie • Codeguru.pl • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnetremotingoverview.asp

More Related