Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi ,


  public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }


   [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebInvoke(Method = "POST",
       BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "GetData")]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


Web.config -

<pre><pre lang="c#"><?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>



Consuming the WCF Service:

C#
int CloseCall1 = 28;

                DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(int));
                MemoryStream mem1 = new MemoryStream();
                ser1.WriteObject(mem1, CloseCall1);
                string data1 = System.Text.Encoding.UTF8.GetString(mem1.ToArray(), 0, (int)mem1.Length);

                WebClient webClient1 = new WebClient();
                //webClient1.Headers["Content-type"] = "text/xml";
                webClient1.Headers["Content-type"] = "application/json";
                webClient1.Encoding = System.Text.Encoding.UTF8;

                string strResponse = webClient1.UploadString("http://125.63.72.168:4444/Service1.svc/GetData", "POST", data1);



What I have tried:

I have created a simple WCF Service and hosted it .
Code of my WCF Service is very simple as given here .
When I am consuming this WCF Service getting an error . I have tried all suggestion available online but could not resolve issue . Please help .

The remote server returned an error: (415) Cannot process the message because the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'..


Can anyone please help me . Stucked badly here thanks in advance .
Posted
Comments
Richard Deeming 6-Aug-18 11:44am    
"the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'

Your WCF service only understands XML. You are trying to send JSON.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900