2011-11-01 72 views
0

我有一個WCF REST服務,它作用於POST,接收XML並返回另一個。 截至目前,其他獲得XML的方法正常工作。反序列化XElement時WCF REST問題

看來的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。

我檢查過其他問題,但似乎沒有像這樣受到影響。

在此先感謝!

+0

沒關係,我發現我試圖執行的服務是用一箇舊版本發佈的,它沒有參數上的XElement,而是一個類,所以它不會起作用。 我很抱歉浪費它可能造成的時間! – viniciushana

+1

請發表評論作爲答案並接受它,讓人們更容易跳過這個問題。 – carlosfigueira

+0

是的,我試過了,但那時我不能,所以告訴我等6個小時。他們已經通過了,謝謝提醒。 – viniciushana

回答

1

沒想到,我發現我試圖執行的服務是用一箇舊版本發佈的,它沒有參數上的XElement,而是一個類,所以它不會起作用。

我很抱歉浪費它可能造成的時間!