これは残念なことにサポートされていません。同様のニーズがあり、内部のメンバーをリフレクションで呼び出すことでこれを行いました。私たちはエラーハンドラでそれを使用しています(rawリクエストをダンプすることができるように)。しかし正常に動作します。私は、あなたが所有していないシステム(例えば、このコードを顧客に出荷しない)に対しては、サービスパックなどでいつでも変更できるため、このシステムをお勧めしません。だから、
public static string GetRequestBody()
{
OperationContext oc = OperationContext.Current;
if (oc == null)
throw new Exception("No ambient OperationContext.");
MessageEncoder encoder = oc.IncomingMessageProperties.Encoder;
string contentType = encoder.ContentType;
Match match = re.Match(contentType);
if (!match.Success)
throw new Exception("Failed to extract character set from request content type: " + contentType);
string characterSet = match.Groups[1].Value;
object bufferedMessage = operationContextType.InvokeMember("request",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField,
null, oc, null);
//TypeUtility.AssertType(bufferedMessageType, bufferedMessage);
object messageData = bufferedMessageType.InvokeMember("MessageData",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty,
null, bufferedMessage, null);
//TypeUtility.AssertType(jsonBufferedMessageDataType, messageData);
object buffer = jsonBufferedMessageDataType.InvokeMember("Buffer",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty,
null, messageData, null);
ArraySegment<byte> arrayBuffer = (ArraySegment<byte>)buffer;
Encoding encoding = Encoding.GetEncoding(characterSet);
string requestMessage = encoding.GetString(arrayBuffer.Array, arrayBuffer.Offset, arrayBuffer.Count);
return requestMessage;
}
聖なる牛!それは私の解決策よりもさらに悪いです:-) –
合意 - メリットは、すべての要求に対してメッセージインスペクタを使用してトランスポート層から2番目のコピーを密輸する必要がないことです。こうすることで、サービスコードから直接元のバッファにアクセスすることができます。したがって私の元の注意。 :)私は彼らがWebOperationContextからそれを公開することを望んでいますが、それを分けて、なぜ彼らは(なぜあなたが任意のサイズのストリーミングされた要求を考慮する場合)特にそうでないのか分かります。 – nitzmahone
返信いただきありがとうございます。私は今あなたがこのアプローチをとっている理由を理解しています。 WCFがどのように機能するのかを理解するには、実装を掘り下げる必要があります。それは、複雑さを抽象化しようとする目的を打ち消すものです! –