Trying to add a service reference results in Bad Request (400) in one project, otherwise runs fine

后端 未结 3 1457
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 06:06

I\'m in a delicate situation: As the title suggests, I can\'t seem to connect to a WCF service I wrapped up in a Windows Service. I followed the tutorial http://msdn.microso

3条回答
  •  余生分开走
    2020-12-07 06:46

    For the beginning you have to find an issue, I mean you need to know what happens on server side. You need to handler all error and log them.

    Logging errors

        public class GlobalErrorHanler: IErrorHandler 
        {
            //to use log4net you have to have a proper configuration in you web/app.config
            private static readonly ILog Logger = LogManager.GetLogger(typeof (GlobalErrorHandler));
    
            public bool HandleError(Exception error)
            {
                //if you host your app on IIS you have to log using log4net for example
                Logger.Error("Error occurred on the service side", error);
    
                //Console.WriteLine(error.Message); 
                //Console.WriteLine(error.StackTrace);
    
                return false;
            }
    
            public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
            {
                //you can provide you fault exception here
            }
        }
    

    Then service behavior (inherits Attribute to add possibility to use it as an attribute on service implementation):

    public class MyErrorHandlingBehavior : Attribute, IServiceBehavior
    {
    
        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            return;
        }
    
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher disp in serviceHostBase.ChannelDispatchers)
                disp.ErrorHandlers.Add(new GlobalErrorHanler());
        }
    
        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {        }
    }
    

    And extension element to use it in your config:

    public class ErrorHandlerExtention: BehaviorExtensionElement
    {
    
        public override Type BehaviorType
        {
            get { return typeof(MyErrorHandlingBehavior); }
        }
    
        protected override object CreateBehavior()
        {
            return new MyErrorHandlingBehavior();
        }
    }
    

    Then add to config file:

    
      
        
          
          
        
      
    
    
      
        
          
          
    
          
          
    
        
      
    
    
    
    

    This will allow you to get an error. When you have an error get back to the forum.

    Some more posts with this technique: Error 1, Error 2

    Tracing:

    To turn on tracing you have to add such lines to a config:

    
      
      
        
          
            
          
        
      
    
    

    Tracing tool: Trace Viewer

提交回复
热议问题