zoukankan      html  css  js  c++  java
  • 2.WCF 同步 异步

    2.WCF 同步 异步

    using System.ServiceModel;


    namespace Rhythmk.Contracts
    {
    [ServiceContract(Namespace
    ="http://wwww.wangkun.com")]

    public interface ICalculate
    {
    //通过 IsOneWay 实现无返回 异步调用
    [OperationContract( IsOneWay=true)]
    void IsOneWay();

    [OperationContract]
    void IsReturnWay();

    }
    }

    -----------------------------------------------------

    using Rhythmk.Contracts;

    namespace Rhythmk.Services
    {
    /// <summary>
    /// 计算器
    /// </summary>
    public class Calculate:ICalculate
    {


    #region ICalculate 成员

    public void IsOneWay()
    {
    throw new Exception(" 异常方法:IsOneWay ");
    }

    public void IsReturnWay()
    {
    throw new Exception("异常方法:IsReturnWay");

    }

    #endregion
    }
    }



    ----------------------------------

    protected void btn_Click(object sender, EventArgs e)
    {
    ChannelFactory
    <ICalculate> calculatorChannelFactory = new ChannelFactory<ICalculate>("CalculateEndPoint");
    ICalculate proxy
    = calculatorChannelFactory.CreateChannel();

    try
    {
    proxy.IsOneWay();
    Utitl.Alert(
    "正常调用");
    }
    catch (Exception ex) {
    Utitl.Alert(ex.ToString());
    }
    }

    protected void btn2_Click(object sender, EventArgs e)
    {
    ChannelFactory
    <ICalculate> calculatorChannelFactory = new ChannelFactory<ICalculate>("CalculateEndPoint");
    ICalculate proxy
    = calculatorChannelFactory.CreateChannel();

    try
    {
    proxy.IsReturnWay();
    Utitl.Alert(
    "正常调用");
    }
    catch (Exception ex)
    {
    Utitl.Alert(ex.ToString());
    }
    }

    通过设置:       

    //通过 IsOneWay 实现无返回 异步调用
            [OperationContract( IsOneWay=true)]

    IsOneWay 方法能正常调用,客户端不接收异常。

    IsReturnWay 则将异常传回客户端,客户端抛出异常

    一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
  • 相关阅读:
    关于StringBuilder拼成的json格式数据,在转化成string数据类型后,转义符失效
    jQuery 引用地址{包括jquery和google提供的地址}, 节省你不必要的流量
    jQuery的.bind()、.live()和.delegate()之间区别
    使用Jquery Easy UI要导入的js顺序
    babygin
    card
    DecimalFormat用法
    DATEDIFF 函数 [日期和时间]
    IsDirectory( )的用法
    Arrays.asList()函数
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2063199.html
Copyright © 2011-2022 走看看