Hi,
I am using VS 2008 for creating a sample WS that returns custom SOAP error message. I have created a client that is a win app that should recieve a message but instead of SOAP exception, the Exception block is triggered. why?
here is my server code for exception
and on my form side.
I am using VS 2008 for creating a sample WS that returns custom SOAP error message. I have created a client that is a win app that should recieve a message but instead of SOAP exception, the Exception block is triggered. why?
here is my server code for exception
Code:
[WebMethod]
public void GetException()
{
try
{
int i = 1;
int j = 0;
int z;
z = i / j;
}
catch (Exception ex)
{
throw RaiseException("GetException", "WSSoapException", ex.Message, "1000", ex.Source, FaultCode.Server);
}
}
public SoapException RaiseException(string uri, string wsNameSpace, string errorMessage,
string errorNumber, string errorSource, FaultCode code)
{
XmlQualifiedName faultCodeLocation = null;
switch (code)
{
case FaultCode.Client:
faultCodeLocation = SoapException.ClientFaultCode;
break;
case FaultCode.Server:
faultCodeLocation = SoapException.ServerFaultCode;
break;
}
XmlDocument faultDoc = new XmlDocument();
XmlNode rootNode = faultDoc.CreateNode(XmlNodeType.Element,
SoapException.DetailElementName.Name,
SoapException.DetailElementName.Namespace);
XmlNode wsException = faultDoc.CreateNode(XmlNodeType.Element, "WSException", wsNameSpace);
XmlNode errorCode = faultDoc.CreateNode(XmlNodeType.Element, "ErrorCode", wsNameSpace);
errorCode.InnerText = errorNumber;
XmlNode errMsg = faultDoc.CreateNode(XmlNodeType.Element, "Message", wsNameSpace);
errMsg.InnerText = errorMessage;
XmlNode errorSourceNode = faultDoc.CreateNode(XmlNodeType.Element, "ErrorSource",wsNameSpace);
errorSourceNode.InnerText = errorSource;
wsException.AppendChild(errorCode);
wsException.AppendChild(errMsg);
wsException.AppendChild(errorSourceNode);
rootNode.AppendChild(wsException);
SoapException soapEx = new SoapException(errorMessage, faultCodeLocation, uri, rootNode);
return soapEx;
}
Code:
try
{
ExceptionRef.Service1SoapClient wsEx = new TestApp.ExceptionRef.Service1SoapClient();
wsEx.GetException();
}
catch (SoapException soapEX)
{
MessageBox.Show(soapEX.Code.ToString());
XmlDocument faultDoc = new XmlDocument();
faultDoc.Load(soapEX.Detail.OuterXml);
XmlNamespaceManager nsManager = new XmlNamespaceManager(faultDoc.NameTable);
nsManager.AddNamespace("errorNS", "WSSoapException");
XmlNode Node = faultDoc.DocumentElement.SelectSingleNode("errorNS:WSException", nsManager);
string errorNumber = Node.SelectSingleNode("errorNS:ErrorCode", nsManager).InnerText;
string errorMessage = Node.SelectSingleNode("errorNS:Message", nsManager).InnerText;
string errorSource = Node.SelectSingleNode("errorNS:ErrorSource", nsManager).InnerText;
MessageBox.Show("Error Number is " + errorNumber);
MessageBox.Show("Error Message is " + errorMessage);
MessageBox.Show("Error Source is " + errorSource);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}