原创地址:http://www.cnblogs.com/jfzhu/p/4060666.html

转载请注明出处

前面的文章《WCF服务的异常消息》中介绍过,如果WCF Service发生异常时,Service会将异常序列化为SOAP Fault并发送给客户端。

默认情况下,出于安全原因,WCF Service中未处理的异常的详细信息不会包括在发送给客户的SOAP Fault里,你只能看到一个通用的SOAP Fault(“The server was unable to process the request due to an internal error.”)。在调试程序的时候,如果想在SOAP Fault中包含异常的详细信息,可以修改服务器的配置文件。

<behaviors>
<serviceBehaviors>
<behavior name="includeExceptionDetails">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>

SOAP Fault是XML格式,与平台无关。通常一个SOAP Fault包含以下节点

(1) faultcode

(2) faultstring

(3) detail

Detail节点可以用来包括自定义XML信息。

WCF Service在发生异常时应抛出FaultException或FaultException<T>,而不应该抛出.NET Exception,出于以下两个原因:

(1)未处理的.NET Exception会使服务器与客户端之间的channel变为Fault状态,继而导致client proxy无法使用。

(2).NET Exception只能被.NET平台理解,而FaultException与平台无关。如果想跨平台使用,需要使用FaultException。

下面还是以中《WCF服务的异常消息》的例子来分别演示如何抛出与处理FaultException与强类型的FaultException<T>。

(一)使用FaultException

IDemoService.cs:

using System.ServiceModel;

namespace WCFDemo
{
[ServiceContract(Name = "IDemoService")]
public interface IDemoService
{
[OperationContract]
int Divide(int numerator, int denominator);
}
}

DemoService.cs:

using System;
using System.ServiceModel;
using System.ServiceModel.Activation; namespace WCFDemo
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DemoService : IDemoService
{
public int Divide(int numerator, int denominator)
{
if (denominator == )
{
throw new FaultException("Denominator cannot be ZERO!", new FaultCode("DivideByZeroFault"));
}
return numerator / denominator;
}
}
}

client:

private void buttonCalculate_Click(object sender, EventArgs e)
{
try
{
textBoxResult.Text = demoServiceClient.Divide(Convert.ToInt32(textBoxNumerator.Text), Convert.ToInt32(textBoxDenominator.Text)).ToString();
}
catch (FaultException fault)
{
MessageBox.Show(fault.Code + " - " + fault.Message);
}
}

SOAP Fault XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode>s:DivideByZeroFault</faultcode>
<faultstring xml:lang="en-US">Denominator cannot be ZERO!</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>

(二)使用强类型FaultException<T>

(1)创建一个自定义SOAP Fault类

DivideByZeroFault.cs:

using System.Runtime.Serialization;

namespace WCFDemo
{
[DataContract]
public class DivideByZeroFault
{
[DataMember]
public string Error { get; set; } [DataMember]
public string Detail { get; set; }
}
}

(2) 在Service方法上使用FaultContractAttribute来指示哪个操作可以使用哪个Fault

IDemoService.cs:

using System.ServiceModel;

namespace WCFDemo
{
[ServiceContract(Name = "IDemoService")]
public interface IDemoService
{
[FaultContract(typeof(DivideByZeroFault))]
[OperationContract]
int Divide(int numerator, int denominator);
}
}

DemoService.cs:

using System;
using System.ServiceModel;
using System.ServiceModel.Activation; namespace WCFDemo
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DemoService : IDemoService
{
public int Divide(int numerator, int denominator)
{
try
{
return numerator / denominator;
}
catch (DivideByZeroException ex)
{
DivideByZeroFault fault = new DivideByZeroFault();
fault.Error = ex.Message;
fault.Detail = "Denominator cannot be ZERO!";
throw new FaultException<DivideByZeroFault>(fault);
}
}
}
}

client:

private void buttonCalculate_Click(object sender, EventArgs e)
{
try
{
textBoxResult.Text = demoServiceClient.Divide(Convert.ToInt32(textBoxNumerator.Text), Convert.ToInt32(textBoxDenominator.Text)).ToString();
}
catch (FaultException<DemoServiceReference.DivideByZeroFault> fault)
{
MessageBox.Show(fault.Detail.Error + " - " + fault.Detail.Detail);
}
}

返回的SOAP Fault XML:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="en-US">The creator of this fault did not specify a Reason.</faultstring>
<detail>
<DivideByZeroFault xmlns="http://schemas.datacontract.org/2004/07/WCFDemo" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Detail>Denominator cannot be ZERO!</Detail>
<Error>Attempted to divide by zero.</Error>
</DivideByZeroFault>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>

WCF服务创建与抛出强类型SOAP Fault的更多相关文章

  1. WCF服务创建与使用(双工模式)

    昨天发布了<WCF服务创建与使用(请求应答模式)>,今天继续学习与强化在双工模式下WCF服务创建与使用,步骤与代码如下. 第一步,定义服务契约(Service Contract),注意Se ...

  2. wcf 服务创建,配置,测试

    一.WCF创建: 常规的创建WCF服务是通过SOAP传输的,很多网站开发人员想放弃使用XML而使用JSON,这个时候可以参照:http://www.cnblogs.com/zhili/p/WCFRes ...

  3. WCF服务创建到发布(SqlServer版)

    在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构.该架构的项层为服务模型层. 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型. ...

  4. WCF服务创建与使用(请求应答模式)

    不说废话,直接上代码.以下服务创建是在独立的WCF类库中,若采用WCF应程程序,定义及创建服务代码均相同,但文件名不同,是CalculatorService.svc 第一步,定义服务契约(Servic ...

  5. ssh项目问题01,为创建数据库抛出的异常

    框架什么都搭建好了,但是一直抛出如图问题,网上资料很多让你设置时间之类的,也设置了还是继续抛异常,最后带我的师傅说没有创建数据库,我都要郁闷死了,网上那么多,很多写的都不能解决问题,还乱写,浪费别人时 ...

  6. 添加wcf服务引用,无法签出当前文件

    写了一些wcf服务接口,使用控制台可以正常启动服务,想要测试一下,新建项目添加服务引用,提示:“无法签出当前文件.该文件可能为只读或已锁定,或者您需要手动签出它.” 在网上找了找,有说可能是因为源代码 ...

  7. 创建圆角 抛出一个错误:二元运算符“|”不能用于两个UIRectCorner操作数

    //        let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: UIRect ...

  8. 重温WCF之WCF抛出异常的处理SOAP Fault(十二)

    1.(服务端)抛出和(客户端)捕获SOAP Fault 当我们需要客户端获取到WCF服务端的抛出的异常的时候,使用FaultException类 WCF类库在System.ServiceModel命名 ...

  9. WCF入门(五)---创建WCF服务

    使用Microsoft Visual Studio2012创建WCF服务,理解如下所有必要的编码,更好地创建WCF服务的概念,这里做一个简单的任务. 启动Visual Studio 2012. 单击新 ...

随机推荐

  1. Go语言 获取get、post参数

    在贴代码之前如果能先理解一下golang http.request的三个属性Form.PostForm.MultipartForm应该能较好的理解代码,下面摘录一下. 以上简要翻译一下: Form:存 ...

  2. js字符串格式化扩展方法

    平时使用js的时候会遇到很多需要拼接字符串的时候,如果是遇到双引号和单引号混合使用,经常会搞混.在C#中有string.Format方法,使用起来非常方便,也很容易理解,所以找到一种参考C#的form ...

  3. IDA的脚本IDC的一个简单使用

    目的:主要是想学习一下IDA的IDC的脚本的使用.这里做了一个小的测试. 这里使用的是VS2015Community来生成文件的. 一.编写测试程序: 这里先生成我们的目标数据. 然后编写测试程序.得 ...

  4. hdu2662

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2662 莫名其妙写了一个题,感觉还是很有价值的记录一下. 题目大意:给两个互质的数,求用无限个它们不能组 ...

  5. Java EE之一个表单两个按钮响应不同界面(登录与注册)

    1.在前端,使用javaScript实现: <script> function logup(){ var element = document.getElementById('login- ...

  6. (转) Android开发性能优化简介

    作者:贺小令 随着技术的发展,智能手机硬件配置越来越高,可是它和现在的PC相比,其运算能力,续航能力,存储空间等都还是受到很大的限制,同时用户对手机的体验要求远远高于PC的桌面应用程序.以上理由,足以 ...

  7. .NET开发人员必看:提高ASP.NET Web应用性能的24种方法和技巧

    那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...

  8. 深入理解Java的接口和抽象类(转)

    深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...

  9. Xamarin笔记

    Xamarin学习笔记 1. Xamarin Studio自动更新下载的安装文件缓存路径:C:\Users\登录用户\AppData\Local\XamarinStudio-5.0\Cache\Tem ...

  10. SqlServer windowss身份登陆和sa身份登陆

    今天重新装了系统,但是计算机名变了,于是修改了计算机名,然后装了SQLSEVER,安装完成后登录,发现无论用WINDOWS身份还是SQLSERVER身份都登录不了 1.先说说sqlserver身份登录 ...