<a href="http://webabcd.blog.51cto.com/1787395/343999" target="_blank">[索引页]</a>
化零为整WCF(4) - 异常处理(Exception、FaultException、FaultException<T>、IErrorHandler)
介绍
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();
/// 抛出FaultException异常
void HelloFaultException();
/// 抛出FaultException<T>异常
[FaultContract(typeof(FaultMessage))]
void HelloFaultExceptionGeneric();
/// IErrorHandler处理异常
void HelloIErrorHandler();
}
}
FaultMessage.cs
using System.Runtime.Serialization;
/// 错误信息实体类(用于错误契约FaultContract)
[DataContract]
public class FaultMessage
/// 错误信息
[DataMember]
public string Message { get; set; }
/// 错误代码
public int ErrorCode { get; set; }
FaultErrorHandler.cs
using System.ServiceModel.Dispatcher;
using System.Configuration;
using System.ServiceModel.Channels;
/// 自定义错误处理器(继承自System.ServiceModel.Dispatcher.IErrorHandler)
public class FaultErrorHandler : IErrorHandler
/// 在异常返回给客户端之后被调用
/// <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;
}
/// 在异常发生后,异常信息返回前被调用
/// <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.ServiceModel.Description;
/// Hello类
public class Hello : IHello, IDisposable, IServiceBehavior
public void HelloException()
throw new System.Exception("抛出Exception异常");
public void HelloFaultException()
throw new FaultException("抛出FaultException异常", new FaultCode("服务"));
public void HelloFaultExceptionGeneric()
throw new FaultException<FaultMessage>(new FaultMessage { Message = "抛出FaultException<T>异常", ErrorCode = -1 }, "为了测试FaultException<T>用的");
public void HelloIErrorHandler()
throw new System.IO.IOException("抛出异常,用IErrorHandler处理");
/// 实现IDisposable接口的Dispose()方法
public void Dispose()
/// 为契约增加自定义绑定参数
/// <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)
/// runtime时修改属性值或增加自定义扩展对象
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
IErrorHandler handler = new FaultErrorHandler();
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
// 增加错误处理器
dispatcher.ErrorHandlers.Add(handler);
/// 检查服务描述和服务宿主,以确认服务可以成功运行
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>
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 />
<asp:Button ID="btnHelloException" runat="server" Text="HelloException" OnClick="btnHelloException_Click" />
<asp:Button ID="btnHelloFaultException" runat="server" Text="HelloFaultException"
OnClick="btnHelloFaultException_Click" />
<asp:Button ID="btnHelloFaultExceptionGeneric" runat="server" Text="HelloFaultExceptionGeneric"
OnClick="btnHelloFaultExceptionGeneric_Click" />
<asp:Button ID="btnHelloIErrorHandler" runat="server" Text="HelloIErrorHandler" OnClick="btnHelloIErrorHandler_Click" />
</asp:Content>
Hello.aspx.cs
using System.Collections;
using System.Data;
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;
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)
proxy.HelloFaultException();
catch (FaultException ex)
lblMsg.Text = string.Format("错误编码:{0};错误原因:{1}",
ex.Code.Name,
ex.Reason.ToString());
proxy.Close();
protected void btnHelloFaultExceptionGeneric_Click(object sender, EventArgs e)
proxy.HelloFaultExceptionGeneric();
catch (System.ServiceModel.FaultException<ExceptionService.FaultMessage> ex)
lblMsg.Text = string.Format("错误代码:{0};错误信息:{1};错误原因:{2}",
ex.Detail.ErrorCode.ToString(),
ex.Detail.Message,
protected void btnHelloIErrorHandler_Click(object sender, EventArgs e)
proxy.HelloIErrorHandler();
System.ServiceModel.FaultException faultException = ex as System.ServiceModel.FaultException;
if (faultException != null)
lblMsg.Text = string.Format("错误信息:{0}", faultException.Message);
else
lblMsg.Text = ex.ToString();
<client>
<!--address - 服务地址-->
<!--binding - 通信方式-->
<!--contract - 服务契约-->
<endpoint address="http://localhost:3502/ServiceHost/Exception/Hello.svc" binding="wsHttpBinding" contract="ExceptionService.IHello" />
</client>
运行结果:
单击"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
<a href="http://down.51cto.com/data/100781" target="_blank">[源码下载]</a>
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344112,如需转载请自行联系原作者