Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
Framework C#技术 VB.NET VC.NET WCFWPF
当前位置: 主页 > WinForm教程 > WPF >

WCF学习序列(五):异常处理

时间:2009-10-13 23:37来源:未知 作者:admin 点击:

WCF 将服务异常(Exception)转换成 SOAP faults,传递到客户端后再次转换成 Exception。只不过缺省情况下,我们很难从中获取有意义的信息。

[ServiceContract]
public interface ICalculate
{
  [OperationContract]
  int Add(int a, int b);
}
public class CalculateService : ICalculate
{
  public int Add(int a, int b)
  {
    throw new Exception("错误!");
  }
}

  客户端调用 Add 方法触发异常,信息如下:

  System.ServiceModel.FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.

  Server stack trace:

  在 System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)

  ......

  (我们可以使用 "(host as ServiceHost).Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;" 来启动调试行为,这样也能看到具体的出错信息! )

  当然,WCF 会提供一个包装异常类 FaultException 来帮助我们处理这些问题。

[ServiceContract]
public interface ICalculate
{
  [OperationContract]
  int Add(int a, int b);
}
public class CalculateService : ICalculate
{
  public int Add(int a, int b)
  {
    throw new FaultException(new Exception("错误!").Message);
  }
}

  这次输出的信息要友好得多。

  System.ServiceModel.FaultException: 错误!

Server stack trace:
  在 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
  ......

  另外,我们还可以通过 FaultContractAttribute 传递更详细的异常信息给客户端。

[DataContract]
public class FaultMessage
{
  [DataMember] public string Message;
  [DataMember] public int ErrorCode;
}
[ServiceContract]
public interface ICalculate
{
  [OperationContract]
  [FaultContract(typeof(FaultMessage))]
  int Add(int a, int b);
}
public class CalculateService : ICalculate
{
  public int Add(int a, int b)
  {
    FaultMessage fault = new FaultMessage();
    fault.Message = "错误信息!";
    fault.ErrorCode = 1234;
    throw new FaultException<FaultMessage>(fault, fault.Message);
  }
}

  客户端代码

try
{
  CalculateClient client = new ConsoleApplication1.localhost.CalculateClient();
  client.Add(1, 2);
}
catch (FaultException<FaultMessage> e)
{
  Console.WriteLine("{0}; {1}", e.Detail.Message, e.Detail.ErrorCode);
}

(责任编辑:admin)
Tags:WCF
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价