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 则将异常传回客户端,客户端抛出异常
一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。