Web service wrapper in try / catch block

Is it better to use the web service method / call in a try / catch block?

I am not requesting web services, are usually the cause of the failure of .NET applications for the PC? Therefore, I thought that all calls should be wrapped in try / catch to prevent this.

A good idea?

Also, should this be excluded or just have an empty catch?

+5
source share
6 answers

I assume you are using WCF since your question is tagged with it. It’s good practice to handle exceptions with WFC to prevent bubbles through the wire to your consumer, but instead to trigger meaningful FaultExceptions.

try... catch , , . , : , , , . , , , - , .

, FaultExceptions, , , FaultContracts . ... . ... catch, Fault.

, - !

- , WSDL. , , , , .

, .

.

+4

, , .

- "" , , / / , -.

Windows Forms, "" #if DEBUG, .

#if !DEBUG
catch (Exception ex)
{
    // show messagebox, log, etc
}
#endif
+2

, - try-catch. , ( ) . catch , , . , , , (, " - " ) .

+1
using System;
using System.ServiceModel;
using Entities; //my entities
using AuthenticationService; //my webservice reference

namespace Application.SL.Model
{
    public class AuthenticationServiceHelper
    {
        /// <summary>
        /// User log in
        /// </summary>
        /// <param name="callback"></param>
        public void UserLogIn(Action<C48PR01IzhodOut, Exception> callback)
        {
            var proxy = new AuthenticationServiceClient();

        try
        {
            proxy.UserLogInCompleted += (sender, eventargs) =>
            {
                var userCallback = eventargs.UserState as Action<C48PR01IzhodOut, Exception>;
                if (userCallback == null)
                    return;

                if (eventargs.Error != null)
                {
                    userCallback(null, eventargs.Error);
                    return;
                }
                userCallback(eventargs.Result, null);
            };
            proxy.UserLogInAsync(callback);
        }
        catch (Exception ex)
        {
            proxy.Abort();
            ErrorHelper.WriteErrorLog(ex.ToString());
        }
        finally
        {
            if (proxy.State != CommunicationState.Closed)
            {
                proxy.CloseAsync();
            }
        }
        }
}

?

+1

, , , catch try.

, ...

0

- try catch , , , , - -.

, , , , , - , , , .

0
source

All Articles