反序列化XElement时出现WCF REST问题

时间:2011-11-01 12:21:24

标签: .net wcf datacontractserializer wcf-rest

我有一个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。

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

提前致谢!

1 个答案:

答案 0 :(得分:1)

没关系,我发现我试图执行的服务是用旧版本发布的,参数上没有XElement,相反,它是一个类,所以无论如何它都无法工作。

我很抱歉它可能浪费时间!