Understanding .NET Remoting - Method 1 (contd.)
(Page 4 of 8 )
This is the listing for the ASP.NET page that consumes the Diner Web Service using the code-behind assembly that we just saw in the previous section:
<%@ Page language="C#" Codebehind="PizzaRemotingClientHTTP" & _
inherits="PizzaRemotingClientHTTP.RemoteClient" %>
.........
<asp:DataList runat="server" id="PizzaList"
RepeatColumns="2" RepeatDirection="Vertical" RepeatMode="Table" DataSource='<%# PizzaMenu %>'
Width="100%">
<property name="AlternatingItemStyle">
<asp:TableItemStyle BackColor="#f5deb3"/>
</property>
<template name="ItemTemplate">
<asp:Panel runat=server font-size="12pt" font-bold="true">
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</asp:Panel>
<asp:Label runat=server font-Size="8pt"
Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>'>
</asp:Label>
<asp:Label runat=server font-Size="10pt" Text="$" ForeColor="Blue">
</asp:Label>
<asp:Label runat=server font-Size="10pt" ForeColor="Blue"
Text='<%# DataBinder.Eval(Container.DataItem, "Cost") %>' >
</asp:Label>
</template>
</asp:DataList> Method 2: (Using a configuration file on the client to register the channel and specify the object's URN) Creating and registering the channel explicitly and then using Acivator.GetObject is just one way of invoking the remote object. You can also specify the channel and the URI endpoint of the remote object through a configuration file , and just instantiate the Diner object using new . The client application will use the information in the loaded configuration file to register the channel and activate the object at the URI endpoint specified.
The general format of the client configuration file is as follows:
Name#Name of the Application
Assembly#[AssemblyName]#[RemoteApplicationName]
#[FullTypeName]=[ObjectURI]
RemoteApplication#[RemoteApplicationName]
#[RemoteApplicationURI]
Channel#[ChannelAssemblyName]#[ChannelFullTypeName]#[Name=Value;Name=Value] So based on the above format, here's how your configuration file ( MyDinerClient.cfg ) would look like:
Name#MyDinerClientConfig
Assembly#MyDiner#MyDiner#MyDiner.Diner=
HTTP://bingo:80/MyDiner/Diner.soap
RemoteApplication#MyDiner#HTTP://bingo:80/MyDiner
Channel#System.Runtime.Remoting
#System.Runtime.Remoting.Channels.TCP.TCPChannel
Channel#System.Runtime.Remoting
#System.Runtime.Remoting.Channels.HTTP.HTTPChannel To load this configuration file, we have to use the RemotingServices.ConfigureRemoting method and pass it the name of the client configuration file to use. The information in the configuration file is used to register the channel and use the specified remote URI endpoint to activate the object. You must load the configuration file only once, typically when your application starts up. Shown below is the global.asax file for an ASP.NET consumer application that loads the configuration file.
<%@ Import Namespace="System.Runtime.Remoting" %>
<script runat="server" language="vb">
Sub Application_OnStart()
RemotingServices.ConfigureRemoting(HttpContext.Current.
Server.MapPath("MyDinerClient.cfg"))
End Sub
</script> So now, since we have all the details specified in the configuration file, we do not have to explicitly register the channel or provide details about the object's URN endpoint in our code. You would simply have to create the object using new and then call its methods as shown below:
// Create an instance of the remote object
Diner dinerObj = new Diner();
// Get Today's Menu from the remote Pizza Service
if(dinerObj != null)
m_listPizza = dinerObj.getPizzaMenu(); If there's an inkling of doubt whether your ASP.NET page is accessing the locally deployed assembly instead of the remote one, simply shut down the remote server or rename the assembly on the remote server and you will see that the client will choke.
Next: Hosting the Remote Object in a Managed .NET Application Executable >>
More ASP.NET Articles
More By Wrox Team