none
WCF service that accepts requests without specifying Content-Type RRS feed

  • Question

  • Hi people

    I've got a smart device that implements a SOAP 1.1 webservice client. Now I want to implement a WCF service to serve that smart device.

    The transmission protocol used by the device is simple and well described, so I easily managed to create a WCF service with basicHttpBinding.  However, when my smart device accesses the WCF service, it receives the error from the service - "HTTP/1.1 415 Cannot process the message because the content type 'application/octet-stream' was not the expected type 'text/xml; charset=utf-8'."

    I performed some investigations on the network packages that my smart device sends to the service and found that there is the Content-Type setting is missing in the http-header of the request:

    POST /service1 HTTP/1.1
    Host: 192.168.71.1
    User-Agent: SmartDevice1
    Accept: text/xml, application/soap
    Connection: Keep-Alive
    SOAPAction: "urn:Operation1"
    Content-Length: 409
    
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Namespace1">
      <SOAP-ENV:Body>
        <ns1:Operation1>
          <param1>123</param1>
        </ns1:Operation1>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

    When I simulate the same request in web browser including Content-Type, all works like a charm:
    POST /service1 HTTP/1.1
    Host: 192.168.71.1
    User-Agent: SmartDevice1
    Accept: text/xml, application/soap
    Connection: Keep-Alive
    SOAPAction: "urn:Operation1"
    Content-Length: 409
    Content-Type: text/xml; charset=utf-8
    
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Namespace1">
      <SOAP-ENV:Body>
        <ns1:Operation1>
          <param1>123</param1>
        </ns1:Operation1>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

    Please, how can I make my WCF service to accept the requests without Content-Type specified?




    • Edited by Sargay Monday, March 26, 2018 4:55 PM
    Sunday, March 25, 2018 10:42 PM

All replies

  • Hi Sargay,

    What is the .net framework version for WCF Service? If it is earlier than .net 4.0, I suggest you upgrade it to .net 4.0 later.

    As the Http/1.1 indicates,

    Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

    This is the Http Protocol limit, the Http Protocol set content type as ‘application/octet-stream’. I would suggest you change your client to add Content-type header.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 26, 2018 1:41 AM
  • Hello Sargay, 

    I would take a look at extending WCF to have a custom message formatter to add the content type in the pipeline. There some good resources about this, for example: https://blogs.msdn.microsoft.com/carlosfigueira/2011/05/02/wcf-extensibility-message-formatters/

     


    Cheers, Jeff

    Monday, March 26, 2018 2:11 AM
  • Hi Tao,

    thanks for your fast response.

    I use Visual Studio 2017, C# and .Net Framework 4.6.*.

    I cannot change the client behavior, 'cause it's hard-coded in my smart device.

    The only actions I can do here are:
    - throw out the device and then learn how to live without it ;)
    - teach my WCF server understand that incorrect Content-Type from the device.

    I prefer the second option ;) 


    Sargay Kolpakov, Russia


    • Edited by Sargay Monday, March 26, 2018 8:37 PM
    Monday, March 26, 2018 5:04 PM
  • Hi Jeff,

    I tried to use WCF extensions - custom bindings, custom behaviors, message formatters and so on. Unfortunately, I had no success in that.

    The main problem with the WCF extensions I guess is that they work after WCF accepted the incoming messages, but in my case WCF refuses the incorrect incoming message in very early stage, so no chance to make my extensions take part.

    Anyway, thank you for your answer and for the link you have provided.


    Sargay Kolpakov, Russia

    Monday, March 26, 2018 5:11 PM
  • Content-type is incorectly validated at the transport layer, instead of the application layer. I suggest you check whether customing the Transport Channels will work.

    #WCF Extensibility – Transport Channels – Request Channels, part 1

    https://blogs.msdn.microsoft.com/carlosfigueira/2011/12/07/wcf-extensibility-transport-channels-request-channels-part-1/

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 27, 2018 2:23 AM