0

当我尝试使用通用消息处理程序时,如果我使用我自己的类型(例如 text/x-json),我会在接受或内容类型为 html/xml/json 时遇到错误,一切都按预期工作,消息被发送到我的处理程序和流将数据返回给 webclient。我已经使用调试器逐步完成了此操作,并且我的代码成功创建了消息,但是服务总线绑定中的某些内容阻塞并导致服务器不响应。是否需要更改设置以允许 application/json 并使服务总线发送原始数据而不是尝试重新序列化它?

[WebGet( UriTemplate = "*" )]
[OperationContract( AsyncPattern = true )]
public IAsyncResult BeginGet( AsyncCallback callback, object state )
{
    var context = WebOperationContext.Current;
    return DispatchToHttpServer( context.IncomingRequest, null, context.OutgoingResponse, _config.BufferRequestContent, callback, state );
}

public Message EndGet( IAsyncResult ar )
{
    var t = ar as Task<Stream>;
    var stream = t.Result;
    return StreamMessageHelper.CreateMessage( MessageVersion.None, "GETRESPONSE", stream ?? new MemoryStream() );
}
4

1 回答 1

0

而不是使用:StreamMessageHelper.CreateMessage,您可以在更改后使用以下一个:

WebOperationContext.Current.OutgoingResponse.ContentTYpe = "application/json"


public Message CreateJsonMessage(MessageVersion version, string action, Stream jsonStream)
{
    var bodyWriter = new JsonStreamBodyWriter(jsonStream);
    var message = Message.CreateMessage(version, action, bodyWriter);
    message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json));
    return message;
}

class JsonStreamBodyWriter : BodyWriter
{
    Stream jsonStream;
    public JsonStreamBodyWriter(Stream jsonStream)
        : base(false)
    {
        this.jsonStream = jsonStream;
    }

    protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
    {
        writer.WriteNode(JsonReaderWriterFactory.CreateJsonReader(this.jsonStream, XmlDictionaryReaderQuotas.Max), false);
        writer.Flush();
    }
}
于 2012-08-23T18:36:37.543 回答