WCF异常相关经验分享

开发 开发工具
WCF异常在实际使用中是不可避免的。那么如何才能将WCF异常信息传递到客户端中呢?对于WCF异常的具体含义又有多少人清楚呢?

WCF框架作为一款由微软开发的帮助解决跨平台操作的方案,深受开发人员喜爱。在这里主要介绍一下WCF异常的相关知识。希望对大家有用。#t#

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

 

 

  1. < ServiceContract()> _  
  2. Public Interface IService1  
  3. < OperationContract()> _  
  4. Function GetData(ByVal
     value As Integer) As String  
  5.  
  6. End Interface  
  7. Public Class Service1  
  8. Implements IService1  
  9. Public Function GetData(ByVal 
    value As Integer) As String 
    Implements IService1.GetData  
  10. Throw New Exception("发生错误。")  
  11. Return String.Format
    ("You entered: {0}", value)  
  12. End Function  
  13. End Class 

 

抛出来的WCF异常信息:

接收对 http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/ 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。有关详细信息,请参阅服务器日志。

 

Server stack trace:

在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)

在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
 

 

 

不太好理解

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

发生错误。

Server stack trace:

在 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)

在 System.Servi
 

这样就对WCF异常好理解多了,所以在服务端抛出异常时,使用FaultException 异常类。

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

 

 

 

  1. < ServiceContract()> _  
  2. Public Interface IService1  
  3. < OperationContract(), 
    FaultContract(GetType(Fault))
    > _  
  4. Function GetData(ByVal 
    value As Integer) As String  
  5. End Interface  
  6. Public Class Service1  
  7. Implements IService1  
  8. Public Function GetData(
    ByVal value As Integer) As 
    String Implements IService1.GetData  
  9. Dim f As New Fault  
  10. f.ErrorCode = 200 
  11. f.Message = "发生错误" 
  12. Throw New FaultException
    (Of Fault)(f, f.Message)  
  13. Return String.Format("You
     entered: {0}", value)  
  14. End Function  
  15. End Class  
  16. < DataContract()> _  
  17. Public Class Fault  
  18. < DataMember()> _  
  19. Public ErrorCode As Integer  
  20. < DataMember()> _  
  21. Public Message As String
    String = String.Empty  
  22. End Class 

 

 

 

 

 

 

客户端代码:

 

  1. Sub Main()  
  2. Dim url As String = "http:/
    /localhost:8731/Design_Time_
    Addresses/WcfServiceLibrary1
    /Service1/mex"
     
  3. Dim host As New System.Servi
    ceModel.ServiceHost(GetType
    (WcfServiceLibrary1.Service1))  
  4. host.AddServiceEndpoint(GetType
    (WcfServiceLibrary1.IService1), 
    New System.ServiceModel.Basic
    HttpBinding(), url)  
  5. host.Open()  
  6. Console.WriteLine(host.State.ToString)  
  7. Dim c = New System.ServiceModel
    .ChannelFactory(Of WcfServiceL
    ibrary1.IService1)(New System.
    ServiceModel.BasicHttpBinding, url)  
  8. Dim s As WcfServiceLibrary1.
    IService1
     = c.CreateChannel  
  9. Try  
  10. Console.WriteLine(s.GetData(1))  
  11. Catch ex As System.ServiceModel.
    FaultException(Of WcfService
    Library1.Fault)  
  12. Console.WriteLine("错误代码:" 
    & ex.Detail.ErrorCode)  
  13. Console.WriteLine("错误消息:" 
    & ex.Detail.Message)  
  14. End Try  
  15. Console.ReadKey()  
  16. End Sub 

 

这样WCF异常信息,就可以传递到客户端了。

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-21 13:27:45

WCF服务配置信息

2009-12-07 15:02:46

WCF学习

2009-12-22 19:26:51

WCF绑定

2009-12-22 13:48:09

引用WCF服务

2010-02-22 17:58:06

WCF异步上传

2009-12-22 18:18:11

WCF客户端编程

2011-05-16 09:30:30

jQueryWCF

2010-02-24 11:22:04

WCF方法重载

2010-02-22 11:10:17

WCF获取客户端IP

2017-07-27 17:37:44

MySQL死锁日志

2010-01-13 18:09:09

VB.NET动态生成代

2010-02-22 13:35:03

WCF异常处理

2009-12-07 18:33:31

WCF Service

2009-12-08 16:42:48

WCF Service

2010-03-02 17:48:35

WCF寻址报头

2009-11-09 17:06:38

WCF选择绑定

2010-01-07 15:29:59

VB.NET表达式

2009-11-09 10:10:13

WCF异常

2009-12-07 18:38:16

WCF异常

2009-11-05 12:45:25

WCF异常
点赞
收藏

51CTO技术栈公众号