您的位置:首页 > 其它

化零为整WCF(4) - 异常处理(Exception、FaultException、FaultException、IErrorHandler)

2008-01-28 14:12 459 查看
[索引页]

[源码下载]

[align=center]化零为整WCF(4) - 异常处理(Exception、FaultException、FaultException<T>、IErrorHandler)[/align]

作者: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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

namespace WCF.ServiceLib.Exception

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

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

2、宿主

Hello.svc

<?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

<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

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>

运行结果:

单击"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

[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐