日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
WCF異常相關(guān)經(jīng)驗(yàn)分享

WCF框架作為一款由微軟開(kāi)發(fā)的幫助解決跨平臺(tái)操作的方案,深受開(kāi)發(fā)人員喜愛(ài)。在這里主要介紹一下WCF異常的相關(guān)知識(shí)。希望對(duì)大家有用。#t#

WCF異常將服務(wù)異常(Exception)轉(zhuǎn)換成 SOAP faults,傳遞到客戶(hù)端后再次轉(zhuǎn)換成 Exception。只不過(guò)缺省情況下,我們很難從中獲取有意義的信息。

  1. < ServiceContract()> _
  2. Public Interface IService1
  3. < OperationContract()> _
  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. Throw New Exception("發(fā)生錯(cuò)誤。")
  10. Return String.Format
    ("You entered: {0}", value)
  11. End Function
  12. End Class

拋出來(lái)的WCF異常信息:

接收對(duì) http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/ 的 HTTP 響應(yīng)時(shí)發(fā)生錯(cuò)誤。這可能是由于服務(wù)終結(jié)點(diǎn)綁定未使用 HTTP 協(xié)議造成的。這還可能是由于服務(wù)器中止了 HTTP 請(qǐng)求上下文(可能由于服務(wù)關(guān)閉)所致。有關(guān)詳細(xì)信息,請(qǐng)參閱服務(wù)器日志。

Server stack trace:

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

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

不太好理解

當(dāng)然,WCF 會(huì)提供一個(gè)包裝異常類(lèi) FaultException 來(lái)幫助我們處理這些問(wèn)題。

發(fā)生錯(cuò)誤。

Server stack trace:

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

在 System.Servi

這樣就對(duì)WCF異常好理解多了,所以在服務(wù)端拋出異常時(shí),使用FaultException 異常類(lèi)。

另外,我們還可以通過(guò) FaultContractAttribute 傳遞更詳細(xì)的異常信息給客戶(hù)端。

 
 
 
  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 = "發(fā)生錯(cuò)誤"
  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

客戶(hù)端代碼:

 
 
 
  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("錯(cuò)誤代碼:" 
    & ex.Detail.ErrorCode)
  13. Console.WriteLine("錯(cuò)誤消息:" 
    & ex.Detail.Message)
  14. End Try
  15. Console.ReadKey()
  16. End Sub

這樣WCF異常信息,就可以傳遞到客戶(hù)端了。


本文標(biāo)題:WCF異常相關(guān)經(jīng)驗(yàn)分享
鏈接分享:http://m.5511xx.com/article/dhigdis.html