Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wcf “错误”;此消息无法支持该操作,因为它已被读取;_Wcf_Wcf Binding - Fatal编程技术网

Wcf “错误”;此消息无法支持该操作,因为它已被读取;

Wcf “错误”;此消息无法支持该操作,因为它已被读取;,wcf,wcf-binding,Wcf,Wcf Binding,我有同样的问题,列在下面的线程 我执行了同一线程中列出的步骤(如下所示) 1) 下载Microsoft WCF示例。 从WF\U WCF\U Samples\WCF\Extensibility\Interop\RouteByBody\CS\service将以下文件添加到项目中 Dispatch By Body Operations Selector.cs DispatchByBodyBehaviorAttribute.cs 2) 将以下属性添加到界面(ServiceContract旁边) 3)

我有同样的问题,列在下面的线程

我执行了同一线程中列出的步骤(如下所示)

1) 下载Microsoft WCF示例。 从WF\U WCF\U Samples\WCF\Extensibility\Interop\RouteByBody\CS\service将以下文件添加到项目中

Dispatch By Body Operations Selector.cs

DispatchByBodyBehaviorAttribute.cs

2) 将以下属性添加到界面(ServiceContract旁边)

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

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
}
at System.ServiceModel.Channels.Message.GetReaderAtBodyContents()
位于System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContent(消息消息,对象[]参数,布尔值isRequest)
位于System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(消息消息,对象[]参数)
位于System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatcheRuntime.ProcessMessage3(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatcheRuntime.ProcessMessage1(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.MessageRpc.Process(布尔isOperationContextSet)
位于System.ServiceModel.Dispatcher.ChannelHandler.DispatchedReleasePump(RequestContext请求、布尔cleanThread、OperationContext currentOperationContext)
位于System.ServiceModel.Dispatcher.ChannelHandler.HandlerRequest(RequestContext请求,OperationContext currentOperationContext)
位于System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult结果)
位于System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult结果)
在System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult结果)中
在System.Runtime.AsyncResult.Complete(布尔同步完成)
在System.Runtime.InputQueue`1.AsyncQueueReader.Set(项)上
在System.Runtime.InputQueue`1.EnqueueAndDispatch(项项项,布尔值canDispatchOnThisThread)
在System.Runtime.InputQueue`1.EnqueueAndDispatch(T项,操作dequeuedCallback,布尔canDispatchOnThisThread)
位于System.ServiceModel.Channels.SingletonChannelAcceptor`3.Enqueue(QueueItemType项、Action dequeuedCallback、Boolean canDispatchOnThisThread)
位于System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext上下文,操作回调)
位于System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(IAsyncResult结果)
位于System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContext(IAsyncResult结果)
在System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult结果)中
在System.Net.LazyAsyncResult.Complete(IntPtr userToken)中
在System.Net.LazyAsyncResult.ProtectedInvokeCallback(对象结果,IntPtr userToken)
在System.Net.ListenerAsyncResult.WaitCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32错误代码,UInt32个字节,本机重叠*pOVERLAP)
类别DispatchByBodyElement操作选择器:IDispatchOperationSelector
{
词典调度词典;
public DispatchByBodyElement操作选择器(字典dispatchDictionary)
{
this.dispatchDictionary=dispatchDictionary
}
#地区选举委员
私有消息CreateMessageCopy(消息消息,XmlDictionaryReader正文)
{
//消息副本=Message.CreateMessage(Message.Version、Message.Headers.Action、body);
//copy.Headers.CopyHeaderFrom(消息,0);
//copy.Properties.CopyProperties(message.Properties);
//返回副本;
MessageBuffer=message.CreateBufferedCopy(Int32.MaxValue);
消息副本=buffer.CreateMessage();
buffer.Close();
copy.Headers.CopyHeaderFrom(消息,0);
copy.Properties.CopyProperties(message.Properties);
返回副本;
}
公共字符串选择操作(参考System.ServiceModel.Channels.Message消息)
{
XmlDictionaryReader bodyReader=message.getReaderBodyContents();
XmlQualifiedName lookupQName=新的XmlQualifiedName(bodyReader.LocalName,bodyReader.NamespaceURI);
message=CreateMessageCopy(message,bodyReader);
if(dispatchDictionary.ContainsKey(lookupQName))
{
返回dispatchDictionary[lookupQName];
}
其他的
{
返回null;
}
}
#端区
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
密封类DispatchByBodyBehaviorAttribute:属性,IContractBehavior
{
#区域IContractBehavior成员
公共void AddBindingParameter
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&amp; rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; 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
}
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 => ");
}
 private Message CreateMessageCopy(ref Message message, XmlDictionaryReader body)
{
...
   message = buffer.CreateMessage();
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;
    }
}