WCF WebHttp Service for XML and JSON picks serializer based on endpoint address

I'm trying to create a web service WCF RESTful, which returns both XML

and JSON

. The problem is that it works, but XML

it ignores attributes for serialization XML

.

As you can see in the endpoint config, I have two addresses like XML

and JSON

.

So, I am accessing the url -

For XML

-

http://localhost:59310/TestService.svc/xml/GetResponse

      

For JSON

-

http://localhost:59310/TestService.svc/json/GetResponse

      

Now I would like to use DataContractFormat

when I speak to the url JSON

and use XmlSerializerFormat

for the XML

url. How can I achieve this. Please, help.

This is a response class with attributes 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> Response { get; set; }
    }
}

      

This is a 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>

      

+3


source to share





All Articles