0

I am stuck trying to extend SoapException in order to add two additional string attributes.
I have a Web-Service method that should throw 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 System.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();
}
shA.t
  • 16,580
  • 5
  • 54
  • 111
Tomato Guy
  • 33
  • 7
  • Don't use ASMX web services or clients. Go read about WCF. ASMX is a legacy technology which, BTW, won't allow you to "extend SoapException". – John Saunders Sep 27 '11 at 15:54
  • Sorry can't use WCF, my application should be working with legacy software that uses simple asmx web services. I'm pretty sure you can extend SoapException, the problem is I don't know how to manually serialize IDictionary Data property inside derived class. – Tomato Guy Sep 27 '11 at 15:58
  • You can't extend `SoapException` in any meaningful way - it's not supported in ASMX, and there's no reason not to use WCF with the ASMX services. It's completely compatible. – John Saunders Sep 27 '11 at 16:21
  • See this question regarding serializing Exception classes: http://stackoverflow.com/questions/390051/in-c-how-can-i-serialize-system-exception-net-cf-2-0 – Keith Sep 27 '11 at 16:22
  • I think that link gives solutions about serializing Exception classes in the context of Message Queues, not asmx webservices. Anyways I've added actual source code for better understanding what I'm trying to do. – Tomato Guy Sep 27 '11 at 16:46

0 Answers0