Writing a well known WebService object is a simple as declaring a class and implementing the methods for the class.
Sample Code for HelloService
using System; namespace HelloService { // Well Known WebService object public class Hello : MarshalByRefObject { // Return the name prefixed with a string public String HelloMethod(String name) { Console.WriteLine("Hello.HelloMethod : {0}", name); return "Hi there " + name; } } }
The class must derive from System.MarshalByRefObject or any of its children, this allows NGWS runtime remoting to optimize for Remoting objects. See the Remoting Object Model section for details.
The Remote Application is published by hosting it in ASP+ or a host such as the sample MyHost. Once the Remote Application is published it can start processing messages.
Hosting the Remote Application in ASP+ involves creating a IIS root that points to the application directory and adding a “Remoting.cfg” configuration file to that directory. The Remoting configuration file is automatically loaded when the first message arrives in the application.
Remoting.cfg for the HelloService
Name#HelloService WellKnownObject#HelloService.Hello#HelloService#HelloService/Hello.soap#SingleCall
Name
The 1st line indicates the application name, The format is:
Name#Name of the Application
The application name is HelloService.
WellKnownObject
The 2nd line indicated the well known object that is published for this application.
The format is:
WellKnownObject#[FullTypeName]#[AssemblyName]#[ObjectURI]#[ObjectMode]
Entity | Description | Example |
---|---|---|
FulllTypeName | The full type name. | MyNamespace.MyType |
AssemblyName | The Assembly Name | MyNamespace |
ObjectURI | The Object URI on which to publish the object. | foo.soap |
ObjectMode | SingleCall
Singleton |
SingleCall |
See the Remoting Configuration topic for further details
MyHost is a sample managed console EXE that loads a Remoting Configuration file and is useful for debugging purposes since it can be started from the command line and run under a debugger. Hosting the Remote Application in MyHost involves creating a Remoting configuration file and specifying it as a command line argument to MyHost.
HelloService.cfg for the HelloService
Name#HelloService WellKnownObject#HelloService.Hello#HelloService#HelloService/Hello.soap#SingleCall
An example of starting MyHost and specifying the HelloService.cfg
MyHost -cfg HelloService.cfg
A proxy is used to communicate with the WK WebService in the Remote Application. In simple terms a Proxy is a local object, which is an image of a remote object. Calls on the proxy result in messages that are sent to the Remote Application. The proxy can be obtained in a number of ways that are shown below.
A proxy can be obtained by calling “new” on some class. To use “new”, a Remoting Configuration file must be loaded. The file can be loaded by calling RemotingServices.ConfigureRemoting. Once the Remoting configuration is loaded, future objects created for the class Hello will be created at the well known URL specified in the configuration file.
Sample Code using new and Remoting Configuration
using System; using System.Runtime.Remoting; using HelloService; public class SimpleHello { public static void Main(String[] args) { String name = "Bill"; RemotingServices.ConfigureRemoting("MyHello.cfg"); Hello hello = new Hello(); String result = hello.HelloMethod(name); Console.WriteLine("Hello.HelloMethod returned: " + result); } }
Contents of MyHello.cfg
Name#MyHello Assembly#HelloService#HelloService#Hello=HTTP://localhost:80/HelloService/Hello.soap RemoteApplication#HelloService#HTTP://localhost:80/HelloService Channel#System.Runtime.Remoting#System.Runtime.Remoting.Channels.HTTP.HTTPChannel
An application hosted in ASP+ can send messages to a Remote Application. The Remoting Configuration is loaded in the global.asax. That said one RemoteApplication hosted in ASP+ can send messages to another Remote Application hosted in ASP+.
Sample ASP+ global.asax
<%@ Import Namespace="System.Runtime.Remoting" %> <script runat="server" language="vb"> Sub Application_OnStart() RemotingServices.ConfigureRemoting HttpContext.Current.Server.MapPath("MyHello.cfg") End Sub </script>
Sample ASP+ Page using new and Remoting Configuration
<%@ Page Language="C#" %> <%@ Assembly Name="HelloService" %> <%@ Import Namespace="HelloService" %> <html> <head> <script runat="server"> String name; String reply; void name_change(Object Source, EventArgs E) { // new the class Hello hello = new Hello(); // Call method with Name from INPUT reply = hello.HelloMethod(Name.Value); } </script> </head> <body style="font: 10pt verdana"> <h3> MyHello - Hello.HelloMethod </h3> <form runat="server"> <font face="Verdana"> <b>Please Enter you name: </b> <INPUT id="Name" VALUE="" TYPE="TEXT" runat="server" OnServerChange = "name_change"><p> </font> </form> <h3> Reply : <%=reply%> </h3> </body> </html>
A proxy can be obtained by calling Activator.GetObject and passing a URL to the well known WebServices object. A Remoting Configuration file can optional be used to load the HTTP Channel. Two code samples are provided, one that uses configuration and one that loads the HTTPChannel using ChannelServices.
Sample Code using Activator.GetObject and Remoting Configuration
using System; using System.Runtime.Remoting; using HelloService; public class SimpleHello { public static void Main(String[] args) { String name = "Bill"; RemotingServices.ConfigureRemoting("MyHello.cfg"); Hello hello = (Hello)Activator.GetObject( typeof(Hello), "http://localhost:80/HelloService/Hello.soap"); String result = hello.HelloMethod(name); Console.WriteLine("Hello.HelloMethod returned: " + result); } }
Sample Code using Activator.GetObject and ChannelServices
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.HTTP; using HelloService; public class SimpleHello { public static void Main(String[] args) { String name = "Bill"; ChannelServices.RegisterChannel(new HTTPChannel()); Hello hello = (Hello)Activator.GetObject( typeof(Hello), "http://localhost:80/HelloService/Hello.soap"); String result = hello.HelloMethod(name); Console.WriteLine("Hello.HelloMethod returned: " + result); } }