【问题标题】:WCF CustomBehavior set JSON/stream for contextWCF CustomBehavior 为上下文设置 JSON/流
【发布时间】:2016-12-25 14:25:14
【问题描述】:

与这个问题类似,我需要发回 JSON。 WCF ResponseFormat For WebGet

但我正在从 BizTalk 2010 SendPort 适配器调用的 WCF 行为中工作。

我在这个方法里面:

public object BeforeSendRequest(ref Message request, IClientChannel channel)

所以我有我可以操纵的消息和频道。

我认为方向是这样的:

1) //WebOperationContext.Current.OutgoingResponse.ContentType = “text/plain”;

2) OperationContext.Current.... 一些东西 - 但我不太了解对象模型。

我目前正在使用 MemoryStream:

byte[] byteArrayJSON = Encoding.UTF8.GetBytes(strJSON);
MemoryStream memStreamJSON = new MemoryStream(byteArrayJSON); 
//WebOperationContext.Current.OutgoingResponse.ContentType = “text/plain”;
Message newMessage = Message.CreateMessage(MessageVersion.None, "", memStreamJSON);
... 
request = newMessage;  // substitute my new message for the original one.

我的标题是这样的:

Content-Type: application/json
Accept: application/json

【问题讨论】:

    标签: json wcf stream


    【解决方案1】:

    我认为这些是我需要的行,但仍在测试...

    WebBodyFormatMessageProperty bodyFormat = new WebBodyFormatMessageProperty(WebContentFormat.Json);
    newMessage.Properties.Add(WebBodyFormatMessageProperty.Name, bodyFormat);
    

    看起来现在我应该传递 XML,这会导致序列化发生吗?我现在正在处理我的下一个错误:

    System.ArgumentException: Encountered unexpected namespace 'http://schemas.datacontract.org/2004/07/System.IO'. The namespace must be empty.
    Parameter name: ns
    

    要尝试“原始”:

    WebBodyFormatMessageProperty bodyFormat = new WebBodyFormatMessageProperty(WebContentFormat.Json);
    newMessage.Properties.Add(WebBodyFormatMessageProperty.Name, bodyFormat);
    

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 1970-01-01
      • 2012-09-24
      • 2023-03-26
      • 2014-11-22
      • 1970-01-01
      • 2013-09-16
      • 2018-06-26
      • 1970-01-01
      相关资源
      最近更新 更多