WCF异常相关经验分享
创始人
2024-06-17 14:21:04
0

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异常信息,就可以传递到客户端了。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...