[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Web Services - onSuccess & Errors

Posted on 2009-04-23
9
Medium Priority
?
366 Views
Last Modified: 2012-05-06
I'm learning web services at the moment and have a need to get something into place.

I have produced a basic WebService which is taking 10+ strings and inserting into an SQL table.
[WebMethod]
public string InsertOrder

This as far as I can see allows me to do some checks on what is coming in and return an exception on an error.  But I can only output one error with this.

using XML:SOAP what should I be doing to output multiple errors and should I return a string success on completion or a bool.

Do you have some working code I could just drop into my Service1.asmx.cs file.
I'm not really familiar with having separate classes and referencing them etc.
Is there somethin I could just drop into this file that would output the XML:SOAP errors / success?

Many Thanks.
Andrew.
0
Comment
Question by:andrewmilner
  • 4
  • 3
  • 2
9 Comments
 
LVL 25

Assisted Solution

by:apeter
apeter earned 400 total points
ID: 24221929
Call you web service inside a try catch blocks in your client code. So that if there are any errors you can catch then in ur client code "Catch" block. Below i have sample. Hope it helps.

try
{
      <WebserviceProxyClass> instance = new <WebserviceProxyClass()>;
      string outputmsg = instance.InsertOrder();
      //no exception the flow continues else it goes to catch block
}
Catch(WebException wex)
{
      // you can catch like Timeout, Connection errors
}
Catch(Exception ex)
{
        //Take appropriate action for other than webexception errors.
}
0
 
LVL 6

Accepted Solution

by:
HarryNS earned 1600 total points
ID: 24222010
Best way of doing it is,
public class Service1 : System.Web.Services.WebService
    {
        public callStatus1 ResponseStatus;
 
 
        [WebMethod]
        [SoapHeader("ResponseStatus", Direction = SoapHeaderDirection.Out)]
        public string HelloWorldEx(string name, int val)
        {
            ResponseStatus = new callStatus1();
 
            // Your web service Code
                // ON SUCCESS
                ResponseStatus.statusText = "Success";
                ResponseStatus.statusCode = 0;
                return "Success";
                // ON FAILURE
                ResponseStatus.statusText = "Unauthorized Access";
                ResponseStatus.statusCode = 1;
                return "Failed";
        }
    }
 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/",
    IsNullable = false)]
    public partial class callStatus1 : System.Web.Services.Protocols.SoapHeader
    {
        private string statusTextField;
        private int statusCodeField;
        private System.Xml.XmlAttribute[] anyAttrField;
 
 
        public string statusText
        {
            get
            {
                return this.statusTextField;
            }
            set
            {
                this.statusTextField = value;
            }
        }
        public int statusCode
        {
            get
            {
                return this.statusCodeField;
            }
            set
            {
                this.statusCodeField = value;
            }
        }
        [System.Xml.Serialization.XmlAnyAttributeAttribute()]
        public System.Xml.XmlAttribute[] AnyAttr
        {
            get
            {
                return this.anyAttrField;
            }
            set
            {
                this.anyAttrField = value;
            }
        }
    }
 
 
    //YOUR UI Code
    string result = dhs.HelloWorldEx("blah", 123); 
    int statusCode = dhs.callStatus1Value.statusCode; 
    string statusText = dhs.callStatus1Value.statusText; 
    MessageBox.Show(result); 

Open in new window

0
 

Author Comment

by:andrewmilner
ID: 24223243
I have implemented everything here bar the Your UI Code at the bottom which I couldnt really understand what was for.  whats the dhs referencing.

Also this doesnt appear to be doing anything more than what I was getting with.

catch(exception e)
return(exception.Message)

As it is still only returning one error.
What I'd like to do is to go through my entire strings of input and build the response errors as items within the SOAP response, and then simply return at the end of my web method all of the errors and only return success if error count = 0.

Is that what this code is meant to be doing?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 25

Expert Comment

by:apeter
ID: 24223389
Instead of passing a string, your normal webserice can return a custom class which has all details of the transaction.

The same is done and it is returned in the soap header by andrew. Extend his class "callStatus1" which has more properties to give the more than one result of ur transaction. Like a property which is a array of  statusText and one for Codes

dhs is nothing but in the client, is the name of web service proxy instance name.
0
 
LVL 25

Expert Comment

by:apeter
ID: 24223395
Sorry the soad header sample referred by Harry and not by Andrew.
0
 

Author Comment

by:andrewmilner
ID: 24223678
Could you help me get started in Extending callStatus1 with the array with some example code and example of sending an error from a catch :-)

Many thanks.
0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24223818
1) dhs is just the instance of the webservice.
2) Just you can change this part of your CallStatus1 Class,
private List<string> statusTextField = new List<string>();
        public List<string> statusText
        {
            get
            {
                return this.statusTextField;
            }
            set
            {
                this.statusTextField = value;
            }
        }
3) Whenever you encounter a catch block or wanted to add an error message just add it like,
[WebMethod]
        [SoapHeader("ResponseStatus", Direction = SoapHeaderDirection.Out)]
        public string HelloWorldEx(string name, int val)
        {
            ResponseStatus = new callStatus1();
try
{
//Your Code
// ResponseStatus.statusText.Add("Connection to DB successful"); //Some message you can add in.

}
catch
{
ResponseStatus.statusText.Add("Failed");
ResponseStatus.statusText.Add("Error #1");
ResponseStatus.statusText.Add("Error #2");
}
}
0
 

Author Comment

by:andrewmilner
ID: 24233681
Must i only use one try catch block to make this work? i had a toy around yesterday trying it to output multiple lines in the XML:SOAP output but it still just kept outputting one line in the XML.
0
 

Author Comment

by:andrewmilner
ID: 24237435
Many thanks for all your help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question