简体   繁体   中英

IErrorHandler WCF configure to return text/plain Raw

I'm trying to impement custom error handler fo rest endpoin in wcf service to return unwrapped string on error

        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;
    }

This code results in response with header "text/plain" but error message is still is serialized to xml

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

When I add raw formatting to created message

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

service stops to return at all. What is the way to return unwrapped string error from wcf? There is an internal StringMessage class derived from ystem.ServiceModel.Channels, mb I can instantiate it somehow?

Try to use an approach which is presented in the article:

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();
    }
}

In my opinion, I think it could not be attained by the current requirement, WCF is a type of web service based on the SOAP, which defines the transfer format during communication. What is SOAP?

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

WCF client-server communication is established by binding, which means that all bindings specify the format of the request as xml., but in order that the server could understand the meaning of communication, the body of the message is still SOAP format, that is XML.

You can define the format of the response to make it look like text, and we know that the Chrome browser expects the response format to be application/JSON by default, if you define the format of the response is JSON, then the browser renders the text. 在此处输入图片说明 在此处输入图片说明

but if you want to return the pure text string, I don't think that is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM