我不得不尝试扩展SoapException
以添加两个额外的字符串属性
我有一个Web服务方法应该从CustomSoapException
派生SoapException
,我想在Web服务客户端中捕获CustomSoapException
。但是,当我尝试通过向我的WebMethod添加CustomSoapException
属性向您的Web服务客户端公开[XmlInclude(typeof(CustomSoapException))]
时,我的ASMX
Web服务在启动时失败并显示以下消息:
无法序列化System.Collections.IDictionary类型的成员System.Exception.Data,因为它实现了IDictionary。
如果有人可以告诉我如何在我的Data
内正确序列IDictionary
CustomSoapException
类型的Data
属性,那么它可以正确地向Web服务客户端公开。我甚至不打算在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;
}
}
的代码:
asmx.cs
这是[WebMethod(EnableSession = true)]
[XmlInclude(typeof(CustomSoapException))]
public void testCustomExceptionPassing()
{
throw new CustomSoapException("user message", "developer message","customException");
}
文件中的 WebMethod 代码:
try
{
Srv.testCustomExceptionPassing();
}
catch (SoapException ex) {
string devMessage = (ex as Srv.CustomSoapException).GetDeveloperMessage();
}
Web服务客户端代码:
{{1}}