tags:

views:

71

answers:

1

I'm creating a custom binding (mostly for diagnostic purposes initially) that programatically changes thw way WCF messages are encoded. Here's what it looks like:

public class ConfigurableNetTcpBinding : Binding
{
    public enum MessageEncoding
    {
        Text,
        Binary,
        MTOM,
        ByteStream,
        JSON,
    };

    private TcpTransportBindingElement transport;
    private MessageEncodingBindingElement encoding;
    public ConfigurableNetTcpBinding(MessageEncoding encoding = MessageEncoding.Binary, bool enableMessageCounters = false)
        : base()
    {
        EnableMessageCounters = enableMessageCounters;
        Encoding = encoding;
        this.Initialize();
    }

    public override BindingElementCollection CreateBindingElements()
    {
        BindingElementCollection elements = new BindingElementCollection();
        elements.Add(this.encoding);
        elements.Add(this.transport);
        return elements;
    }
    public override string Scheme
    {
        get { return this.transport.Scheme; }
    }

    public long MaxReceivedMessageSize
    {
        get { return transport.MaxReceivedMessageSize; }
        set { transport.MaxReceivedMessageSize = value; }
    }

    public TransferMode TransferMode
    {
        get { return transport.TransferMode; }
        set { transport.TransferMode = value; }
    }

    public MessageEncoding Encoding { get; private set; }

    public bool EnableMessageCounters { get; private set; }

    private void Initialize()
    {
        MessageEncodingBindingElement messageEncoder;
        switch (Encoding)
        {
            default:
            case MessageEncoding.Text:
                messageEncoder = new TextMessageEncodingBindingElement();
                break;

            case MessageEncoding.Binary:
                messageEncoder = new BinaryMessageEncodingBindingElement();
                break;

            case MessageEncoding.MTOM:
                messageEncoder = new MtomMessageEncodingBindingElement();
                break;

            case MessageEncoding.ByteStream:
                messageEncoder = new ByteStreamMessageEncodingBindingElement();
                break;

            case MessageEncoding.JSON:
                WebMessageEncodingBindingElement webEncoder = new WebMessageEncodingBindingElement();
                // Um... what goes here to to configure the webEncoder to serialize with JSON vs POX
                messageEncoder = webEncoder;
                break;
        }

        transport = new TcpTransportBindingElement();
        encoding = EnableMessageCounters ? new CountingEncoderBindingElement(messageEncoder) : messageEncoder;
    }
}

Ignore the CountingEncoderBindingElement... it's just a wrapper for counting bytes on the wire. Basically, I'm trying to figure out how to configure WebMessageEncodingBindingElement so that it always serialized messages as JSON. What am I missing? All the MSDN Docs say this is the encoding element to use for JSON encoding and the docs say to use the Encoding constructor to set that mode, but Encoding does not have any options for JSON... it's all about Unicode.

A: 

Web message format is controlled by WebHttpBehavior. I am interested if you will be able to make it work over net tcp.

Ladislav Mrnka
Interesting. There shouldn't be anything http-specific about the message encoding format... at least I hope not :) I'll keep digging and will surely post the solution if someone doesn't beat me to it.
Simon Gillbee