[索引页]
[源码下载]
作者: webabcd
介绍
WCF(Windows Communication Foundation) - 异常处理:一般Exception的处理,FaultException和FaultException<T>的抛出和处理,使用IErrorHandler处理异常。
示例
1、服务
IHello.cs
FaultMessage.cs
FaultErrorHandler.cs
Hello.cs
2、宿主
Hello.svc
Web.config
3、客户端
Hello.aspx
Hello.aspx.cs
Web.config
运行结果:
单击" btnHelloException"后显示
抛出Exception异常
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
单击" btnHelloFaultException"后显示
错误编码:服务;错误原因:抛出FaultException异常
单击" btnHelloFaultExceptionGeneric"后显示
错误代码:-1;错误信息:抛出FaultException异常;错误原因:为了测试FaultException用的
单击" btnHelloIErrorHandler"后显示
错误信息:IErrorHandler - ProvideFault测试
OK
[源码下载]
[源码下载]
化零为整WCF(4) - 异常处理(Exception、FaultException、FaultException<T>、IErrorHandler)
作者: webabcd
介绍
WCF(Windows Communication Foundation) - 异常处理:一般Exception的处理,FaultException和FaultException<T>的抛出和处理,使用IErrorHandler处理异常。
示例
1、服务
IHello.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Exception
{
/// <summary>
/// IHello接口
/// </summary>
[ServiceContract]
public interface IHello
{
/// <summary>
/// 抛出Exception异常
/// </summary>
[OperationContract]
void HelloException();
/// <summary>
/// 抛出FaultException异常
/// </summary>
[OperationContract]
void HelloFaultException();
/// <summary>
/// 抛出FaultException<T>异常
/// </summary>
[OperationContract]
[FaultContract(typeof(FaultMessage))]
void HelloFaultExceptionGeneric();
/// <summary>
/// IErrorHandler处理异常
/// </summary>
[OperationContract]
void HelloIErrorHandler();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Exception
{
/// <summary>
/// IHello接口
/// </summary>
[ServiceContract]
public interface IHello
{
/// <summary>
/// 抛出Exception异常
/// </summary>
[OperationContract]
void HelloException();
/// <summary>
/// 抛出FaultException异常
/// </summary>
[OperationContract]
void HelloFaultException();
/// <summary>
/// 抛出FaultException<T>异常
/// </summary>
[OperationContract]
[FaultContract(typeof(FaultMessage))]
void HelloFaultExceptionGeneric();
/// <summary>
/// IErrorHandler处理异常
/// </summary>
[OperationContract]
void HelloIErrorHandler();
}
}
FaultMessage.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace WCF.ServiceLib.Exception
{
/// <summary>
/// 错误信息实体类(用于错误契约FaultContract)
/// </summary>
[DataContract]
public class FaultMessage
{
/// <summary>
/// 错误信息
/// </summary>
[DataMember]
public string Message { get; set; }
/// <summary>
/// 错误代码
/// </summary>
[DataMember]
public int ErrorCode { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace WCF.ServiceLib.Exception
{
/// <summary>
/// 错误信息实体类(用于错误契约FaultContract)
/// </summary>
[DataContract]
public class FaultMessage
{
/// <summary>
/// 错误信息
/// </summary>
[DataMember]
public string Message { get; set; }
/// <summary>
/// 错误代码
/// </summary>
[DataMember]
public int ErrorCode { get; set; }
}
}
FaultErrorHandler.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace WCF.ServiceLib.Exception
{
/// <summary>
/// 自定义错误处理器(继承自System.ServiceModel.Dispatcher.IErrorHandler)
/// </summary>
public class FaultErrorHandler : IErrorHandler
{
/// <summary>
/// 在异常返回给客户端之后被调用
/// </summary>
/// <param name="error">异常</param>
/// <returns></returns>
public bool HandleError(System.Exception error)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:/WCF_Log.txt", true);
sw.Write("IErrorHandler - HandleError测试。错误类型:{0};错误信息:{1}", error.GetType().ToString(), error.Message);
sw.WriteLine();
sw.Flush();
sw.Close();
// true - 已处理
return true;
}
/// <summary>
/// 在异常发生后,异常信息返回前被调用
/// </summary>
/// <param name="error">异常</param>
/// <param name="version">SOAP版本</param>
/// <param name="fault">返回给客户端的错误信息</param>
public void ProvideFault(System.Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
{
if (error is System.IO.IOException)
{
FaultException ex = new FaultException("IErrorHandler - ProvideFault测试");
MessageFault mf = ex.CreateMessageFault();
fault = Message.CreateMessage(version, mf, ex.Action);
// InvalidOperationException error = new InvalidOperationException("An invalid operation has occurred.");
// MessageFault mfault = MessageFault.CreateFault(new FaultCode("Server", new FaultCode(String.Format("Server.{0}", error.GetType().Name))), new FaultReason(error.Message), error);
// FaultException fe = FaultException.CreateFault(mfault, typeof(InvalidOperationException));
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace WCF.ServiceLib.Exception
{
/// <summary>
/// 自定义错误处理器(继承自System.ServiceModel.Dispatcher.IErrorHandler)
/// </summary>
public class FaultErrorHandler : IErrorHandler
{
/// <summary>
/// 在异常返回给客户端之后被调用
/// </summary>
/// <param name="error">异常</param>
/// <returns></returns>
public bool HandleError(System.Exception error)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:/WCF_Log.txt", true);
sw.Write("IErrorHandler - HandleError测试。错误类型:{0};错误信息:{1}", error.GetType().ToString(), error.Message);
sw.WriteLine();
sw.Flush();
sw.Close();
// true - 已处理
return true;
}
/// <summary>
/// 在异常发生后,异常信息返回前被调用
/// </summary>
/// <param name="error">异常</param>
/// <param name="version">SOAP版本</param>
/// <param name="fault">返回给客户端的错误信息</param>
public void ProvideFault(System.Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
{
if (error is System.IO.IOException)
{
FaultException ex = new FaultException("IErrorHandler - ProvideFault测试");
MessageFault mf = ex.CreateMessageFault();
fault = Message.CreateMessage(version, mf, ex.Action);
// InvalidOperationException error = new InvalidOperationException("An invalid operation has occurred.");
// MessageFault mfault = MessageFault.CreateFault(new FaultCode("Server", new FaultCode(String.Format("Server.{0}", error.GetType().Name))), new FaultReason(error.Message), error);
// FaultException fe = FaultException.CreateFault(mfault, typeof(InvalidOperationException));
}
}
}
}
Hello.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace WCF.ServiceLib.Exception
{
/// <summary>
/// Hello类
/// </summary>
public class Hello : IHello, IDisposable, IServiceBehavior
{
/// <summary>
/// 抛出Exception异常
/// </summary>
public void HelloException()
{
throw new System.Exception("抛出Exception异常");
}
/// <summary>
/// 抛出FaultException异常
/// </summary>
public void HelloFaultException()
{
throw new FaultException("抛出FaultException异常", new FaultCode("服务"));
}
/// <summary>
/// 抛出FaultException<T>异常
/// </summary>
public void HelloFaultExceptionGeneric()
{
throw new FaultException<FaultMessage>(new FaultMessage { Message = "抛出FaultException<T>异常", ErrorCode = -1 }, "为了测试FaultException<T>用的");
}
/// <summary>
/// IErrorHandler处理异常
/// </summary>
public void HelloIErrorHandler()
{
throw new System.IO.IOException("抛出异常,用IErrorHandler处理");
}
/// <summary>
/// 实现IDisposable接口的Dispose()方法
/// </summary>
public void Dispose()
{
}
/// <summary>
/// 为契约增加自定义绑定参数
/// </summary>
/// <param name="serviceDescription">服务描述</param>
/// <param name="serviceHostBase">服务宿主</param>
/// <param name="endpoints">服务端点</param>
/// <param name="bindingParameters">需要增加的自定义绑定参数</param>
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
/// <summary>
/// runtime时修改属性值或增加自定义扩展对象
/// </summary>
/// <param name="serviceDescription">服务描述</param>
/// <param name="serviceHostBase">服务宿主</param>
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
IErrorHandler handler = new FaultErrorHandler();
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
// 增加错误处理器
dispatcher.ErrorHandlers.Add(handler);
}
}
/// <summary>
/// 检查服务描述和服务宿主,以确认服务可以成功运行
/// </summary>
/// <param name="serviceDescription">服务描述</param>
/// <param name="serviceHostBase">服务宿主</param>
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace WCF.ServiceLib.Exception
{
/// <summary>
/// Hello类
/// </summary>
public class Hello : IHello, IDisposable, IServiceBehavior
{
/// <summary>
/// 抛出Exception异常
/// </summary>
public void HelloException()
{
throw new System.Exception("抛出Exception异常");
}
/// <summary>
/// 抛出FaultException异常
/// </summary>
public void HelloFaultException()
{
throw new FaultException("抛出FaultException异常", new FaultCode("服务"));
}
/// <summary>
/// 抛出FaultException<T>异常
/// </summary>
public void HelloFaultExceptionGeneric()
{
throw new FaultException<FaultMessage>(new FaultMessage { Message = "抛出FaultException<T>异常", ErrorCode = -1 }, "为了测试FaultException<T>用的");
}
/// <summary>
/// IErrorHandler处理异常
/// </summary>
public void HelloIErrorHandler()
{
throw new System.IO.IOException("抛出异常,用IErrorHandler处理");
}
/// <summary>
/// 实现IDisposable接口的Dispose()方法
/// </summary>
public void Dispose()
{
}
/// <summary>
/// 为契约增加自定义绑定参数
/// </summary>
/// <param name="serviceDescription">服务描述</param>
/// <param name="serviceHostBase">服务宿主</param>
/// <param name="endpoints">服务端点</param>
/// <param name="bindingParameters">需要增加的自定义绑定参数</param>
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
/// <summary>
/// runtime时修改属性值或增加自定义扩展对象
/// </summary>
/// <param name="serviceDescription">服务描述</param>
/// <param name="serviceHostBase">服务宿主</param>
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
IErrorHandler handler = new FaultErrorHandler();
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
// 增加错误处理器
dispatcher.ErrorHandlers.Add(handler);
}
}
/// <summary>
/// 检查服务描述和服务宿主,以确认服务可以成功运行
/// </summary>
/// <param name="serviceDescription">服务描述</param>
/// <param name="serviceHostBase">服务宿主</param>
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
}
2、宿主
Hello.svc
<%
@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Exception.Hello"
%>
Web.config
<?
xml version="1.0"
?>
< configuration >
< system.serviceModel >
< behaviors >
< serviceBehaviors >
< behavior name ="ExceptionBehavior" >
<!-- httpGetEnabled - 使用get方式提供服务 -->
< serviceMetadata httpGetEnabled ="true" />
< serviceDebug includeExceptionDetailInFaults ="true" />
</ behavior >
</ serviceBehaviors >
</ behaviors >
< services >
<!-- name - 提供服务的类名 -->
<!-- behaviorConfiguration - 指定相关的行为配置 -->
< service name ="WCF.ServiceLib.Exception.Hello" behaviorConfiguration ="ExceptionBehavior" >
<!-- address - 服务地址 -->
<!-- binding - 通信方式 -->
<!-- contract - 服务契约 -->
< endpoint address ="" binding ="wsHttpBinding" contract ="WCF.ServiceLib.Exception.IHello" />
</ service >
</ services >
</ system.serviceModel >
</ configuration >
< configuration >
< system.serviceModel >
< behaviors >
< serviceBehaviors >
< behavior name ="ExceptionBehavior" >
<!-- httpGetEnabled - 使用get方式提供服务 -->
< serviceMetadata httpGetEnabled ="true" />
< serviceDebug includeExceptionDetailInFaults ="true" />
</ behavior >
</ serviceBehaviors >
</ behaviors >
< services >
<!-- name - 提供服务的类名 -->
<!-- behaviorConfiguration - 指定相关的行为配置 -->
< service name ="WCF.ServiceLib.Exception.Hello" behaviorConfiguration ="ExceptionBehavior" >
<!-- address - 服务地址 -->
<!-- binding - 通信方式 -->
<!-- contract - 服务契约 -->
< endpoint address ="" binding ="wsHttpBinding" contract ="WCF.ServiceLib.Exception.IHello" />
</ service >
</ services >
</ system.serviceModel >
</ configuration >
3、客户端
Hello.aspx
<%
@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Hello.aspx.cs"
Inherits="Exception_Hello" Title="异常处理(Exception、FaultException、FaultException<T>、IErrorHandler)" %>
< asp:Content ID ="Content1" ContentPlaceHolderID ="head" runat ="Server" >
</ asp:Content >
< asp:Content ID ="Content2" ContentPlaceHolderID ="ContentPlaceHolder1" runat ="Server" >
< div >
< asp:Label ID ="lblMsg" runat ="server" />
</ div >
< br />
< div >
< asp:Button ID ="btnHelloException" runat ="server" Text ="HelloException" OnClick ="btnHelloException_Click" />
</ div >
< div >
< asp:Button ID ="btnHelloFaultException" runat ="server" Text ="HelloFaultException"
OnClick ="btnHelloFaultException_Click" />
</ div >
< div >
< asp:Button ID ="btnHelloFaultExceptionGeneric" runat ="server" Text ="HelloFaultExceptionGeneric"
OnClick ="btnHelloFaultExceptionGeneric_Click" />
</ div >
< div >
< asp:Button ID ="btnHelloIErrorHandler" runat ="server" Text ="HelloIErrorHandler" OnClick ="btnHelloIErrorHandler_Click" />
</ div >
</ asp:Content >
Inherits="Exception_Hello" Title="异常处理(Exception、FaultException、FaultException<T>、IErrorHandler)" %>
< asp:Content ID ="Content1" ContentPlaceHolderID ="head" runat ="Server" >
</ asp:Content >
< asp:Content ID ="Content2" ContentPlaceHolderID ="ContentPlaceHolder1" runat ="Server" >
< div >
< asp:Label ID ="lblMsg" runat ="server" />
</ div >
< br />
< div >
< asp:Button ID ="btnHelloException" runat ="server" Text ="HelloException" OnClick ="btnHelloException_Click" />
</ div >
< div >
< asp:Button ID ="btnHelloFaultException" runat ="server" Text ="HelloFaultException"
OnClick ="btnHelloFaultException_Click" />
</ div >
< div >
< asp:Button ID ="btnHelloFaultExceptionGeneric" runat ="server" Text ="HelloFaultExceptionGeneric"
OnClick ="btnHelloFaultExceptionGeneric_Click" />
</ div >
< div >
< asp:Button ID ="btnHelloIErrorHandler" runat ="server" Text ="HelloIErrorHandler" OnClick ="btnHelloIErrorHandler_Click" />
</ div >
</ asp:Content >
Hello.aspx.cs
using
System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ServiceModel;
public partial class Exception_Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnHelloException_Click(object sender, EventArgs e)
{
ExceptionService.HelloClient proxy = new ExceptionService.HelloClient();
try
{
proxy.HelloException();
}
catch (Exception ex)
{
lblMsg.Text = ex.Message;
}
finally
{
try
{
proxy.Close();
}
catch (Exception ex)
{
lblMsg.Text += "<br />" + ex.Message;
}
}
}
protected void btnHelloFaultException_Click(object sender, EventArgs e)
{
ExceptionService.HelloClient proxy = new ExceptionService.HelloClient();
try
{
proxy.HelloFaultException();
}
catch (FaultException ex)
{
lblMsg.Text = string.Format("错误编码:{0};错误原因:{1}",
ex.Code.Name,
ex.Reason.ToString());
}
finally
{
proxy.Close();
}
}
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();
}
}
protected void btnHelloIErrorHandler_Click(object sender, EventArgs e)
{
ExceptionService.HelloClient proxy = new ExceptionService.HelloClient();
try
{
proxy.HelloIErrorHandler();
}
catch (Exception ex)
{
System.ServiceModel.FaultException faultException = ex as System.ServiceModel.FaultException;
if (faultException != null)
{
lblMsg.Text = string.Format("错误信息:{0}", faultException.Message);
}
else
{
lblMsg.Text = ex.ToString();
}
}
finally
{
proxy.Close();
}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ServiceModel;
public partial class Exception_Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnHelloException_Click(object sender, EventArgs e)
{
ExceptionService.HelloClient proxy = new ExceptionService.HelloClient();
try
{
proxy.HelloException();
}
catch (Exception ex)
{
lblMsg.Text = ex.Message;
}
finally
{
try
{
proxy.Close();
}
catch (Exception ex)
{
lblMsg.Text += "<br />" + ex.Message;
}
}
}
protected void btnHelloFaultException_Click(object sender, EventArgs e)
{
ExceptionService.HelloClient proxy = new ExceptionService.HelloClient();
try
{
proxy.HelloFaultException();
}
catch (FaultException ex)
{
lblMsg.Text = string.Format("错误编码:{0};错误原因:{1}",
ex.Code.Name,
ex.Reason.ToString());
}
finally
{
proxy.Close();
}
}
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();
}
}
protected void btnHelloIErrorHandler_Click(object sender, EventArgs e)
{
ExceptionService.HelloClient proxy = new ExceptionService.HelloClient();
try
{
proxy.HelloIErrorHandler();
}
catch (Exception ex)
{
System.ServiceModel.FaultException faultException = ex as System.ServiceModel.FaultException;
if (faultException != null)
{
lblMsg.Text = string.Format("错误信息:{0}", faultException.Message);
}
else
{
lblMsg.Text = ex.ToString();
}
}
finally
{
proxy.Close();
}
}
}
Web.config
<?
xml version="1.0"
?>
< configuration >
< system.serviceModel >
< client >
<!-- address - 服务地址 -->
<!-- binding - 通信方式 -->
<!-- contract - 服务契约 -->
< endpoint address ="http://localhost:3502/ServiceHost/Exception/Hello.svc" binding ="wsHttpBinding" contract ="ExceptionService.IHello" />
</ client >
</ system.serviceModel >
</ configuration >
< configuration >
< system.serviceModel >
< client >
<!-- address - 服务地址 -->
<!-- binding - 通信方式 -->
<!-- contract - 服务契约 -->
< endpoint address ="http://localhost:3502/ServiceHost/Exception/Hello.svc" binding ="wsHttpBinding" contract ="ExceptionService.IHello" />
</ client >
</ system.serviceModel >
</ configuration >
运行结果:
单击" btnHelloException"后显示
抛出Exception异常
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
单击" btnHelloFaultException"后显示
错误编码:服务;错误原因:抛出FaultException异常
单击" btnHelloFaultExceptionGeneric"后显示
错误代码:-1;错误信息:抛出FaultException异常;错误原因:为了测试FaultException用的
单击" btnHelloIErrorHandler"后显示
错误信息:IErrorHandler - ProvideFault测试
OK
[源码下载]