[英]XML format in WCF REST request and response
I have set up a WCF service that will accept both JSON and XML in the same method, and that supports both SOAP and REST. 我已经建立了一个WCF服务,该服务将以相同的方法接受JSON和XML,并且同时支持SOAP和REST。
The JSON works fine, but I do not know how the XML should look. JSON可以正常工作,但是我不知道XML的外观。
The interface looks like this: 该界面如下所示:
[ServiceContract]
public interface IWebService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
string[] EchoArray(string[] stringArray);
}
If possible, I would like to keep the XML as simple as possible, without namespaces, like this: 如果可能的话,我想使XML尽可能简单,没有命名空间,如下所示:
<stringArray>
<string>hello</string>
<string>hola</string>
</stringArray>
The response should be simple as well. 响应也应该很简单。
If it makes any difference, I am doing it all in code, without any web.config. 如果有什么不同,我将用代码完成所有工作,而无需任何web.config。
This is so I can use an Azure worker role. 这样便可以使用Azure辅助角色。
I decided to go with a wrapped request instead of the bare (because another method required it), and figured out how to format it. 我决定使用包装的请求而不是原始请求(因为另一个方法需要它),并弄清楚了如何格式化它。
First I changed the 首先,我改变了
[ServiceContract]
to 至
[ServiceContract(Namespace = "")]
Then this worked: 然后这工作:
<EchoArray>
<stringArray xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>hello</a:string>
<a:string>hola</a:string>
</stringArray>
</EchoArray>
It would probably work without the wrapped request as well, but for consistency I made this method wrapped as well. 它也可以在没有包装请求的情况下工作,但是为了保持一致,我也将此方法包装了。
If you want to control what the XML looks like, you could do this: 如果要控制XML的外观,可以执行以下操作:
[ServiceContract]
public interface IWebService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
StringArray EchoArray(StringArray stringArray);
}
public class StringArray : IXmlSerializable {
public XmlSchema GetSchema() {
return null;
}
public void ReadXml(XmlReader reader) {
// However you have formatted it
}
public void WriteXml(XmlWriter writer) {
// However you want it formatted
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.