How to extend SoapException
I am stuck trying to extend SoapException
in order to add two additional string attributes.
CustomSoapException
derived from SoapException
and I want to catch that CustomSoapException
in web service client. But when I try to expose CustomSoapException
to web service client by adding [XmlInclude(typeof(CustomSoapException))]
property to my WebMethod, my ASMX
web service fails upon start-up with the message:
Cannot serialize member System.Exception.Data of type Sy开发者_如何学Gostem.Collections.IDictionary, because it implements IDictionary.
If someone can show me how to properly serialize Data
property of IDictionary
type inside of my CustomSoapException
so it can be correctly exposed to web service client. I don't even intend to put any data inside Data
property. Maybe it can be somehow removed completely from extended class altogether to avoid the need to serialize it.
Here's my code for CustomSoapException
:
[Serializable]
public class CustomSoapException : SoapException, ISerializable
{
private string customExceptionType;
private string developerMessage;
private string userMessage;
public override System.Collections.IDictionary Data
{
get
{
return base.Data;
}
}
public CustomSoapException(): base()
{
}
public string GetDeveloperMessage()
{
return developerMessage;
}
public string GetCustomExType()
{
return customExceptionType;
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
public CustomSoapException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
public CustomSoapException(string usrMessage, string devMessage, string customExType) : base(usrMessage, SoapException.ServerFaultCode)
{
customExceptionType = customExType;
developerMessage = devMessage;
userMessage = usrMessage;
}
}
Here's my WebMethod code inside of asmx.cs
file:
[WebMethod(EnableSession = true)]
[XmlInclude(typeof(CustomSoapException))]
public void testCustomExceptionPassing()
{
throw new CustomSoapException("user message", "developer message","customException");
}
Web service client code:
try
{
Srv.testCustomExceptionPassing();
}
catch (SoapException ex) {
string devMessage = (ex as Srv.CustomSoapException).GetDeveloperMessage();
}
精彩评论