化零为整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
[源码下载]
[源码下载]
[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
[源码下载]
相关文章推荐
- 化零为整WCF(4) - 异常处理(Exception、FaultException、FaultException<T>、IErrorHandler)
- 化零为整WCF(4) - 异常处理(Exception、FaultException、FaultException、IErrorHandler)
- DefaultDispatcherErrorHandler: Exception occurred during processing request
- 异常处理函数set_exception_handler
- 异常处理:Sys.WebForms.PageRequestManagerParserErrorException:The message……
- php:关闭出错提示后用set_error_handler/set_exception_handler输出出错
- Error libc++abi.dylib handler threw exception
- 稍作完善:wcf的异常处理(Exception Handling )
- springMVC源码分析--异常处理机制HandlerExceptionResolver执行原理(二)
- SpringMVC 学习笔记(十) 异常处理HandlerExceptionResolver
- Spring全局异常捕捉实现HandlerExceptionResolver接口,在error-page配置捕捉异常
- HTTP Status 500 - Handler processing failed; nested exception is java.lang.OutOfMemoryError: PermGen
- 【WCF】自定义错误处理(IErrorHandler接口的用法)
- 利用Attribute和IErrorHandler处理WCF全局异常
- IOS崩溃 异常处理(NSSetUncaughtExceptionHandler)
- 使用Spring中的ExceptionHandlerExceptionResolver进行统一的异常处理
- PHP7的异常处理机制,set_error_handler和set_exception_handler方法介绍
- java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "javamelody jenkins
- PHP 的异常处理、错误处理:error_reporting,try-catch,trigger_error,set_error_handler,set_exception_handler,regis
- Oracle OGG 学习五: GoldenGate Exception Handler 异常处理二