c# - WCF WebHttp service for both XML and JSON choose serializer based on endpoint address -


i'm trying make wcf restful web service return both xml , json. problem it's working xml serialization it's ignoring xml attributes.

as can see in endpoint configuration have 2 address xml , json.

so access url -

for xml -

http://localhost:59310/testservice.svc/xml/getresponse 

for json -

http://localhost:59310/testservice.svc/json/getresponse 

now want use datacontractformat when access json url , use xmlserializerformat xml url. how can achieve this. please help.

this response class 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 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 service contract interface.

namespace wcfmultiformattest {     [servicecontract]     public interface itestservice     {         [operationcontract]                 [webinvoke(method = "get", uritemplate = "/getresponse")]         root getresponse();     } } 

this 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> 


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -