错误“此消息无法支持该操作,因为它已被读取”
Posted
技术标签:
【中文标题】错误“此消息无法支持该操作,因为它已被读取”【英文标题】:Error "This message cannot support the operation because it has been read" 【发布时间】:2012-06-17 20:38:41 【问题描述】:我遇到了以下线程中列出的相同问题。
WSDL first WCF server where client does not send SOAPAction
我执行了同一线程中列出的步骤(如下所示)
1) 下载 Microsoft WCF 示例。 从 WF_WCF_Samples\WCF\Extensibility\Interop\RouteByBody\CS\service 将以下文件添加到您的项目中
DispatchByBodyOperationSelector.cs
DispatchByBodyBehaviorAttribute.cs
2) 将以下属性添加到您的界面(在您的 ServiceContract 旁边)
XmlSerializerFormat
DispatchByBodyBehavior
3) 将以下内容添加到您的服务接口中
[OperationContract(Action = "")]
public void DoNothing()
4) 对于我的服务,WrapperName 和 Wrappernamespace 对于所有消息都是空的。我必须进入 DispatchByBodyBehaviorAttribute 并编辑 ApplyDispatchBehavior() 以添加以下行来检查:
if (qname.IsEmpty)
qname = new XmlQualifiedName(operationDescription.Messages[0].Body.Parts[0].Name, operationDescription.Messages[0].Body.Parts[0].Namespace);
现在,我收到一条错误消息“此消息无法支持该操作,因为它已被读取”。我打开了跟踪并捕获了堆栈跟踪(如下)。如果有人对如何解决此问题有任何想法,如果您能发布一些 cmets,我将不胜感激。感谢您的帮助!
at System.ServiceModel.Channels.Message.GetReaderAtBodyContents()
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)
at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)
at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)
at System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.InputQueue`1.AsyncQueueReader.Set(Item item)
at System.Runtime.InputQueue`1.EnqueueAndDispatch(Item item, Boolean canDispatchOnThisThread)
at System.Runtime.InputQueue`1.EnqueueAndDispatch(T item, Action dequeuedCallback, Boolean canDispatchOnThisThread)
at System.ServiceModel.Channels.SingletonChannelAcceptor`3.Enqueue(QueueItemType item, Action dequeuedCallback, Boolean canDispatchOnThisThread)
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
at System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(IAsyncResult result)
at System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContext(IAsyncResult result)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
at System.Net.ListenerAsyncResult.WaitCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
class DispatchByBodyElementOperationSelector : IDispatchOperationSelector
Dictionary<XmlQualifiedName, string> dispatchDictionary;
public DispatchByBodyElementOperationSelector(Dictionary<XmlQualifiedName, string> dispatchDictionary)
this.dispatchDictionary = dispatchDictionary;
#region IDispatchOperationSelector Members
private Message CreateMessageCopy(Message message, XmlDictionaryReader body)
//Message copy = Message.CreateMessage(message.Version, message.Headers.Action, body);
//copy.Headers.CopyHeaderFrom(message, 0);
//copy.Properties.CopyProperties(message.Properties);
//return copy;
MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue);
Message copy = buffer.CreateMessage();
buffer.Close();
copy.Headers.CopyHeaderFrom(message, 0);
copy.Properties.CopyProperties(message.Properties);
return copy;
public string SelectOperation(ref System.ServiceModel.Channels.Message message)
XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
XmlQualifiedName lookupQName = new XmlQualifiedName(bodyReader.LocalName, bodyReader.NamespaceURI);
message = CreateMessageCopy(message,bodyReader);
if (dispatchDictionary.ContainsKey(lookupQName))
return dispatchDictionary[lookupQName];
else
return null;
#endregion
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]
sealed class DispatchByBodyBehaviorAttribute : Attribute, IContractBehavior
#region IContractBehavior Members
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
// no binding parameters need to be set here
return;
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
// this is a dispatch-side behavior which doesn't require
// any action on the client
return;
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.DispatchRuntime dispatchRuntime)
// We iterate over the operation descriptions in the contract and
// record the QName of the request body child element and corresponding operation name
// to the dictionary to be used for dispatch
Dictionary<XmlQualifiedName,string> dispatchDictionary = new Dictionary<XmlQualifiedName,string>();
foreach( OperationDescription operationDescription in contractDescription.Operations )
XmlQualifiedName qname =
new XmlQualifiedName(operationDescription.Messages[0].Body.WrapperName, operationDescription.Messages[0].Body.WrapperNamespace);
if (qname.IsEmpty)
qname = new XmlQualifiedName(operationDescription.Messages[0].Body.Parts[0].Name, operationDescription.Messages[0].Body.Parts[0].Namespace);
dispatchDictionary.Add(qname, operationDescription.Name);
// Lastly, we create and assign and instance of our operation selector that
// gets the dispatch dictionary we've just created.
dispatchRuntime.OperationSelector =
new DispatchByBodyElementOperationSelector(dispatchDictionary);
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
//
#endregion
【问题讨论】:
【参考方案1】:你应该使用MessageBuffer.CreateMessage
:
Message
实例的主体只能使用或写入一次。 如果您希望多次使用 Message 实例,您应该 使用MessageBuffer
类完全存储整个Message
实例到内存中。
http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.messagebuffer.aspx
我当前项目的代码:
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
reply = buffer.CreateMessage();
Message m = buffer.CreateMessage();
LogMessage(m, " Response => ");
为Message
参数添加ref
并返回新消息。
private Message CreateMessageCopy(ref Message message, XmlDictionaryReader body)
...
message = buffer.CreateMessage();
【讨论】:
感谢您发布代码。在我的场景中,我正在重用来自 WCF 示例下载的类。我尝试将 MessageBuffer 代码放在 CreateMessageCopy 方法中,如下所示,但它对我不起作用。 我已经编辑了我的原始问题并发布了我从 WCF 示例中重用的两个类。我注释掉了 CreateMessageCopy() 中的现有代码,并尝试使用 MessageBuffer 创建消息。当我运行我的客户端时,它失败了。 MessageBuffer 缓冲区 = message.CreateBufferedCopy(Int32.MaxValue); 看起来该消息在到达此行之前已被阅读。我不确定应该将 MessageBuffer 代码放在哪里。有任何想法吗?谢谢! 我按照您的建议尝试了 ref,但仍然出现相同的错误。我还尝试在 SelectOperation() 处放置一个断点,并注意到它在第一次到达 MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue) 行时触发了错误。我不知道为什么它说该消息已被阅读,即使它是第一次到达这一行。 您在此处阅读消息 copy.Headers.CopyHeaderFrom(message, 0); copy.Properties.CopyProperties(message.Properties);【参考方案2】:我也遇到了一个非常相似的问题,我也使用了 WCF Samples 中的代码(确切地说是 RouteByBody),并且能够以不同的方式解决它,所以我会在此处发布它以防对任何人有所帮助。
情况: 客户端应用程序(消费者)可以在Release中工作,但是,当附加调试器时,它总是会失败并出现错误“此消息无法支持该操作,因为它已被读取” em>。
经过大量跟踪和记录 WCF 消息后,唯一对我有用的解决方案竟然如此简单:
我的服务托管在 IIS 上,debug="true"
在 web.config 的 <compilation>
部分中。
将其更改为debug="false"
在服务上解决了我所有的问题。
【讨论】:
那么我们将如何调试呢? 这对我们有用。从进程中分离调试器允许消息通过。附加调试器将导致 InvalidOperationException “消息无法支持此操作,因为它已被读取”。此外,该错误似乎仅在DispatchByBodyBehavior
用于服务时发生。【参考方案3】:
如果您正在调试服务,Dmitry Harnitski 的回答不起作用(它会给您“此消息无法支持该操作,因为它已被复制。”错误。)
即使在调试模式下也可以:
XmlDictionaryReader GetReader(ref Message message)
MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue);
message = buffer.CreateMessage();
newMessage = buffer.CreateMessage();
XmlDictionaryReader rv = buffer.CreateMessage().GetReaderAtBodyContents();
buffer.Close();
return rv;
static System.ServiceModel.Channels.Message newMessage = null;
static System.ServiceModel.Channels.Message lastMessage = null;
public string SelectOperation(ref System.ServiceModel.Channels.Message message)
try
if(message == lastMessage)
message = newMessage;
XmlDictionaryReader bodyReader = GetReader(ref message);
lastMessage = message;
XmlQualifiedName lookupQName = new XmlQualifiedName(bodyReader.LocalName, bodyReader.NamespaceURI);
if (dispatchDictionary.ContainsKey(lookupQName))
return dispatchDictionary[lookupQName];
else
return null;
catch(Exception ex)
throw ex;
【讨论】:
以上是关于错误“此消息无法支持该操作,因为它已被读取”的主要内容,如果未能解决你的问题,请参考以下文章
Pig 安装错误:错误 pig.Main:错误 2998:未处理的内部错误