Using Web Services for Remoting over the Internet. (下)

发布时间: 2007-01-24 11:34    作者: 未知    来源: 未知    浏览:    评论

  • 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.
 
Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符

Tags

SQL 数据库 asp.net C# XML 控件 .NET教程 程序 事件 数据 安全 代码 Server 客户端 验证 数据库专栏 接口 文件 Oracle DataSet 函数 DataGrid 问题 .net return C#语言 JavaScript 服务 IIS 对象 语句 windows 继承 时间 web.config 设计 开发 参数 变量 解决 字符 ADO.net 环境 VB.Net语言 web 异常 工具 服务器 计算 实例 OLEDB Application VB Word WebService insert asp net 安装 记录

精华推荐

更多

精品下载

更多