Adding global error handling to WCF REST service

前端 未结 1 771
说谎
说谎 2021-01-03 04:49

I have a WCF/REST Web Service that I\'m trying to add a global exception handler to. I\'m looking for something similar to the Application_Error event in a standard .NET web

相关标签:
1条回答
  • 2021-01-03 05:42

    The wiring up of your IServiceBehaviour can be achieved by creating a custom WebServiceHostFactory that overrides CreateServiceHost.

    For example if you have a class GlobalErrorHandlerBehaviour which implements IServiceBehavior, then you could wire it up as follows:

    public class CustomWebServiceHostFactory : WebServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(System.Type serviceType, System.Uri[] baseAddresses)
        {
            return ApplyGlobalErrorHandler(base.CreateServiceHost(serviceType, baseAddresses));
        }
    
        private ServiceHost ApplyGlobalErrorHandler(ServiceHost serviceHost)
        {
            serviceHost.Description.Behaviors.Add(new GlobalErrorHandlerBehaviour());
            return serviceHost;
        }
    }
    

    You would then update your call to the ServiceRoute constructor to pass in this custom factory.

    0 讨论(0)
提交回复
热议问题