none
content type error when sending JSON request to WCF service RRS feed

  • Question

  • Hi There,

    I wanting to know how I can make my WCF service support JSON & SOAP+XML? Is there a setting that I need  to add to the config file in order to enable this? The service works fine when  using SOAP but no when  I use JSON.


    Sean - thanks in advance



    HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.
    Date: Wed, 24 Sep 2008 05:16:44 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    Cache-Control: private
    Content-Length: 0


      <system.serviceModel>
        <diagnostics>
          <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
        </diagnostics>
        <services>
          <service behaviorConfiguration="Behave" name="myService.Service">
            <enableWebScript/>
            <endpoint address="" binding="wsHttpBinding" contract="myService.IService" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress = "http://myservername/" />
              </baseAddresses>
            </host>
          </service>
        </services>

        <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
        <behaviors>
          <serviceBehaviors>
            <behavior name="Behave">
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="True" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    Wednesday, September 24, 2008 6:21 AM

Answers

  • You will also need to make sure that your WebInvoke attribute is setup to expect the message type to be json. Such as:

    Code Snippet

     

    [WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Json)]

     

     

     

     

    Wednesday, September 24, 2008 1:38 PM
    Moderator
  • hi There,

    Finally figured it out. I stripped everything out of the config and went back to basics. Here is what i came up with. I have not deployed it to a remote machine as yet but it seems to work fine now with the multiple endpoints and correct configuration of the service contract with invoke attributes.

    Thanks for the help.

    <system.serviceModel>
        <diagnostics>
          <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
        </diagnostics>
        <services>
          <service behaviorConfiguration="serviceBehavior" name="Service.Service">
            <endpoint address="" behaviorConfiguration="jsonEndpointBehavior"
              binding="webHttpBinding" name="json" contract="Service.IService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
            <endpoint address="wsHttpBinding" behaviorConfiguration="wsHttpBinding"
              binding="wsHttpBinding" bindingConfiguration="" name="wsHttpBinding"
              contract="Service.IService">
              <identity>
                <dns value="localhost/wsHttpBinding" />
              </identity>
            </endpoint>
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="jsonEndpointBehavior">
              <enableWebScript />
            </behavior>
            <behavior name="wsHttpBinding" />
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="serviceBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
     
    Monday, September 29, 2008 4:39 AM

All replies

  • Hi,

     You need to add an endpoint that uses the webHttpBinding and add an endpoint behavior configuration with the webHttp behavior.

     <endpoint address="json" binding=webHttpBinding" contract="myService.IService" behaviorConfiguration="jsonBehavior" />
     ...


     <behaviors>
        <endpointBehaviors>
             <behavior name="jsonBehavior">
                 <webHttp />
             </behavior>
         </endpointBehaviors>
     
     ....

     --larsw
    Wednesday, September 24, 2008 8:52 AM
    Moderator
  • You will also need to make sure that your WebInvoke attribute is setup to expect the message type to be json. Such as:

    Code Snippet

     

    [WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Json)]

     

     

     

     

    Wednesday, September 24, 2008 1:38 PM
    Moderator
  • Hi Dan,

    Thanks for the reply. So I have made some changes to the web.config and added the web invoke attribute that you suggested. I was able to browse the service without any issues but I received the first error below.I then added the bodystyle attribue and set it to wrapped like the error message suggested but it came up with the original error.

    Is there anything else I should be looking at?

    Sean


    !-------------------------------------------------------------


      <system.serviceModel>
        <diagnostics>
          <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
        </diagnostics>
        <services>
      <service behaviorConfiguration="Behave" name="myService.Service">

               <endpoint address="" binding="wsHttpBinding" contract="myService.IService" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    behaviorConfiguration="jsonBehavior" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress = "http://myserverr/" />
              </baseAddresses>
            </host>
          </service>
        </services>

        <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
        <behaviors>
          <serviceBehaviors>
            <behavior name="Behave">
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="True" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="jsonBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>

      </system.serviceModel>




    !---------------------------------------------------------





    error with one attribute

     [OperationContract]
            [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
            string[] myoperation(string prefixText, int count);
    !-----------------------------------------------------

    'myoperation' of contract 'IService' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.. ---> System.InvalidOperationException: Operation 'myoperation' of contract 'IService' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.

    !---------------------


    !-=----------------------------------

    HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.

    !-----------------------------
    Thursday, September 25, 2008 6:44 AM
  • Hi,

     

     You have two mex endpoints:

     

             <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> behaviorConfiguration="jsonBehavior" />

     

     One of the should be

     

             <endpoint address="json" binding="webHttpBinding" contract="myService.IService" /> behaviorConfiguration="jsonBehavior" />

     

     --larsw

     

    Thursday, September 25, 2008 8:15 AM
    Moderator
  • Hi Lars,

    I did create another endpoint but still get the same error.

    Sean


    <system.serviceModel>
        <diagnostics>
          <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
        </diagnostics>
        <services>
          <service behaviorConfiguration="Behave" name="Service.Service">

            <endpoint address="" binding="wsHttpBinding" contract="Service.IService" />
            <endpoint address="json" binding="webHttpBinding" contract="Service.IService" behaviorConfiguration="jsonBehavior" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress = "http://myservername8/" />
              </baseAddresses>
            </host>
          </service>
        </services>

        <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
        <behaviors>
          <serviceBehaviors>
            <behavior name="Behave">
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="True" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="jsonBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>

      </system.serviceModel>
    Thursday, September 25, 2008 10:23 PM
  • hi There,

    Finally figured it out. I stripped everything out of the config and went back to basics. Here is what i came up with. I have not deployed it to a remote machine as yet but it seems to work fine now with the multiple endpoints and correct configuration of the service contract with invoke attributes.

    Thanks for the help.

    <system.serviceModel>
        <diagnostics>
          <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
        </diagnostics>
        <services>
          <service behaviorConfiguration="serviceBehavior" name="Service.Service">
            <endpoint address="" behaviorConfiguration="jsonEndpointBehavior"
              binding="webHttpBinding" name="json" contract="Service.IService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
            <endpoint address="wsHttpBinding" behaviorConfiguration="wsHttpBinding"
              binding="wsHttpBinding" bindingConfiguration="" name="wsHttpBinding"
              contract="Service.IService">
              <identity>
                <dns value="localhost/wsHttpBinding" />
              </identity>
            </endpoint>
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="jsonEndpointBehavior">
              <enableWebScript />
            </behavior>
            <behavior name="wsHttpBinding" />
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="serviceBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
     
    Monday, September 29, 2008 4:39 AM
  • Thank for the share .it worked for me
    Tuesday, September 12, 2017 10:10 AM