問題描述
在 wse 中使用自己的異常,而不僅僅是 SoapException (Using own exceptions in wse, not only SoapException)
是否可以通過 Soap 將我自己開發的異常發送到使用 http.sys 的客戶端??
參考解法
方法 1:
To the best of my knowledge unfortunately the answer is no. You cannot build your own custom exceptions on the server side and expect to use them on the client side through WSE. I can't give much technical background as to why (as in why this is not allowed by WSE), but I am sure about my answer because I tested this out.
You can use the approach described in the provided link to return a custom exception that inherits from a System.Web.Services.Protocols.SoapException, however you must capture the exception on the client side as a SoapException since you will not be able to capture it as the custom exception type: http://msdn.microsoft.com/en‑us/library/ms229064.aspx
To recreate the test for your own confirmation do the following:
- Create a test exception class, call it whatever you'd like and make sure it follows the pattern described in the link I provided above (there are code samples provided).
Create a web method that explicitly returns the test exception like so:
'This is in VB.Net <WebMethod()> _ Public Function ThrowTestSoapException() As TestSoapException Return New TestSoapException() End Function
Try to regenerate your client's WSE Library (using WseWsdl3.exe) and you should receive an error message like this one: "Error: Server unavailable, please try later"
That is as far as I could get when trying to create my own transferable Custom Exceptions. Again the only thing I was able to do was return a Custom Exception that inherited from the SoapException class, and caught it on the client side as a SoapException. That is the same method described in the link that "CheGueVerra" pointed out above.
In response to John Saunders's comment above: Yes, if possible move over to WCF, WSE is indeed obsolete. Since this is work related for me and for others asking these questions, making a shift from WSE to WCF would require managerial approval ‑ so some of us cannot make those changes easily ‑ even if we desperately want to.
方法 2:
Yes, you can throw your own exceptions. Any uncaught exception that does not derive from SoapException will be bottled up by the .NET framework into a SoapException. You can derive from SoapException if you want to control how certain parts of the SoapException are formed (for instance, the fault and detail portions).
方法 3:
Like the ASMX services it is based on, WSE has little support for SOAP Faults. You can return them (SoapException with Detail property set), but they won't appear in the WSDL. If received by a WSE client, they will appear as SoapException, not as a custom exception type.
WCF does have full support for SOAP faults, both on the client and the server side.
(by KaiSan、dyslexicanaboko、Zach、John Saunders)