Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press
Buy directly from Paradoxal Press at $33.99 (Save 43%)
Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA
|
Listing 22-28 extracted from chapter
.NET Remoting
Listing 22-27< > Listing 22-29
This listing can be compiled with the command line: csc.exe /out:Client.exe /target:exe Example_22_28_to_rename_Client.cs /r:Interface.dll Errors: 1 Warnings: 0
Example_22_28_to_rename_Client.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Collections;
using RemotingInterfaces;
// You must use soapsuds.exe to le the client know
// about the 'Adder' class.
using RemotingServer;
public class CustomRealProxy : RealProxy {
// URI for server acticvated object.
String m_Uri;
// References the first message sink of the HttpSender channel.
IMessageSink m_MsgSink;
public CustomRealProxy( Type type, String uri,
IChannelSender channelSender) : base(type) {
m_Uri = uri;
string unused;
// Get the first message sink of 'channelSender'.
m_MsgSink = channelSender.CreateMessageSink(m_Uri, null, out unused);
}
// This method is called by the CLR before any client call.
public override IMessage Invoke( IMessage msgIn ) {
// The URI of the remote object must be stored in msgIn.
IDictionary d = msgIn.Properties;
d["__Uri"] = m_Uri;
// Display 'in parameters' stored in msgIn.
IMethodCallMessage msgCall = (IMethodCallMessage) msgIn;
Console.Write( "CustomRealProxy: Before calling:{0}(",
msgCall.MethodName );
for ( int i = 0; i < msgCall.InArgCount; i++ )
Console.Write(" {0}={1} ",msgCall.GetArgName(i),msgCall.GetArg(i));
Console.WriteLine(")");
// Perform the remote call!
IMethodReturnMessage msgOut =
(IMethodReturnMessage) m_MsgSink.SyncProcessMessage( msgIn );
// Display the value returned in msgOut.
Console.WriteLine( "CustomRealProxy: After calling:{0}() RetVal={1}",
msgCall.MethodName, msgOut.ReturnValue );
return msgOut;
}
}
namespace RemotingClient {
class Program {
static void Main() {
HttpChannel channel = new HttpChannel( 0 );
ChannelServices.RegisterChannel( channel, false );
// Build a custom real proxy.
// We initialize it with the object type, the URI of
// the WKO service and the channel it should use.
CustomRealProxy proxy = new CustomRealProxy(
typeof(Adder),
"http://localhost:65100/AddService",
(IChannelSender) channel );
IAdder obj = (IAdder) proxy.GetTransparentProxy();
// Here, we haven't contacted yet the server.
// This line triggers the first call to the server.
double d = obj.Add(3.0, 4.0);
}
}
}
Copyright Patrick Smacchia 2006 2007
|