我们如何将类对象作为XML传递给WCF REST后的METHOD? [英] How can we pass the class object as XML to WCF REST post METHOD?

查看:70
本文介绍了我们如何将类对象作为XML传递给WCF REST后的METHOD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我需要将类对象作为输入发送到WCF REST方法,如下所示。



I have a scenario where I need to send the class object to the WCF REST method as input as given below.

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/InsertContact", ResponseFormat = WebMessageFormat.Xml,
          RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
        int InsertContact(ContactType objContactType);





在提琴手中获得以下错误





Getting the below error in the Fiddler

<pre lang="xml"><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)&#xD;
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)&#xD;
   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)&#xD;
   at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)&#xD;
   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)&#xD;
   at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)&#xD;
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.ServiceModel.CommunicationException</Type></ExceptionDetail></Detail></Fault>









I想让我的REST API接受JSON或XML请求dep结束于client.Do我需要在WebInvoke属性或web.config中设置此属性吗?



任何人都可以帮我这个吗?





I want to make my REST API to accept either JSON or XML request depending on the client.Do I need to set this property in the WebInvoke attribute or in the web.config ?

Can anyone help me regarding this ?

推荐答案

我通常使用DataContractSerializer序列化对象以获取实际的xml。

其次我使用BodyStyle = WebMessageBodyStyle.Bare



public static string Serialize(ContactType obj)

{

using(MemoryStream memoryStream = new MemoryStream())

{

使用(StreamReader reader = new StreamReader(memoryStream))

{

DataContractSerializer serializer = new DataContractSerializer(obj.GetType()) ;

serializer.WriteObject(memoryStream,obj);

memoryStream.Position = 0;

return reader.ReadToEnd();

} < br $>
}

}



public static ContactType反序列化(字符串xml)

{

使用(Stream stream = new MemoryStream())

{

byte [] data = System.Text.Encoding.UTF8.GetBytes( xml);

stream.Write(data,0,data.Length);

stream.Position = 0;

DataContractSerializer deserializer = new DataContractSerializer(typeof(ContactType));

return(ContactType)deserializer.ReadObject(stream);

}

}
i normally serialize the object with DataContractSerializer to get the actual xml.
secondly i use BodyStyle = WebMessageBodyStyle.Bare

public static string Serialize(ContactType obj)
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamReader reader = new StreamReader(memoryStream))
{
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
}

public static ContactType Deserialize(string xml)
{
using (Stream stream = new MemoryStream())
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(typeof(ContactType));
return (ContactType)deserializer.ReadObject(stream);
}
}


这篇关于我们如何将类对象作为XML传递给WCF REST后的METHOD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆