【问题标题】:WCF REST problems while deserializing XElement反序列化 XElement 时出现 WCF REST 问题
【发布时间】:2011-12-19 11:05:16
【问题描述】:

我有一个 WCF REST 服务,它作用于一个 POST,接收一个 XML 并返回另一个。 到目前为止,其他获取 XML 的方法都可以正常工作。

似乎 DataContractSerializer 无法将 XML 文本反序列化为 XElement,尽管我设法直接在 PoC 上使用 DataContractSerializer 做到了:

static void Main(string[] args)
{
    var stream = File.Open("afile.xml", FileMode.Open); // afile.xml has the same contect as the request below
    var serial = new System.Runtime.Serialization.DataContractSerializer(typeof(XElement));
    var obj = serial.ReadObject(stream); //obj is a XElement instance
}

当我尝试将 XML 发布到我的 REST 服务时,问题是这个异常:

<Exception>
  <ExceptionType>System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
  <Message>Unable to deserialize XML body with root name 'documento' and root namespace 'http://uri.org' (for operation 'Publicar' and contract ('IDocumentos',  'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.</Message>
  <StackTrace>
    at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject(Message message)
    at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
    at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
    at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp;amp; rpc)
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp;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.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.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
    at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
    at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
    at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
    at System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
    at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)
    at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
    at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
    at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
  </StackTrace>
  <ExceptionString>System.Runtime.Serialization.SerializationException: Unable to deserialize XML body with root name 'documento' and root namespace 'http://uri.org' (for operation 'Publicar' and contract ('IDocumentos',  'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.</ExceptionString>
</Exception>

合同:

[ServiceContract]
public interface IDocumentos
{
    [OperationContract]
    [WebInvoke(UriTemplate = "", Method = "POST", 
        RequestFormat = WebMessageFormat.Xml, 
        ResponseFormat = WebMessageFormat.Xml, 
        BodyStyle = WebMessageBodyStyle.Bare)]
    XElement Publicar(XElement documento);
}

通过 Fiddler 发送的请求:

POST http://integracao/documentos/ HTTP/1.1
User-Agent: Fiddler
Host: integracao
Content-Length: 46
Content-Type: text/xml

<documento></documento>

在 Application_Start 中添加路由,创建一个 ServiceHostFactory 并使用它向 RouteTable 添加路由。

我检查了其他问题,但似乎没有一个受到这样的影响。

提前致谢!

【问题讨论】:

  • 没关系,我发现我尝试执行的服务是使用旧版本发布的,该版本在参数上没有 XElement,而是一个类,所以它不起作用反正。对于可能造成的时间浪费,我深表歉意!
  • 请将此评论作为答案发布并接受它,以便人们更容易跳过此问题。
  • 是的,我试过但当时我做不到,所以告诉我等 6 小时。已经过去了,谢谢提醒。

标签: .net wcf datacontractserializer wcf-rest


【解决方案1】:

没关系,我发现我尝试执行的服务是使用旧版本发布的,该版本在参数上没有 XElement,而是一个类,所以无论如何它都不起作用。

对于可能造成的时间浪费,我深表歉意!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    相关资源
    最近更新 更多