国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 都安| 手游| 盐源县| 揭东县| 崇礼县| 芜湖市| 陆良县| 尚志市| 洛南县| 延津县| 台安县| 齐齐哈尔市| 西宁市| 绥阳县| 大兴区| 桃江县| 鹤峰县| 明光市| 灵台县| 铜梁县| 五莲县| 西畴县| 任丘市| 博野县| 高密市| 荣昌县| 莱阳市| 蕲春县| 朔州市| 漠河县| 章丘市| 古浪县| 钟山县| 海伦市| 平谷区| 和平区| 古蔺县| 大冶市| 阿拉善左旗| 榆社县| 政和县|