介绍 WCF(windows Communication Foundation) -异常处理:一般Exception的处理,FaultException和FaultException
WCF(windows Communication Foundation) -异常处理:一般Exception的处理,FaultException和FaultException<T>的抛出和处理,使用IErrorHandler处理异常。
下面介绍第三种异常:FaultException<T>的抛出和处理
/// <summary>
/// 错误信息实体类(用于错误契约FaultContract)
/// </summary>
[DataContract]
public class FaultMessage
{
/// <summary>
/// 错误信息
/// </summary>
[DataMember]
public string Message { get; set; }
/// <summary>
/// 错误代码
/// </summary>
[DataMember]
public int ErrorCode { get; set; }
}
[ServiceContract]
public interface IHello
{
[OperationContract]
[FaultContract(typeof(FaultMessage))]
void HelloFaultExceptionGeneric();
}
/// <summary>
/// Hello类
/// </summary>
public class Hello : IHello
{
/// <summary>
/// 抛出FaultException<T>异常
/// </summary>
public void HelloFaultExceptionGeneric()
{
throw new FaultException<FaultMessage>(new FaultMessage { Message = "抛出FaultException<T>异常", ErrorCode = -1 }, "为了测试FaultException<T>用的");
}
}
protected void btnHelloFaultExceptionGeneric_Click(object sender, EventArgs e)
{
ExceptionService.HelloClient proxy = new ExceptionService.HelloClient();
try
{
proxy.HelloFaultExceptionGeneric();
}
catch (System.ServiceModel.FaultException<ExceptionService.FaultMessage> ex)
{
lblMsg.Text = string.FORMat("错误代码:{0};错误信息:{1};错误原因:{2}",
ex.Detail.ErrorCode.ToString(),
ex.Detail.Message,
ex.Reason.ToString());
}
finally
{
proxy.Close();
}
}
--结束END--
本文标题: WCF的异常处理
本文链接: https://lsjlt.com/news/148636.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0