Web Services best practice question

Hi, some questions about web methods in .Net and best practice

I have two made up to explain my point, one that can throw an exception and one that never does.

void AddEmployee(String employeeFirstName, String employeeSecondName, int age);
int GetEmployeeAgeOrNegative1IfNotFound(String employeeFirstName, String employeeSecondName);

My question is do I handle the exception for every web method in the client with loads of try/catch blocks or to I return magic numbers or an error code somehow? The silly method GetEmployeeAgeOrNegative1IfNotFound never throws an exception but returns a magic number of -1 and AddEmployee might throw an exception for many reasons.

How do people do this & what is the best practice here?

Thank you
John BolterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
It's up to you of how your Web Services will get back to the client when exceptions occur. But in general, you have to handle exception for each and every method.

If you don't want to throw back unhandled exceptions to client, then you have to manually define your own list of return/status codes and then return them back to the client callers to tell about the problem. In my opinion, this should be the best way, we'd better log unhandled/unexpected exceptions for our own, while returning meaningful error codes back to the caller so they know what's wrong and 'being handled' by our service. But of course if your service is only used internally by any other applications developed by you, that isn't really needed since it will require more effort in handling all possible exceptions.

With WCF, you might have better option by using IOperationInvoker (see an example of IOperationInvoker here: https://code.msdn.microsoft.com/windowsdesktop/WCF-Caching-Operation-c43969e9) to help centralizing the generic exception handling at one place - your custom OperationInvoker. And then in all WCF Service methods, you just need to throw the exceptions upon the context or let unhandled/unexpected exceptions get through to your custom OperationInvoker.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.