日期:2008-10-23  浏览次数:20431 次

  • 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().Sp