Returning a error message from within a .NET web service in C#

Please advise sample code for returning a error message from within a .NET web service in C#? . Thanks.


Please mark the post(s) that have helped you as "Answer"
0
thuhue
10/5/2007 7:07:56 AM
📁 asp.net.xml-web-services
📃 7071 articles.
⭐ 0 followers.

💬 2 Replies
👁️‍🗨️ 2233 Views

refer the below article

http://msdn2.microsoft.com/en-us/library/ds492xtk(VS.71).aspx

 


Thanks
Sku
Please remember to click €œMark as Answer€ on the post that helps you.
This can be beneficial to other community members reading the thread.
0
skurocks
10/5/2007 7:38:51 AM

Hi thuhue,

You can throw a SoapException in your webservice and add some information in it and catch the exception in your client . Here is my codes below, hope it is helpful to you.

 

1    [WebMethod]
2            public string HelloWorld()
3            {
4                string myNS = "Microsoft.Samples.XmlMessaging.WebServices.SoapExceptionSample";
5                XmlDocument doc = new XmlDocument();
6                XmlNode detail = doc.CreateNode(XmlNodeType.Element, SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace);
7                XmlNode errorType = doc.CreateNode(XmlNodeType.Element,"ErrorType",myNS);
8                errorType.InnerText = "Validation";
9    
10               XmlNode lineNum = doc.CreateNode(XmlNodeType.Element,"Line",myNS);
11               lineNum.InnerText = "24";
12   
13               detail.AppendChild(errorType);
14               detail.AppendChild(lineNum);
15   
16               string errorMsg = "this is errorMsg";
17   
18               SoapException exc = new SoapException(errorMsg, SoapException.ClientFaultCode, "", detail);
19               throw exc;
20   
21               return "Hello World";
22           }
23   
24   private void button1_Click(object sender, EventArgs e)
25           {
26               localhost.Service1 ser = new WindowsApplication1.localhost.Service1();
27   
28               try
29               {
30                   string res = ser.HelloWorld();
31               }
32               catch(SoapException soapExc)
33               {
34                   string message = soapExc.Message;
35                   
36                   string show = soapExc.Detail.OuterXml;
37               }
38           }
Please remember to click €œMark as Answer€ on the post that helps you.
 
0
zhangming870
10/9/2007 5:47:38 AM