gpt4 book ai didi

wcf - 错误 "This message cannot support the operation because it has been read"

转载 作者:行者123 更新时间:2023-12-03 01:17:19 25 4
gpt4 key购买 nike

我遇到了以下线程中列出的相同问题。

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) 将以下内容添加到您的服务接口(interface)

[OperationContract(Action = "")]

public void DoNothing()
{
}

4) 对于我的服务,所有消息的 WrapperName 和 Wrappernamespace 均为 null。我必须进入 DispatchByBodyBehaviorAttribute 并编辑 ApplyDispatchBehavior() 以添加以下行来检查这一点:

 if (qname.IsEmpty) {
qname = new XmlQualifiedName(operationDescription.Messages[0].Body.Parts[0].Name, operationDescription.Messages[0].Body.Parts[0].Namespace);
}

现在,我收到一条错误消息“此消息无法支持该操作,因为它已被读取”。我打开跟踪并捕获堆栈跟踪(如下)。如果有人对如何解决这个问题有任何想法,如果您能发表一些评论,我将不胜感激。感谢您的帮助!

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
}

最佳答案

您应该使用MessageBuffer.CreateMessage:

The body of a Message instance can only be consumed or written once. If you wish to consume a Message instance more than once, you should use the MessageBuffer class to completely store an entire Message instance into memory.

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 - 错误 "This message cannot support the operation because it has been read",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com