服务契约(类型与响应方式)

1、同步请求回复操作

对服务操作来说,同步请求回复消息交换是最普通的模式



2、异步访问服务操作

      请求-响应服务操作会令客户端阻塞直到服务操作执行结束,异步方式可以采用回调方式解决等待问题

      注意:服务端不知道客户端在使用异步编程,客户端的异步模式无需服务器参与操作


代码:

class  Program
     {
         static  int  c = 0;
         static  void  Main( string [] args)
         {
             StockServiceClient proxy = new  StockServiceClient();
             IAsyncResult arGetPrice;
             for  ( int  i = 0; i < 10; i++)
             {
                 arGetPrice = proxy.BeginGetPrice( "msft" , GetPriceCallback, proxy);
                 InterLocker.Increment( ref  c);
             }
             while  (c > 0)
             {
                 Thread.Sleep(1000);
                 Console.WriteLine( "Waiting... Calls outstanding:{0}" , c);
             }
             proxy.Close();
             Console.WriteLine( "Done!" );
         }
 
         //Asynchronous callbacks for displaying results.
         static  void  GetPriceCallback(IAsyncResult ar)
         {
             double  d = ((StockServiceClient)ar.AsyncState).EndGetPrice(ar);
             Interlocked.Decrement( ref  c);
         }
        
     }

3、单向操作

     向服务器发送消息却不需要回应,只需要知道发送成功即可,[OperationContract(IsOneWay = true)] 即可实现

代码:

[ServiceContract]
public  interface  IStockService
{
     [OperationContract(IsOneWay = true )]
     void  DoBigAnalysisFast( string  ticker);
 
     [OperationContract]
     void  DoBigAnalysisSlow( string  ticker);
}


4、双工操作

       双工操作使得服务器向客户端发送消息成为可能,信道一旦建立,可以向任一方发送未经请求的消息



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值