WCF服务协定同时要XML和Json序列化 [英] WCF Service contract to be both XML and Json serialized
问题描述
如何在WCF RESTful服务中以 XmlSerializerFormat 和 WebMessageFormat.Json 的形式创建服务合同.
我需要从ASP.Net 1.1后面的代码中调用"CallADSWebMethod"操作协定,该协定需要进行XML序列化和来自Json序列化的jQuery ajax.
服务合同
[ServiceContract,XmlSerializerFormat]公共接口IService{[OperationContract,XmlSerializerFormat][WebInvoke(UriTemplate ="/CallADSWebMethod",方法="POST",BodyStyle = WebMessageBodyStyle.WrappedRequest,ResponseFormat = WebMessageFormat.Json)]VINDescription CallADSWebMethod(string vin,string styleID);}
端点信息
<端点地址=基本"binding ="basicHttpBinding"name ="httpEndPoint"contract ="ADSChromeVINDecoder.IService"/>< endpoint address ="json"binding ="webHttpBinding"behaviorConfiguration ="webBehavior"name ="webEndPoint"contract ="ADSChromeVINDecoder.IService"/>< endpoint contract ="IMetadataExchange"binding ="mexHttpBinding"address ="mex"/>
您可以做的就是这样指定您的Web服务:
[OperationContract][WebInvoke(Method ="POST",ResponseFormat = WebMessageFormat.Xml,BodyStyle = WebMessageBodyStyle.WrappedRequest,UriTemplate ="/CallADSWebMethod")][WebInvoke(Method ="POST",ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.WrappedRequest,UriTemplate ="/CallADSWebMethod")]VINDescription CallADSWebMethod(string vin,string styleID);}
但是,我建议您指定两个不同的端点:一个用于 XML 序列化数据,另一个用于> JSON 序列化的数据.伙计,伙计,您正在使用 REST体系结构 .....为什么不充分利用它?
How can I create the service contract to be in XmlSerializerFormat as well as WebMessageFormat.Json within a WCF RESTful service.
What I need is to call the "CallADSWebMethod" operation contract from code behind of ASP.Net 1.1 which needs to be XML serialized and from jQuery ajax which is Json serialized.
SERVICE CONTRACT
[ServiceContract, XmlSerializerFormat]
public interface IService
{
[OperationContract, XmlSerializerFormat]
[WebInvoke(UriTemplate = "/CallADSWebMethod",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json)]
VINDescription CallADSWebMethod(string vin, string styleID);
}
Endpoint Info
<endpoint address="basic"
binding="basicHttpBinding"
name="httpEndPoint"
contract="ADSChromeVINDecoder.IService" />
<endpoint address="json"
binding="webHttpBinding"
behaviorConfiguration="webBehavior"
name="webEndPoint"
contract="ADSChromeVINDecoder.IService" />
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
What you can do is specify your Web Service like this:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = ""/CallADSWebMethod"")]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = ""/CallADSWebMethod"")]
VINDescription CallADSWebMethod(string vin, string styleID);
}
However, what I would suggest you to do is specify 2 different endpoints: one for the XML serialized data and another for JSON serialized data. Come on dude, you are using the REST architecture.....why not make full use of it??!
这篇关于WCF服务协定同时要XML和Json序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!