我遇到的情況,我需要對類對象發送到WCF REST方法輸入下面給出。我們如何將類對象作爲XML傳遞給WCF REST post方法?
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/InsertContact", ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
int InsertContact(ContactType objContactType);
提示以下錯誤:在提琴手
<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"><Code><Value>Receiver</Value><Subcode><Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</Value></Subcode></Code><Reason><Text xml:lang="en-US">Error in deserializing body of request message for operation 'InsertContact'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'InsertContact' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'ContactType' and namespace ''</Text></Reason><Detail><ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException><HelpLink i:nil="true"/><InnerException i:nil="true"/><Message>OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'InsertContact' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'ContactType' and namespace ''</Message><StackTrace> at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)</StackTrace><Type>System.Runtime.Serialization.SerializationException</Type></InnerException><Message>Error in deserializing body of request message for operation 'InsertContact'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'InsertContact' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'ContactType' and namespace ''</Message><StackTrace> at System.ServiceModel.Dispatcher.OperationFormatter.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.CompositeDispatchFormatter.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)</StackTrace><Type>System.ServiceModel.CommunicationException</Type></ExceptionDetail></Detail></Fault>
我想讓我的REST API JSON或XML請求取決於client.Do接受我需要在WebInvoke設置該屬性屬性還是在web.config中?
傳遞以下XML
<ContactType>
<ContactTypeID>3</ContactTypeID>
<Name>Assistant Sales Representative</Name>
<ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
</ContactType>
誰能幫助我對此?
您是否使用了命名空間來驗證?如果沒有,那麼也許只是刪除它 - 然後重試。 –
@AnthonyHorne通過這項XML 3 ContactTypeID> 助理銷售代表 2002-06-01T00:00:00 ModifiedDate> ContactType> –
最近我的服務寫入允許用戶發送txt,XMLElement(或Document)或實際的對象。界面只是單獨定義它們。我需要改變System.Linq的對system.xml(否則沒有工作),並在我的情況,要求我刪除NS(作爲賣方放入一個無效) –