WCF Custom encoder - Assigning message content type at runtime

1.6k views Asked by At

I'm currently building my own WCF Encoder that will parse my message. I want to dynamically change the content type that is assigned when I'm sending a reply but I can't get my head around where he is looking for it.

Is he using the CustomTextEncoder.ContentType or the content type of the innerencoder? How can I force him to send the response as "text/xml" or "application/soap+xml"?

Thank you in advance.

EDIT - Just to clarify that I need to the decision in the WriteMessage-method of my custom encoder. I'm using a string property that holds the requested content type but he's not doing it correctly.

public class MyEncoder : MessageEncoder
{
    private string _contentType = "application/soap+xml";

    public override string ContentType
    {
        get
        {
            return _contentType;
        }
    }

    public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
    {
        ...

        if (_MY_CONDITION_HERE_)
        {
            _contentType = "multipart/related";
        }
        else
            _contentType = "application/soap+xml";

        ...
     }
}

Unfortunately when I send my message he's still assigning the default value of my encoder...

2

There are 2 answers

0
Tom Kerkhove On BEST ANSWER

It seems like can't change the content type of an inbound request, my problem was fixed by using a Message Inspector where I assign the content type that I want. (This link helped me)

Good to know - The ACKs from the encoder are not passing through the Message Inspector so it uses the ContentType-property.

1
Yaron Naveh On

Yes, you can overide the ContentType in your encoder to control what is being sent. Then you can choose if you want to delegate to the inner encoder or supply your own value.

public override string ContentType
{
    get
    {
        //return this.inner.ContentType;
        //return "text/xml";
    }
}