XML和JSON的WCF WebHttp服务根据端点地址选择序列化程序
我正在尝试创建一个WCF RESTful Web服务,它将返回XML
和JSON
。 问题是它正在工作,但对于XML
序列化,它忽略了XML
属性。
正如您在端点配置中看到的,我有两个地址,如xml
和json
。
所以我访问的url如下 –
对于XML
–
http://localhost:59310/TestService.svc/xml/GetResponse
对于JSON
–
http://localhost:59310/TestService.svc/json/GetResponse
现在我想在访问JSON
url时使用DataContractFormat
并使用XmlSerializerFormat
for XML
url。 我怎样才能做到这一点。 请帮忙。
这是具有XML
属性的响应类。
namespace WCFMultiFormatTest { [XmlRoot(ElementName = "RESPONSE")] public class Response { [XmlAttribute(AttributeName = "MESSAGE")] public string Message { get; set; } } [XmlRoot(ElementName = "TEST")] public class Root { [XmlElement(ElementName = "RESPONSE")] public List Response { get; set; } } }
这是服务实现类(svc)。
namespace WCFMultiFormatTest { [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class TestService : ITestService { public Root GetResponse() { Root r = new Root(); r.Response = new List(); Response r1 = new Response() { Message = "Hello" }; Response r2 = new Response() { Message = "World" }; r.Response.Add(r1); r.Response.Add(r2); return r; } } }
这是服务合同界面。
namespace WCFMultiFormatTest { [ServiceContract] public interface ITestService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/GetResponse")] Root GetResponse(); } }
这是web.config
的服务配置。