XML和JSON的WCF WebHttp服务根据端点地址选择序列化程序

我正在尝试创建一个WCF RESTful Web服务,它将返回XMLJSON 。 问题是它正在工作,但对于XML序列化,它忽略了XML属性。

正如您在端点配置中看到的,我有两个地址,如xmljson

所以我访问的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的服务配置。