Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

How to return response from webservice

I have made a web service and clients will call its methods. Suppose one method takes four parameters and I want to check that if any one of these is empty I want to send a response back to the client and I want to send it like an alert so client knows what argument is empty. I can not force client to give all parameters accurate. Is this possible?

[WebMethod]
Public void Method1(string str1,string str2,string str3,string str4)
{
    if((str1=="") || (str2==""))
    {
        //send response back to client in form of an alert
    }
}

Upvotes: 2

Views: 1817

Answers (3)

u936293
u936293

Reputation: 16264

You said you can't control the person calling the web method. As the method is of type void, the user will only know if something drastic happens. How about throwing an exception, and the error will propagate, to what detail it depends on the settings of the web server hosting the web service and the web server hosting the client (if the client is a web page).

throw new Exception("str1 or str2 in Method1 is empty");

In the "best" case, the user will see the error message "str1 or str2 in Method1 is empty. In the "worst" case, the user will get the standard IIS/ASP.NET error page. Like you said you have no control over the caller, it depends on how the caller codes the client and how he deploys the application. But whatever the case, it gets his attention!

I put best and worst in quotes because best and worst mean different things to different people. For people who want maximum security, best is when the error page says nothing about the error. For people who want user friendliness and fast troubleshooting, best is when the error message has a lot of details and what to do next.

Upvotes: 1

to StackOverflow
to StackOverflow

Reputation: 124794

Don't reinvent the wheel with custom objects. Look at this MSDN article.

The easiest thing is just to throw an exception (sounds like an ArgumentException is appropriate in your case) and the client will receive a SoapException.

Or throw a SoapException yourself if you want more control, such as the ability to set a fault code.

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66499

As far as I know, you can send some agreed-upon object back to the sender, which the sender should be prepared to receive. It could be as simple as a string that contains "OK" or "ERROR", or it could be complex and have an error code, description, severity level, etc. What the sender does with it when they consume it is completely up to them though.

You could, for instance, make one of the fields in your response a severity level (maybe using something like Microsoft's Severity enum), which the sender could then use to determine whether they display an alert to their users or simply log the error somewhere on their system.

Upvotes: 3

Related Questions