Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I wrap all my WCF service code in a try catch block?

try
{
    ///// here is the code that might throw erros. If I discover the user is unauthorized I throw a WebFaultException myself to alert the client
}      
catch (WebFaultException ex)
{
    throw ex; //but if I wrap all code in try-catch, I have to rethrow the exception o the status code reaches the client
}
catch (Exception ex)
{
    throw new WebFaultException(ex.Message, HttpStatusCode.InternalServerError);
}

Should I wrap all in a try-catch, or what do you recommend? I use WCF with rest-ful JSON services

like image 286
Ryan Avatar asked Apr 23 '12 22:04

Ryan


People also ask

Why you should not use try catch?

With a try catch, you can handle an exception that may include logging, retrying failing code, or gracefully terminating the application. Without a try catch, you run the risk of encountering unhandled exceptions. Try catch statements aren't free in that they come with performance overhead.

Does Try Catch affect performance?

In general, wrapping your Java code with try/catch blocks doesn't have a significant performance impact on your applications. Only when exceptions actually occur is there a negative performance impact, which is due to the lookup the JVM must perform to locate the proper handler for the exception.

Does try catch make code slower?

This answer will obviously vary from compiler to compiler and application to application, but generally yes — “try and catch” is slower than some of the other canonical alternatives to error handling.

What should I put in catch block C#?

The C# try and catch keywords are used to define a try catch block. A try catch block is placed around code that could throw an exception. If an exception is thrown, this try catch block will handle the exception to ensure that the application does not cause an unhandled exception, user error, or crash the application.


1 Answers

you could do, but it would probably be better to implement IErrorHandler and add it as a behaviour to your service, which will allow your unhandled exceptions to be handled in a single place, so you'll be able to create a fault exception there to return details to the users.

ErrorHandler : IErrorHandler
{
... just implement the handling of errors here, however you want to handle them
}

then to create a behaviour which uses this:

/// <summary>
///   Custom WCF Behaviour for Service Level Exception handling.
/// </summary>
public class ErrorHandlerBehavior : IServiceBehavior
    {
    #region Implementation of IServiceBehavior


    public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }


    public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
                                      Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
        }


    public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        IErrorHandler errorHandler = new ErrorHandler ();

        foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
            {
            var channelDispatcher = channelDispatcherBase as ChannelDispatcher;

            if (channelDispatcher != null)
                {
                channelDispatcher.ErrorHandlers.Add (errorHandler);
                }
            }
        }

    #endregion
    }

Then if you are self hosting you can just add the behaviour programmatically:

 myServiceHost.Description.Behaviors.Add (new ErrorHandlerBehavior ());

if you want to add it via configuration then you need one of these:

public class ErrorHandlerElement : BehaviorExtensionElement
    {
    public override Type BehaviorType
        {
        get { return typeof (ErrorHandlerBehavior); }
        }

    protected override object CreateBehavior ()
        {
        return new ErrorHandlerBehavior ();
        }
    }
}

and then the configuration:

<system.serviceModel>
<extensions>
  <behaviorExtensions>
    <add name="ErrorLogging" type="ErrorHandlerBehavior, ErrorHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<whatever>" />
  </behaviorExtensions>
</extensions>
<bindings>
  <basicHttpBinding>
    <binding name="basicBinding">
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="Service1Behavior" name="Service">
    <endpoint address="" binding="basicHttpBinding"  bindingConfiguration="basicBinding" contract="Service" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="Service1Behavior">
      <serviceMetadata httpGetUrl="" httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <ErrorLogging />  <--this adds the behaviour to the service behaviours -->
    </behavior>
  </serviceBehaviors>
</behaviors>

like image 181
Sam Holder Avatar answered Oct 31 '22 13:10

Sam Holder