【问题标题】:IErrorHandler WCF configure to return text/plain RawIErrorHandler WCF 配置为返回 text/plain Raw
【发布时间】:2018-10-24 12:04:58
【问题描述】:

我正在尝试在 wcf 服务中实现自定义错误处理程序,以便在错误时返回未包装的字符串

        public void ProvideFault(Exception error,
        MessageVersion version,
        ref Message fault)
    {
         fault = CreateError(error.Message);
         SetContentType();
    }

    private static void SetContentType()
    {
        if (WebOperationContext.Current != null)
        {
            var response = WebOperationContext.Current.OutgoingResponse;
            response.ContentType = "text/plain";
        }
    }

    private static Message CreateError(string message)
    {
        var fault = Message.CreateMessage(MessageVersion.None, "", message);
        return fault;
    }

此代码导致响应标题为“text/plain”,但错误消息仍被序列化为 xml

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Bad Request</string>

当我向创建的消息添加原始格式时

private static Message CreateError(string message)
        {
            var fault = Message.CreateMessage(MessageVersion.None, "", message);
            fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
            return fault;
        }

服务完全停止返回。从 wcf 返回未包装字符串错误的方法是什么?有一个从 ystem.ServiceModel.Channels, m.b. 派生的内部 StringMessage 类。我可以以某种方式实例化它吗?

【问题讨论】:

    标签: c# rest wcf error-handling


    【解决方案1】:

    尝试使用文章中介绍的方法:

    https://www.codeproject.com/Articles/34632/How-to-Pass-Arbitrary-Data-in-a-Message-Object-usi

    private static Message CreateError(string message)
    {
        var fault = Message.CreateMessage(MessageVersion.None, "", new TextBodyWriter(message));
        fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
        return fault;
    }
    
    // source: https://www.codeproject.com/Articles/34632/How-to-Pass-Arbitrary-Data-in-a-Message-Object-usi
    public class TextBodyWriter : BodyWriter
    {
        byte[] messageBytes;
    
        public TextBodyWriter(string message)
            : base(true)
        {
            this.messageBytes = Encoding.UTF8.GetBytes(message);
        }
    
        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement("Binary");
            writer.WriteBase64(this.messageBytes, 0, this.messageBytes.Length);
            writer.WriteEndElement();
        }
    }
    

    【讨论】:

      【解决方案2】:

      在我看来,我认为目前的要求是达不到的,WCF是一种基于SOAP的Web服务,它定义了通信过程中的传输格式。什么是 SOAP?

      https://msdn.microsoft.com/en-us/library/ms995800.aspx

      WCF客户端-服务器通信是通过绑定建立的,也就是说所有的绑定都将请求的格式指定为xml。但是为了服务器能够理解通信的含义,消息体还是SOAP格式,即 XML。

      你可以定义响应的格式让它看起来像文本,我们知道Chrome浏览器默认期望响应格式是application/JSON,如果你定义响应的格式是JSON,那么浏览器呈现文本。

      但如果你想返回纯文本字符串,我认为这是不可能的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-02
        • 2012-09-07
        • 2016-03-10
        • 2012-05-19
        • 1970-01-01
        • 2016-01-23
        • 2013-11-10
        相关资源
        最近更新 更多