I'm trying to make a WCF RESTful web service which will return both XML
and JSON
. The problem is that it's working but for XML
serialization it's ignoring the XML
attributes.
As you can see in the endpoint configuration I have two address like xml
and json
.
So I access the url like -
For XML
-
http://localhost:59310/TestService.svc/xml/GetResponse
For JSON
-
http://localhost:59310/TestService.svc/json/GetResponse
Now I would want to use the DataContractFormat
when I access the JSON
url and use XmlSerializerFormat
for XML
url. How can I achieve this. Please help.
This is the response class with XML
attributes.
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> Response { get; set; } } }
This is the service implementation class(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>(); Response r1 = new Response() { Message = "Hello" }; Response r2 = new Response() { Message = "World" }; r.Response.Add(r1); r.Response.Add(r2); return r; } } }
This is the service contract interface.
namespace WCFMultiFormatTest { [ServiceContract] public interface ITestService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/GetResponse")] Root GetResponse(); } }
This is the service configuration in web.config
.
<system.serviceModel> <bindings> <webHttpBinding> <binding name="WebHttpBinding_TestService" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm=""/> </security> </binding> </webHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="JSONRestBehavior"> <webHttp defaultOutgoingResponseFormat="Json" /> </behavior> <behavior name="XMLRestBehavior"> <webHttp defaultOutgoingResponseFormat="Xml" /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> <behavior name="TestServiceBehavior"> <serviceThrottling maxConcurrentCalls="300" maxConcurrentSessions="100" maxConcurrentInstances="2147483647" /> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="TestServiceBehavior" name="WCFMultiFormatTest.TestService"> <endpoint address="XML" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_TestService" contract="WCFMultiFormatTest.ITestService" behaviorConfiguration="XMLRestBehavior"></endpoint> <endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_TestService" contract="WCFMultiFormatTest.ITestService" behaviorConfiguration="JSONRestBehavior"></endpoint> </service> </services> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
No comments:
Post a Comment