Asdfg
Asdfg

Reputation: 12253

Error: Cannot process the message because the content type 'application/json; charset=utf-8 was not the expected type

I am trying to invoke WCF service using jQuery and getting this error:

"Cannot process the message because the content type 
'application/json; charset=utf-8' was not the expected type 'multipart/related; 
type="application/xop+xml"'."

This is how my WCF service looks like:

Interface:

public interface IService
{

    [OperationContract]
    [WebInvoke(Method = "POST",
      ResponseFormat = WebMessageFormat.Json)]
    PhotoServiceResponse GetPhoto();

}

[DataContract]
public class PhotoServiceResponse
{
    [MessageBodyMember]
    public Byte[] Photo { get; set; }
}

Implementation:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
    #region IService Members


    public PhotoServiceResponse GetPhoto()
    {
        PhotoServiceResponse response = new PhotoServiceResponse();
        response.Photo = File.ReadAllBytes(@"C:\Temp\SomePic.bmp");
        return response; 
    }

    #endregion
}

Config:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WsHttpMtomBinding" maxReceivedMessageSize="5242880" messageEncoding="Mtom">
          <readerQuotas maxStringContentLength="655360" maxArrayLength="1310720" maxNameTableCharCount="1310720" maxBytesPerRead="327680" />
        </binding>

      </wsHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="svcBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

    <services>
      <service behaviorConfiguration="svcBehaviour"
               name="Service">
        <endpoint address="" binding="wsHttpBinding"
           contract="IService"
           bindingConfiguration="WsHttpMtomBinding"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8081/Service" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

This is how i am trying to access this service using jQuery AJAX:

   <script type="text/javascript">
    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "http://localhost:8081/Service/GetPhoto",
      data: "{}",
      crossDomain: true,
      dataType: "json",
      success: function (msg) {
            alert(msg);
      },
      error: function(jqXHR)
      {
        alert(jqXHR.statusText);
      }
    });

    </script>

What is the reason i am getting this error? How do i fix it?

Any help will he highly appreciated.

Upvotes: 4

Views: 10992

Answers (3)

Maurizio In denmark
Maurizio In denmark

Reputation: 4284

Try to add this on top of your class:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

I had the same problem and it fixed it.

Upvotes: 1

VJAI
VJAI

Reputation: 32768

I think you are facing some exception because you are doing some mistakes in binding and others.

For REST communication in WCF, you should use the webHttpBinding and not wsHttpBinding. Second you should mark the Photo property with DataMember.

  [DataContract]
  public class PhotoServiceResponse
  {
    [DataMember]
    public Byte[] Photo { get; set; }
  }

And you have to use the System.ServiceModel.Activation.WebServiceHostFactory in the svc file.

Ex.

<%@ ServiceHost Service="Service1" 
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Once you fixed this issues at-least you see some response in the client side and if the service is running in different domain make sure you have done enough settings in the service side for cross site communication.

Upvotes: 4

devstruck
devstruck

Reputation: 1507

Unless I'm really misreading your code, it looks like you try to return over Ajax a file as a byte array. This presents a couple of problems. First of all, as described in the jQuery API reference for Ajax return types, it appears you can't return files through Ajax using jQuery's built-in Ajax object. Second (and a bit more subjective), why aren't you just returning a URL to the file, especially if it happens to be a image? Unless you have a compelling reason, I'd just return the file URL in your Ajax result.

Upvotes: 1

Related Questions