Download presentation
Presentation is loading. Please wait.
1
Carnegie Mellon University MSCF 1.NET Distributed Computing Notes from “Distributed.NET Programming in C#” by Tom Barnaby and “.NET Programming” by Pradeep Tapadiya
2
Carnegie Mellon University MSCF 2 Remote Objects We can’t call methods on remote objects directly. We call methods on remote objects through a remote reference to that object. Parameters to and from the remote method are “marshaled”
3
Carnegie Mellon University MSCF 3 Marshaling Classification Marshal-by-value Marshal-by-reference Nonremotable
4
Carnegie Mellon University MSCF 4 Marshal-By-Value Objects Objects marked with the custom attribute [serializable] are pass-by-value objects The entire object is copied into the remote domain Any local change to the object is not reflected in the remote domain
5
Carnegie Mellon University MSCF 5 Marshal-By-Reference Object MBR objects derive from MarshalByRefObject The client of such an object is actually making calls on a proxy object created by the runtime
6
Carnegie Mellon University MSCF 6 MBR Objects (1) using System; using System.Threading; public class Foo : MarshalByRefObject { public void DisplayDomainInfo() { String s = "Domain=" + AppDomain.CurrentDomain.FriendlyName + " Thread ID=" + AppDomain.GetCurrentThreadId() + " Context ID=" + Thread.CurrentContext.ContextID; Console.WriteLine(s); }
7
Carnegie Mellon University MSCF 7 public class MyApp { public static void Main() { Foo fDefault = new Foo(); // No proxy is used because we are fDefault.DisplayDomainInfo(); // not crossing domains } D:\..\46-690\MBRandMBV>mbvandmbr.exe Domain=mbvandmbr.exe Thread ID=2284 Context ID=0
8
Carnegie Mellon University MSCF 8 MBR Objects (2) public class MyApp { public static void Main() { Foo fDefault = new Foo(); fDefault.DisplayDomainInfo(); // force a call by remote reference // fNew points to a proxy AppDomain ad = AppDomain.CreateDomain("MyNewAppDomain"); Foo fNew = (Foo) ad.CreateInstanceAndUnwrap("mbvandmbr", "Foo"); fNew.DisplayDomainInfo(); } D:\..\46-690\MBRandMBV>mbvandmbr Domain=mbvandmbr.exe Thread ID=2096 Context ID=0 Domain=MyNewAppDomain Thread ID=2096 Context ID=0
9
Carnegie Mellon University MSCF 9 MBR Objects (3) using System; using System.Threading; [Serializable] // make Serializable and remove MarshalByReference public class Foo { public void DisplayDomainInfo() { String s = "Domain=" + AppDomain.CurrentDomain.FriendlyName + " Thread ID=" + AppDomain.GetCurrentThreadId() + " Context ID=" + Thread.CurrentContext.ContextID; Console.WriteLine(s); }
10
Carnegie Mellon University MSCF 10 public class MyApp { public static void Main() { Foo fDefault = new Foo(); fDefault.DisplayDomainInfo(); AppDomain ad = AppDomain.CreateDomain("MyNewAppDomain"); Foo fNew = (Foo) ad.CreateInstanceAndUnwrap("mbvandmbr", "Foo"); fNew.DisplayDomainInfo(); } D:\...\46-690\MBRandMBV>mbvandmbr Domain=mbvandmbr.exe Thread ID=588 Context ID=0
11
Carnegie Mellon University MSCF 11 Remoting Model REMOTINGREMOTING LAYERLAYER REMOTINGREMOTING LAYERLAYER Client Object Server Object Proxy Channel Namespaces: System.Runtime.Remoting.Channels.Tcp binary serialization System.Runtime.Remoting.Channels.Http uses Microsoft SOAP serialization Classes: TcpServerChannel, HttpServerChannel, TcpClientChannel, HttpClientChannel
12
Carnegie Mellon University MSCF 12 Remote Objects May Be Server-Activated Objects (The server controls the lifetime) Client-Activated Objects (The client controls the lifetime)
13
Carnegie Mellon University MSCF 13 Server Activated Objects Server activated objects – with a well-known name assigned on the server The server code -- Registers a server side channel -- Registers the name of the remote object -- Provides a mechanism to keep the server alive -- Creates the object on the first call to the object trough the client side proxy The Client code -- Registers a client side channel -- Create a proxy with Activator.GetObject passing a type and a location -- Make calls on the proxy as though they were calls on the remote object
14
Carnegie Mellon University MSCF 14 Single-Call and Singletons Server Activated objects are published as either single-call or singleton objects Single-call => on each call from the client the object is created and then torn down (WellKnownObjectMode.SingleCall) Singleton => only one object exists and is shared by callers (no synchronization) (WellKnownObjectMode.Singleton) Neither of these is appropriate for holding state
15
Carnegie Mellon University MSCF 15 Client Activated Objects The client requests that an instance be created on the server For each instance requested a proxy is returned to the client The client may store instance specific state in the object (through the proxy) The objects are garbage collected when their leases expire
16
Carnegie Mellon University MSCF 16 The Object To Be Served // A Remote Student Object using System; public class RemoteStudent : MarshalByRefObject { private int age; private String name; public int getAge() { return age; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public void setName(string name) { this.name = name; } }
17
Carnegie Mellon University MSCF 17 Client Code (along with Student.dll) using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System; using System.Runtime.Remoting.Activation; class MyClient { public static void Main() { ChannelServices.RegisterChannel(new TcpClientChannel()); RemoteStudent r = (RemoteStudent) Activator.CreateInstance( typeof(RemoteStudent),null, new object[] { new UrlAttribute( "tcp://localhost:6502") } );
18
Carnegie Mellon University MSCF 18 r.setName("Bob"); r.setAge(23); RemoteStudent s = (RemoteStudent) Activator.CreateInstance( typeof(RemoteStudent),null, new object[] { new UrlAttribute( "tcp://localhost:6502") } ); s.setName("Alice"); s.setAge(22); Console.WriteLine("Name: " + s.getName() + " age " + s.getAge()); Console.WriteLine("Name: " + r.getName() + " age " + r.getAge()); }
19
Carnegie Mellon University MSCF 19 Server Code (with Student.dll) using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System; class MyApp { public static void Main() { ChannelServices.RegisterChannel( new TcpServerChannel(6502)); RemotingConfiguration.RegisterActivatedServiceType( Type.GetType("RemoteStudent, Student")); Console.WriteLine("Press a key to exit server"); Console.Read(); }
20
Carnegie Mellon University MSCF 20 Output..46-690\RemoteObjects3\client>MyClient Name: Alice age 22 Name: Bob age 23
21
Carnegie Mellon University MSCF 21 Leases.NET uses a lease to manage the lifetime of remote (client activated) objects. A lease may be extended by the client When the lease expires the object is garbage collected and subsequent calls on the proxy will generate a RemotingException
22
Carnegie Mellon University MSCF 22 using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System; using System.Runtime.Remoting.Activation; using System.Threading; The Client Side Lets a Lease Expire
23
Carnegie Mellon University MSCF 23 class MyClient { public static void Main() { try { ChannelServices.RegisterChannel(new TcpClientChannel()); RemoteStudent r = (RemoteStudent) Activator.CreateInstance( typeof(RemoteStudent),null, new object[] { new UrlAttribute( "tcp://localhost:6502") } );
24
Carnegie Mellon University MSCF 24 r.setName("Bob"); r.setAge(23); Thread.Sleep(45000); // wait 45 seconds after creating Bob RemoteStudent s = (RemoteStudent) Activator.CreateInstance( typeof(RemoteStudent),null, new object[] { new UrlAttribute( "tcp://localhost:6502") } ); s.setName("Alice"); s.setAge(22); Console.WriteLine("Name: " + s.getName() + " age " + s.getAge()); Console.WriteLine("Name: " + r.getName() + " age " + r.getAge()); } catch(RemotingException e) { Console.WriteLine("Caught exception " + e); // Bob causes trouble }
25
Carnegie Mellon University MSCF 25 A Student With A Lease // A Remote Student Object using System; using System.Runtime.Remoting.Lifetime; public class RemoteStudent : MarshalByRefObject { private int age; private String name; public int getAge() { return age; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public void setName(string name) { this.name = name; }
26
Carnegie Mellon University MSCF 26 public override Object InitializeLifetimeService() { Console.WriteLine("Lease code called"); // whenever called call base class method ILease lease = (ILease) base.InitializeLifetimeService(); // if first call make some changes if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromSeconds(30); lease.RenewOnCallTime = TimeSpan.FromSeconds(10); } return lease; }
27
Carnegie Mellon University MSCF 27 Same Server Code using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System; class MyApp { public static void Main() { ChannelServices.RegisterChannel( new TcpServerChannel(6502)); RemotingConfiguration.RegisterActivatedServiceType( Type.GetType("RemoteStudent, Student")); Console.WriteLine("Press a key to exit server"); Console.Read(); }
28
Carnegie Mellon University MSCF 28..\46-690\RemoteObjects3\client>MyClient Name: Alice age 22 Caught exception System.Runtime.Remoting.RemotingException: No receiver registered D:..\46-690\RemoteObjects3\server>Server Press a key to exit server Lease code called Output
29
Carnegie Mellon University MSCF 29 The soapsuds tool Perhaps we do not want the client to have access to the Student code We only want the client to have access to meta information The soapsuds tool allows to download and then use meta information
30
Carnegie Mellon University MSCF 30 The server code using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System; class MyApp { public static void Main() { ChannelServices.RegisterChannel( new HttpServerChannel(6502));
31
Carnegie Mellon University MSCF 31 RemotingConfiguration.RegisterWellKnownServiceType( Type.GetType("RemoteStudent, Student"), "SomeStudent/Student.soap", WellKnownObjectMode.SingleCall); Console.WriteLine("Press a key to exit server"); Console.Read(); } // Compile the server code // csc -t:library Student.cs // csc -r:Student.dll Server.cs
32
Carnegie Mellon University MSCF 32 The Client Code using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System; class MyClient { public static void Main() { ChannelServices.RegisterChannel(new HttpClientChannel()); RemoteStudent r = (RemoteStudent) Activator.GetObject( typeof(RemoteStudent), "http://localhost:6502/SomeStudent/Student.SOAP");
33
Carnegie Mellon University MSCF 33 String name = r.getName(); int age = r.getAge(); Console.WriteLine("Student Name: " + name + " Age: " + age); }
34
Carnegie Mellon University MSCF 34 Run The Client But this time we do not have the Student.dll available. So, MyClient.csc will not compile. 1) Fetch the WSDL document from the server and generate the proxy class using soapsuds: soapsuds -url:http://localhost:6502/SomeStudent/Student.soap?wsdl - oa:Student.dll 2) Compile and run the client: csc -r:Student.dll MyClient.cs MyClient Student Name: Mike Age: 23
35
Carnegie Mellon University MSCF 35 Web Services “The internet is evolving from a collection of isolated web sites and applications into a general communication bus for distributed applications.” From page 247 of the course text
36
Carnegie Mellon University MSCF 36 ASP.NET Web Services 0) Check if IIS is running by attempting to visit http://localhost http://localhost 1) If it's not running click Start/Settings/Control Panel/Add Remove Programs/ Add Remove Windows Components and enable IIS. 2) If.NET was installed after IIS reconfigure IIS by running aspnet_regiis.exe /i from a command prompt.
37
Carnegie Mellon University MSCF 37 ASP.NET Web Services Suppose we want to provide a student name given a student ID
38
Carnegie Mellon University MSCF 38 ASP.NET Server Code // CoolService.asmx using System.Web.Services; using System.Collections; namespace Student { [WebService(Namespace="http://localhost/ACoolQueryService/")] public class QueryService : WebService { private static Hashtable nameValuePairs;
39
Carnegie Mellon University MSCF 39 static QueryService() { nameValuePairs = new Hashtable(); nameValuePairs.Add("12345","Moe"); nameValuePairs.Add("01234","Curly Joe"); nameValuePairs.Add("54321","Larry"); } [WebMethod] public string GetName(string id) { return (string)nameValuePairs[id]; } // We can’t compile this code with csc.
40
Carnegie Mellon University MSCF 40 Create a virtual directory under IIS Start Settings Control Panel Administrative Tools Select Internet Information Services Expand and select the default web site Click Action/New/Virtual Directory Provide a name (ACoolQueryService in this case) and browse to the directory holding the.asmx file Select everything but write
41
Carnegie Mellon University MSCF 41 Checking the service Visit the service with your browser http://localhost/ACoolQueryService/CoolService.asmx HTML is generated that allows you to test the service via standard HTTP
42
Carnegie Mellon University MSCF 42 Testing With HTTP Get Request GET /ACoolQueryService/CoolService.asmx/GetName?id=string HTTP/1.1 Host: localhost HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length Response string
43
Carnegie Mellon University MSCF 43 Testing with SOAP POST /ACoolQueryService/CoolService.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: length of document SOAPAction: "http://localhost/ACoolQueryService/GetName" <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
44
Carnegie Mellon University MSCF 44 string
45
Carnegie Mellon University MSCF 45 SOAP Response HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
46
Carnegie Mellon University MSCF 46 <GetNameResponse xmlns= "http://localhost/ACoolQueryService/"> string
47
Carnegie Mellon University MSCF 47 WSDL Structure message operation(message) type { interface Operation names (method names) Message types (parameters and return values) Interfaces (groups of methods) are called ports location of service
48
Carnegie Mellon University MSCF 48 Examine the WSDL Namespaces disambiguate.
49
Carnegie Mellon University MSCF 49 <s:schema elementFormDefault="qualified" targetNamespace="http://localhost/ACoolQueryService/"> message type string </GetName
50
Carnegie Mellon University MSCF 50 <s:element minOccurs="0" maxOccurs="1“ name="GetNameResult" type="s:string" /> Message type string Message type string
51
Carnegie Mellon University MSCF 51 Message GetNameSoapIn : GetName Message GetNameSoapOut : GetNameResponse Message GetNameHttpGetIn: { id : string}
52
Carnegie Mellon University MSCF 52 Message GetNameHttpGetOut: { body : s0:string} Message GetNameHttpPostIn: { id : string} Message GetNameHttpPostOut: { body: s0string}
53
Carnegie Mellon University MSCF 53 Interface QueryServiceSoap So:GetNameSoapOut GetName(s0:GetNameSoapIn) Interface QueryServiceHttpGet S0:GetNameHttpGetOut GetName(s0:GetNameHttpGetIn)
54
Carnegie Mellon University MSCF 54
55
Carnegie Mellon University MSCF 55 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> <soap:operation soapAction="http://localhost/ACoolQueryService/GetName" style="document" /> Bind QueryServiceSOAP to the interface s0:QueryServiceSoap and describe how the data will look on the wire
56
Carnegie Mellon University MSCF 56
57
Carnegie Mellon University MSCF 57
58
Carnegie Mellon University MSCF 58 Provide addresses for each interface.
59
Carnegie Mellon University MSCF 59 Remoting Vs. Web Services Characteristic.NET RemotingWeb Services Type system.NET SpecificXML Schema Client Platform.NETAny Message Format Binary, SOAPHTTP Post or Get Query strings or SOAP Proxy building tool Soapsuds.exeWsdl.exe MarshallingMBR or MBVMBV only InteroperabilityNoYes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.