0

我被困在试图扩展SoapException以添加两个额外的字符串属性。
我有一个应该抛出的 Web 服务方法,CustomSoapExceptionSoapExceptionCustomSoapException在 Web 服务客户端中捕获它。但是,当我尝试通过向我的 WebMethodCustomSoapException添加属性来向 Web 服务客户端公开时,我的Web 服务在启动时失败并显示以下消息:[XmlInclude(typeof(CustomSoapException))]ASMX

无法序列化 System.Collections.IDictionary 类型的成员 System.Exception.Data,因为它实现了 IDictionary。

如果有人可以向我展示如何正确序列化我内部的类型Data属性,以便它可以正确地暴露给 Web 服务客户端。我什至不打算将任何数据放入属性中。也许它可以以某种方式完全从扩展类中完全删除,以避免需要序列化它。IDictionaryCustomSoapExceptionData

这是我的代码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;
    }
}

这是我在文件中的WebMethod代码asmx.cs

[WebMethod(EnableSession = true)]
[XmlInclude(typeof(CustomSoapException))]
public void testCustomExceptionPassing()
{
 throw new CustomSoapException("user message", "developer message","customException");
 }

Web服务客户端代码:

try
{
   Srv.testCustomExceptionPassing();
}
catch (SoapException ex) {
   string devMessage = (ex as Srv.CustomSoapException).GetDeveloperMessage();
}
4

0 回答 0