简体   繁体   English

WCF自定义编码器-在运行时分配消息内容类型

[英]WCF Custom encoder - Assigning message content type at runtime

I'm currently building my own WCF Encoder that will parse my message. 我目前正在构建自己的WCF编码器,以解析我的消息。 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? 他使用的是CustomTextEncoder.ContentType还是innerencoder的内容类型? How can I force him to send the response as "text/xml" or "application/soap+xml"? 如何强制他将响应发送为“ text / xml”或“ 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. 编辑-只是为了阐明我需要对我的自定义编码器的WriteMessage方法中的决定。 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... 不幸的是,当我发送消息时,他仍在分配编码器的默认值...

Yes, you can overide the ContentType in your encoder to control what is being sent. 是的,您可以在编码器中覆盖ContentType来控制要发送的内容。 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";
    }
}

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. 似乎无法更改入站请求的内容类型,我的问题已通过使用Message Inspector进行了修复,在其中分配了所需的内容类型。 (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. 提提您 -来自编码器的ACK没有通过消息检查器,因此它使用ContentType属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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