2011-08-18 52 views
0

我們正在爲客戶端開發一個web服務。我們不是拋出SoapExceptions,所以我們捕獲每個異常服務器端,並返回一個自定義的Exception類。asmx asp.net webservice返回多個類wsdl

Public Class Order 
... 
End Class 

Public Class MyException 
... 
End Class 

然後在我的web服務功能(的WebMethod):

Public Function GetOrder(ByVal id As Integer) As Object 

    Try 
     ... 
     Return New Order() 
    Catch ex As Exception 
     Return New MyException(ex.Message) 
    End Try 

End Function 

現在的問題是,既然我的webmethod是返回類型[對象]。生成的wdsl不包含訂單或異常。

我可以將[Object]更改爲[Order]或[MyException],但只有其中一個在wsdl中生成。

那麼有沒有人有我應該如何處理這個問題的想法?我想在我的wsdl中使用MyException類型和Order類型,但我只是無法讓它工作。

謝謝大家。

回答

2

如果您MyException

Public Class MyException 
     inherits System.Exception 
    ... 
    End Class 

的定義,那麼你不應該需要返回自定義異常,就把它。

,那麼你可以定義

Public Function GetOrder(ByVal id As Integer) As Order 

    Try 
     ... 
     Return New Order() 
    Catch ex As Exception 
     Throw New MyException(ex.Message) 
    End Try 

End Function 

我記得(它已經有一段時間),試圖從一個Web方法返回多個對象可以被證明是極其麻煩

+0

嗨院長,感謝您的評論,我知道我能做到這樣,但問題是,當我拋出異常,它不是我的自定義異常類被序列化併發送給客戶端,但它只是拋出和通用的SoapException。我有點失落在這裏...還有什麼想法? – Vincent

1

如果你真的想返回多個對象,那麼也許你應該建立一個「包裝」對象,例如,像這樣:

'please note: I don't normally use VB.NET, so there might be some errors 
Public Class OrderResponse 

Public Property Order() As Order 
    Get 
     Return m_Order 
    End Get 
    Set 
     m_Order = Value 
    End Set 
End Property 
Private m_Order As Order 

Public Property Exception() As MyException 
    Get 
     Return m_Exception 
    End Get 
    Set 
     m_Exception = Value 
    End Set 
End Property 
Private m_Exception As MyException 
End Class 

然後改變你的方法返回類的一個實例,無論使用哪種設置爲相應值的屬性訂單或例外:

Public Function GetOrder(ByVal id As Integer) As OrderResponse 
    ... 
End Function