代码之家  ›  专栏  ›  技术社区  ›  OfirD

如何在调试时查看wcf rest服务返回的400 http错误请求的原因

  •  0
  • OfirD  · 技术社区  · 6 年前

    这里有很多帖子要求帮助解决400http错误请求。但是,我找不到一篇解释如何获取异常的根本原因的文章。

    假设供应商和客户端都在运行.NET,并且假设我们正在调试:

    1. 如果我是web服务供应商,我如何才能确保客户端收到错误请求的原因? (我尝试用属性修饰web服务类 [ServiceBehavior(IncludeExceptionDetailInFaults = true)] ,但似乎没有效果)。

    2. 如果我是web服务客户机,我怎么能知道错误请求的原因?

    1 回复  |  直到 6 年前
        1
  •  0
  •   mahlatse    6 年前

    您可以尝试在rest服务的错误中包含异常

    catch (Exception ex)
                {
                    Log.Fatal("Could not complete process", ex);
                    throw new WebFaultException<string>(
                           string.Format(ex.Message),
                                HttpStatusCode.BadRequest);
                }
    

    上面的代码将处理这两种情况,假设您启用了日志记录器,您只需获取服务器上的日志文件,客户端就会收到异常消息

    我想你可以给客户端发送一条友好的消息,这样他们就不会关心你的具体实现,或者创建一个你可以调用的类

    WebFaultException &T;

    用。

    编辑2

    如果在收到请求之前遇到问题,可以尝试以下操作

    向服务添加faulthandler

    [System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
        public class GenericErrorHandlingBehavior : Attribute, IErrorHandler, IServiceBehavior
        {
            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
                //throw new NotImplementedException();
            }
    
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                //throw new NotImplementedException();
                // Adds a CoreServiceBehavior to each ChannelDispatcher
                foreach (var channelDispatcherBase in serviceHostBase.ChannelDispatchers)
                {
                    var channelDispatcher = channelDispatcherBase as ChannelDispatcher;
                    channelDispatcher.ErrorHandlers.Add(new GenericErrorHandlingBehavior());
                }
            }
    
            public bool HandleError(Exception error)
            {
                //throw new NotImplementedException();
                return true;
            }
    
            public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
            {
                if (error is FaultException)
                    return;//nothing to do if the error is a fault
    
                //the type cannot be determined[ default to this value]
                var exception = new FaultException<GenericServiceFault>(
                    new GenericServiceFault(), new FaultReason(error.Message));
    
                MessageFault messageFault = exception.CreateMessageFault();
                fault = Message.CreateMessage(version, messageFault, exception.Action);
    
            }
    
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                //throw new NotImplementedException();
            }
        }
    

    这个 GenericServiceFault 类只是一个简单的datacontract类, 然后您可以在 ProvideFaultMethod ,或者 HandleError 方法并向用户提供有意义的错误

    你可以使用 GenericErrorHandlingBehavior 将服务实现作为属性,即[genericerrorhandlingbehavior]

    推荐文章