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 |
![]() |
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 |
![]() |
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 |
![]() |