WCF REST Services - Generic exception handling

前端 未结 2 437
天命终不由人
天命终不由人 2021-02-06 02:18

I have a lot of legacy code that is now a backend for a WCF REST service - it used to be a usual WCF service backend before, if that matters. I want to implement a mechanism tha

2条回答
  •  伪装坚强ぢ
    2021-02-06 03:00

    This what I did in the past

        public class MyServerBehavior : IServiceBehavior {
    
            public void AddBindingParameters(ServiceDescription serviceDescription,
                 ServiceHostBase serviceHostBase, 
                 Collection endpoints, 
                 BindingParameterCollection bindingParameters) {
    
            }
    
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
                                              ServiceHostBase serviceHostBase) {
    
                foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers) {
                    chDisp.IncludeExceptionDetailInFaults = true;
                    if (chDisp.ErrorHandlers.Count > 0) {
                        // Remove the System.ServiceModel.Web errorHandler
                        chDisp.ErrorHandlers.Remove(chDisp.ErrorHandlers[0]);  
                    }
                    // Add new custom error handler
                    chDisp.ErrorHandlers.Add(new MyErrorHandler());
    
                }
    
            }
    
            public void Validate(ServiceDescription serviceDescription, 
                                 ServiceHostBase serviceHostBase) {
            }
    
        }
    

    MyErrorHandler was my class that implemented IErrorHandler.

提交回复
热议问题