Errorhandling in Webservices
-
hey, i've got a webservice with one webmethod. if an exception occurs in this method, i'd like to throw it (and transport it back to the client) as a
SoapException
. my code looks somewhat like this:[WebMethod]
public News[] GetLatestNews(string subscriberKey, int count, string languageName)
{
try {
// do something that throws an exception
}
catch (Exception ex)
{
string msg = "Internal Server Error: " + ex.Message;
string actor = HttpContext.Current.Request.Url.AbsoluteUri;
XmlNode details = BuildDetails(actor, ERR_SERVERERROR, msg);throw new SoapException(msg, SoapException.ServerFaultCode, actor, details);
}
}the method
BuildDetails
creates anXmlNode
with some error information (as you can probably guess by looking at the signature). my problem is: when i start debugging the webservice, the page i get when i invoke the method shows only the error message. without any xml around it. but i'd like to have a proper xml-return which contains all of the information i've placed into theSoapException
. what am I missing here? thanks in advance./matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
hey, i've got a webservice with one webmethod. if an exception occurs in this method, i'd like to throw it (and transport it back to the client) as a
SoapException
. my code looks somewhat like this:[WebMethod]
public News[] GetLatestNews(string subscriberKey, int count, string languageName)
{
try {
// do something that throws an exception
}
catch (Exception ex)
{
string msg = "Internal Server Error: " + ex.Message;
string actor = HttpContext.Current.Request.Url.AbsoluteUri;
XmlNode details = BuildDetails(actor, ERR_SERVERERROR, msg);throw new SoapException(msg, SoapException.ServerFaultCode, actor, details);
}
}the method
BuildDetails
creates anXmlNode
with some error information (as you can probably guess by looking at the signature). my problem is: when i start debugging the webservice, the page i get when i invoke the method shows only the error message. without any xml around it. but i'd like to have a proper xml-return which contains all of the information i've placed into theSoapException
. what am I missing here? thanks in advance./matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
hey, i've got a webservice with one webmethod. if an exception occurs in this method, i'd like to throw it (and transport it back to the client) as a
SoapException
. my code looks somewhat like this:[WebMethod]
public News[] GetLatestNews(string subscriberKey, int count, string languageName)
{
try {
// do something that throws an exception
}
catch (Exception ex)
{
string msg = "Internal Server Error: " + ex.Message;
string actor = HttpContext.Current.Request.Url.AbsoluteUri;
XmlNode details = BuildDetails(actor, ERR_SERVERERROR, msg);throw new SoapException(msg, SoapException.ServerFaultCode, actor, details);
}
}the method
BuildDetails
creates anXmlNode
with some error information (as you can probably guess by looking at the signature). my problem is: when i start debugging the webservice, the page i get when i invoke the method shows only the error message. without any xml around it. but i'd like to have a proper xml-return which contains all of the information i've placed into theSoapException
. what am I missing here? thanks in advance./matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]Looks at the documentation, like I said before I haven't used soapexception. It seems that the xml is only there if you put it there.
public class ThrowSoapException : WebService
{
// This XML Web service method generates a SOAP Client Fault code
[WebMethod]
public void myThrow(){// Build the detail element of the SOAP fault. System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); System.Xml.XmlNode node = doc.CreateNode(XmlNodeType.Element, SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace); // Build specific details for the SoapException. // Add first child of detail XML element. System.Xml.XmlNode details = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo1", "http://tempuri.org/"); System.Xml.XmlNode detailsChild = doc.CreateNode(XmlNodeType.Element, "childOfSpecialInfo", "http://tempuri.org/"); details.AppendChild(detailsChild); // Add second child of detail XML element with an attribute. System.Xml.XmlNode details2 = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo2", "http://tempuri.org/"); XmlAttribute attr = doc.CreateAttribute("t", "attrName", "http://tempuri.org/"); attr.Value = "attrValue"; details2.Attributes.Append(attr); // Append the two child elements to the detail node. node.AppendChild(details); node.AppendChild(details2); //Throw the exception. SoapException se = new SoapException("Fault occurred", SoapException.ClientFaultCode,Context.Request.Url.AbsoluteUri,node); throw se; return; }
}
Ben
-
Looks at the documentation, like I said before I haven't used soapexception. It seems that the xml is only there if you put it there.
public class ThrowSoapException : WebService
{
// This XML Web service method generates a SOAP Client Fault code
[WebMethod]
public void myThrow(){// Build the detail element of the SOAP fault. System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); System.Xml.XmlNode node = doc.CreateNode(XmlNodeType.Element, SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace); // Build specific details for the SoapException. // Add first child of detail XML element. System.Xml.XmlNode details = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo1", "http://tempuri.org/"); System.Xml.XmlNode detailsChild = doc.CreateNode(XmlNodeType.Element, "childOfSpecialInfo", "http://tempuri.org/"); details.AppendChild(detailsChild); // Add second child of detail XML element with an attribute. System.Xml.XmlNode details2 = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo2", "http://tempuri.org/"); XmlAttribute attr = doc.CreateAttribute("t", "attrName", "http://tempuri.org/"); attr.Value = "attrValue"; details2.Attributes.Append(attr); // Append the two child elements to the detail node. node.AppendChild(details); node.AppendChild(details2); //Throw the exception. SoapException se = new SoapException("Fault occurred", SoapException.ClientFaultCode,Context.Request.Url.AbsoluteUri,node); throw se; return; }
}
Ben
hey kubben, thanks for your reply. yes, i knew this already. but i've copy-pasted your code above into my project, just in case i missed something. the same result. the only return i get is the literal: "Fault occurred". i need to return a properly formatted soap response with a couple of details concerning the error? i was already wondering, if IE is "eating" the soap message and reducing it to this simple string. but it seems my IE settings are ok. kind regards
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
hey kubben, thanks for your reply. yes, i knew this already. but i've copy-pasted your code above into my project, just in case i missed something. the same result. the only return i get is the literal: "Fault occurred". i need to return a properly formatted soap response with a couple of details concerning the error? i was already wondering, if IE is "eating" the soap message and reducing it to this simple string. but it seems my IE settings are ok. kind regards
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]You know looking at the overloads for the create of the soapException, I can find one that matches what you are passing in. I am thinking perhaps that is the problem? The only overloads with four parameters are:
public SoapException (
string message,
XmlQualifiedName code,
string actor,
XmlNode detail
)
public SoapException (
string message,
XmlQualifiedName code,
string actor,
Exception innerException
)So I think that is where you need to look. Ben
-
You know looking at the overloads for the create of the soapException, I can find one that matches what you are passing in. I am thinking perhaps that is the problem? The only overloads with four parameters are:
public SoapException (
string message,
XmlQualifiedName code,
string actor,
XmlNode detail
)
public SoapException (
string message,
XmlQualifiedName code,
string actor,
Exception innerException
)So I think that is where you need to look. Ben
but the first one matches the code i've posted:
throw new SoapException(msg, SoapException.ServerFaultCode, actor, details);
regards, matthias
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
but the first one matches the code i've posted:
throw new SoapException(msg, SoapException.ServerFaultCode, actor, details);
regards, matthias
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]I think that SoapException.ServerFaulCode is not a XmlQualifiedName. An XML qualified name is a namespace qualified local name in the format of namespace:localname. Because prefixes are only required when XML is persisted or read, they are irrelevant for XmlQualifiedName objects. This class assumes that prefixes are irrelevant. It is the responsibility of the user to ensure the local name does not contain a ":". Ben
-
but the first one matches the code i've posted:
throw new SoapException(msg, SoapException.ServerFaultCode, actor, details);
regards, matthias
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
but the first one matches the code i've posted:
throw new SoapException(msg, SoapException.ServerFaultCode, actor, details);
regards, matthias
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
I found this link: http://www.developer.com/net/csharp/article.php/10918_3088231_1[^] The only real different between what that article has seems to be the uri they are passing in. Anyway, sorry I haven't been much help. Ben
don't worry about not solving the issue. it's the will that counts. ;) i'll look into the article, thanks! kind regards.
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]