C# Wcf服务使用SOAP,以及.NET4.5.1中的原始Xml和Json
我有一个Wcf服务.NET 4.5.1,我可以使用WcfTestClient.exe连接到该服务,并通过以下方式发送测试对象或(soap)XmlC# Wcf服务使用SOAP,以及.NET4.5.1中的原始Xml和Json,c#,xml,json,wcf,soap,C#,Xml,Json,Wcf,Soap,我有一个Wcf服务.NET 4.5.1,我可以使用WcfTestClient.exe连接到该服务,并通过以下方式发送测试对象或(soap)Xml <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addr
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/PostData</Action>
</s:Header>
<s:Body>
<PostData xmlns="http://tempuri.org/">
<person xmlns:d4p1="PersonNameSpace" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:Id>1</d4p1:Id>
<d4p1:Name>My Name</d4p1:Name>
</person>
</PostData>
</s:Body>
</s:Envelope>
我的方法如下
public string PostData(Person person)
{
//do something with the object
return "Well done";
}
这个很好用。但是现在,我想通过从一个经典的ASP页面传递原始Xml或Json来调用相同的方法PostData作为示例
<PostData>
<person>
<Id>1</Id>
<Name>My name</Name>
</person>
</PostData>
如何将这些数据作为Xml或Json使用,以便使用XmlSerializer或类似的东西
反序列化(PostDataString)
基本上,我想做的是,无论请求是来自使用SOAP的应用程序,还是来自使用基本Xml帖子的网站,将这些数据反序列化到我的对象中。我建议您可以打开两个端点,一个用于Xml,另一个用于json。这是为了在使用REST时更好地练习,也是为了更好地让您的客户消费
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "/PostDataXML")]
string PostDataXML(Person person);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "/PostDataJSON")]
string PostDataJSON(Person person);
然后,您只需将此对象发布到您的服务:
{
"person": {
"Id": "1",
"Name": "My name"
}
}
这是我唯一的选择,因为我将有10个方法,总共是20个。您仍然可以将这两个webinvoke分配给同一个操作契约。这两个端点只是实践性的东西,您可以找到更适合您的模型的东西。我尝试了您的建议,但得到了“System.Net.WebException:远程服务器返回了一个错误:(404)Not Found.”您调用的URL是什么?您需要在路径中的svc文件之后追加/PostData,类似这样:我正在尝试使用oClient.UploadString(“myurl”、“post”、“MyXml”)从C#console应用程序发布到正确的Url;我注意到,如果MyXml是空的,服务就会接受请求。
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "/PostDataXML")]
string PostDataXML(Person person);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "/PostDataJSON")]
string PostDataJSON(Person person);
{
"person": {
"Id": "1",
"Name": "My name"
}
}