• 当前位置:首页>>网络编程>>Asp.net专区>>Using Web Services for Remoting over the Internet.
  • Using Web Services for Remoting over the Internet.

  • [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;
                _messag
  • 上一篇:为 ADO 程序员设计的 ADO.NET
    下一篇:转:Visual Basic.NET快速开发MIS系统(1)