// request: deserialize string into the soapmessage soapformatter sf = new soapformatter(); sf.topobject = new soapmessage(); streamwriter rspsw = new streamwriter(new memorystream()); rspsw.write(request); rspsw.flush(); rspsw.basestream.position = 0; isoapmessage soapmsg = (isoapmessage)sf.deserialize(rspsw.basestream); rspsw.close();
// action: invoke the remote method object[] values = soapmsg.paramvalues; string[] stype = values[2].tostring().split(new char[]{','}); assembly asm = assembly.load(stype[1].trimstart(new char[]{' '})); type objecttype = asm.gettype(stype[0]); string objecturl = values[0].tostring(); realproxywrapper rpw = new realproxywrapper(objecttype, objecturl, soapmsg.paramvalues[4]); object ro = rpw.gettransparentproxy(); methodinfo mi = objecttype.getmethod(values[1].tostring()); object retval = mi.invoke(ro, values[3] as object[]); retmsg = rpw.returnmessage; } catch(exception ex) { retmsg = new returnmessage((ex.innerexception == null) ? ex : ex.innerexception, null); } finally { // response: serialize imessage into string stream rspstream = new memorystream(); soapformatter sf = new soapformatter(); remotingsurrogateselector rss = new remotingsurrogateselector(); rss.setrootobject(retmsg); sf.surrogateselector = rss; sf.assemblyformat = formatterassemblystyle.full; sf.typeformat = formattertypestyle.typesalways; sf.topobject = new soapmessage(); sf.serialize(rspstream, retmsg); rspstream.position = 0; streamreader sr = new streamreader(rspstream); response = sr.readtoend(); rspstream.close(); sr.close(); }
trace.writeline(response); return response; } the implementation of the steps are depended from the type of formatter such as soapformatter or binaryformatter. the first and last steps are straightforward using the remoting namespace classes. the second one (action) for the soapformatter message needed to create the following class to obtain imessage of the methodcall: public class realproxywrapper : realproxy { string _url; string _objecturi; imessagesink _messagesink; imessage _msgrsp; logicalcallcontext _lcc;
public imessage returnmessage { get { return _msgrsp; }} public realproxywrapper(type type, string url, object lcc) : base(type) { _url = url; _lcc = lcc as logicalcallcontext;
foreach(ichannel channel in channelservices.registeredchannels) { if(channel is ichannelsender) { ichannelsender channelsender = (ichannelsender)channel; _messagesink = channelsender.createmessagesink(_url, null, out _objecturi); if(_messagesink != null) break; } }
if(_messagesink == null) { throw new exception("a supported channel could not be found for url:"+ _url); } } public override imessage invoke(imessage msg) { msg.properties["__uri"] = _url; // endpoint msg.properties["__callcontext"] = _lcc; // caller's callcontext _msgrsp = _messagesink.syncprocessmessage(msg);
return _msgrsp; } }// realproxywrapper test i built the following package to test functionality of the webservicelistener and webservicechannellib assemblies. note that this package has only test purpose. here is what you downloaded it:
consoleclient, the test console program to invoke the call over internet - client machine
consoleserver, the host process of the myremoteobject - server machine
myremoteobject, the remote object - server machine
webservicechannellib, the custom client channel
webservicelistener, the web service listener - server machine
to recompile a package and its deploying in your environment follow these notes:
the folder webservicelistener has to be moved to the virtual directory (inetpub/wwwroot).
the myremoteobject assembly has to be install into the gac on the server machine
the webservicechannellib assembly has to be install into the gac on the client machine
(option) the msmqchannellib assembly [1] has to be install into the gac on the server machine
the solution can be tested also using the same machine (win2k/adv server)
use the echo webmethod on the test page of the webservicelistener to be sure that this service will be work
the test case is very simple. first step is to launch the consoleserver program. secondly open the consoleclient program and follow its prompt text. if everything is going right you will see a response from the remote object over internet. i am recommending to make a first test on the same machine and then deploying over internet. conclusion in this article has been shown one simple way how to implement a solution for remoting over internet. i used the power of .net technologies such as soap, remoting, reflection and web service. the advantage of this solution is a full transparency between the consumer and remote object. this logical connectivity can be mapped into the physical path using the config files, which they can be administratively changed.