wse2.0学习笔记--消息的接收

使用WSE2.0的soapclient和soapservice可以模拟WEBSERVICE的功能,区别是可以脱离IIS.这点很重要.

using System;
using System.Web;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Addressing;
using Microsoft.Web.Services2.Messaging;
using Microsoft.Web.Services2.Referral;
namespace MySoapReceiver
{
 /// <summary>
 /// This is a sample which allows the user to receive a message
 /// to a simple Web service.
 /// </summary>
 public class StockServiceHost
 {
       
  [MTAThread]
  static void Main(string[] args)
  {
   StockServiceHost host = null;
           
   //
   // Instantiate the host and run it        
   //
   try
   {
    host = new StockServiceHost();
    host.Run();
   }
   catch (Exception ex)
   {
    Console.WriteLine(ex.Message);              
   }            

   //
   // Wait for a final key press to close the application.
   //
   Console.WriteLine( "" );
   Console.WriteLine("Press any key to exit when done...");
   Console.WriteLine( "" );
   Console.ReadLine();
  }

  public void Run()
  {         
   //
   // Add the System.Type of the service (SoapServiceTest) to the SoapReceivers collection.
   // This will start a TCP-based listener if there isn't one already started.
   //
   Uri via = new Uri("soap.tcp://" + System.Net.Dns.GetHostName() +":8888");//外部地址
   Uri address = new Uri("soap://maxplatform.com/ServiceTest");//类地址
   EndpointReference endpoint = new EndpointReference(address, via);
           
   SoapReceivers.Add( endpoint, typeof(SoapServiceTest) );//添加到soapReceivers
   Console.WriteLine( "Listening for messages at:\n" + address );                    
  }           
 }
}
================receiver.cs

/*=====================================================================
  File:      Service.cs

  Summary:   This is a sample which represents a Web service

---------------------------------------------------------------------
 
=====================================================================*/

using System;
using System.Collections;

using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Messaging;


namespace MySoapReceiver
{
 
 [SoapActor("soap://maxplatform.com/ServiceTest")]//类地址
 public class SoapServiceTest : SoapService
 {
  
  [SoapMethod("SimpleServiceMethod")]//客户端需要有一个相同的代理方法.
  public string GetString( string message )
  {
   Console.WriteLine( "收到消息:\n " );
   Console.WriteLine(message);
   Console.WriteLine("返回消息:\n");
   string ret="\n服务器端收到了你发的消息:["+message+"]";
   Console.WriteLine(ret);
   return ret;
  }

  public static StockServiceHost host;
 }
}
 

posted @ 2004-12-22 02:06  meteorcui  阅读(183)  评论(0编辑  收藏  举报