none
WCF Service return both SOAP and JSON, deciding it dynamically? RRS feed

  • Question

  • Hello,

    I have to write WCF Web Service with C# which should return SOAP or JSON response.

    Returned Protocol type should be decided in runtime and also this WCF Service should run in Dynamics AX 2012 environment.  Are there any experinced programmers around what could be the handiest way to do it?

    Best Regards,

    Michael

    Thursday, May 15, 2014 12:40 PM

Answers

  • Hi,

    As the @keith said, we can use two kinds of the endpoints to help us to return the SOAP or JSON response.

    Then the web.config file will look like below:

    <system.serviceModel>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
         <services>
          <service behaviorConfiguration="WebServiceBehavior" name="MyService.Service">
            <endpoint address="" behaviorConfiguration="jsonBehavior" binding="webHttpBinding"
              bindingConfiguration="webHttpBindingWithJsonP" contract="MyService.IService" />
            <endpoint address="soap" binding="basicHttpBinding" contract="MyService.IService" />
          </service>
        </services>
    
        <behaviors>
          <endpointBehaviors>
            <behavior name="jsonBehavior">
              <webHttp helpEnabled="true"/>
            </behavior>
          </endpointBehaviors>
    
          <serviceBehaviors>
            <behavior name="WebServiceBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <bindings>
          <webHttpBinding>
            <binding name="webHttpBindingWithJsonP"
                     crossDomainScriptAccessEnabled="true" />
          </webHttpBinding>
        </bindings>
      </system.serviceModel>
    

    For more information, please try to refer to:
    #Consume WCF Service with JSON and SOAP endpoints:
    http://www.codeproject.com/Articles/425702/Consume-WCF-Service-with-JSONP-and-SOAP-endpoints.
    #Exposing WCF services with SOAP and REST endpoints:
    http://jaysongo.wordpress.com/2013/01/02/exposing-wcf-services-with-soap-and-rest-endpoints/ .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Landy_Mic Friday, May 16, 2014 9:56 AM
    Friday, May 16, 2014 5:53 AM
    Moderator

All replies

  • The easiest way would be to publish both SOAP and JSON endpoints on your server and determine which to use dynamically on the client. If it really does have to be dynamic on the server, all I can think of is to dynamically do it when publishing the interface itself (in which case, you have to programmatically publish your contracts et al rather than declaratively)
    Thursday, May 15, 2014 5:49 PM
  • Hi,

    As the @keith said, we can use two kinds of the endpoints to help us to return the SOAP or JSON response.

    Then the web.config file will look like below:

    <system.serviceModel>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
         <services>
          <service behaviorConfiguration="WebServiceBehavior" name="MyService.Service">
            <endpoint address="" behaviorConfiguration="jsonBehavior" binding="webHttpBinding"
              bindingConfiguration="webHttpBindingWithJsonP" contract="MyService.IService" />
            <endpoint address="soap" binding="basicHttpBinding" contract="MyService.IService" />
          </service>
        </services>
    
        <behaviors>
          <endpointBehaviors>
            <behavior name="jsonBehavior">
              <webHttp helpEnabled="true"/>
            </behavior>
          </endpointBehaviors>
    
          <serviceBehaviors>
            <behavior name="WebServiceBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <bindings>
          <webHttpBinding>
            <binding name="webHttpBindingWithJsonP"
                     crossDomainScriptAccessEnabled="true" />
          </webHttpBinding>
        </bindings>
      </system.serviceModel>
    

    For more information, please try to refer to:
    #Consume WCF Service with JSON and SOAP endpoints:
    http://www.codeproject.com/Articles/425702/Consume-WCF-Service-with-JSONP-and-SOAP-endpoints.
    #Exposing WCF services with SOAP and REST endpoints:
    http://jaysongo.wordpress.com/2013/01/02/exposing-wcf-services-with-soap-and-rest-endpoints/ .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Landy_Mic Friday, May 16, 2014 9:56 AM
    Friday, May 16, 2014 5:53 AM
    Moderator