国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Using Web Services for Remoting over the Internet.

2024-07-21 02:21:44
字體:
供稿:網(wǎng)友
  • decoding and de-serializing of the request message
  • invoking the remote method
  • encoding and serialization of the response message

[webmethod]
public string syncprocessmessage(string request)
{
   // request: decoding and deserializing
   byte[] reqbytearray = convert.frombase64string(request);
   memorystream reqstream = new memorystream();
   reqstream.write(reqbytearray, 0, reqbytearray.length);
   reqstream.position = 0;
   binaryformatter bf = new binaryformatter();
   imessage reqmsg = (imessage)bf.deserialize(reqstream);
   reqmsg.properties["__uri"] = reqmsg.properties["__uri2"]; // work around!!
   reqstream.close();

   // action: invoke the remote method
   string[] stype = reqmsg.properties["__typename"].tostring().split(new char[]{','}); // split typename
   assembly asm = assembly.load(stype[1].trimstart(new char[]{' '})); // load type of the remote object
   type objecttype = asm.gettype(stype[0]);                           // type
   string objecturl = reqmsg.properties["__uri"].tostring();          // endpoint
   object ro = remotingservices.connect(objecttype, objecturl);       // create proxy
   traceimessage(reqmsg);
   imessage rspmsg = remotingservices.executemessage((marshalbyrefobject)ro,
                                                     (imethodcallmessage)reqmsg);
   traceimessage(rspmsg);

   // response: encoding and serializing
   memorystream rspstream = new memorystream();
   bf.serialize(rspstream, rspmsg);
   rspstream.position = 0;
   string response = convert.tobase64string(rspstream.toarray());
   rspstream.close();

   return response;
}

[webmethod]
public string syncprocesssoapmessage(string request)
{
   imessage retmsg = null;
   string response;

   try
   {
      trace.writeline(request);

      // 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.
 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天全县| 高清| 阿瓦提县| 蓝田县| 紫金县| 中山市| 新密市| 巩留县| 桂林市| 盈江县| 达州市| 肥西县| 大足县| 汉沽区| 盱眙县| 昌邑市| 梁平县| 金华市| 柘荣县| 兴安盟| 星座| 聂拉木县| 凌云县| 东阿县| 浦县| 华坪县| 兰溪市| 仙桃市| 巴塘县| 开远市| 密云县| 虎林市| 湛江市| 江油市| 景洪市| 沂源县| 贵溪市| 平乐县| 凭祥市| 丹凤县| 易门县|