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 则将异常传回客户端,客户端抛出异常

    一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
  • 相关阅读:
    Source InSight context 窗口丢失的解决办法
    [EffectiveC++]item41:了解隐式接口和编译器多态
    [EffectiveC++]item04:Make sure the objects are initialized before they're used
    [EffectiveC++]item3:尽可能使用const
    linux man指令问题
    解读ARM成功秘诀:薄利多销推广产品
    source insight设置问题 [问题点数:20分,结帖人leecapacity]
    totalcommander
    firefox
    处理SecureCRT中使用vim出现中文乱码问题
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2063199.html
Copyright © 2011-2022 走看看