Click here to Skip to main content
15,174,232 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;

    public interface IService1

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

        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here

Web.config -

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

    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
          <!-- 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"/>
        <add binding="basicHttpsBinding" scheme="https" />
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <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"/>


Consuming the WCF Service:

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("", "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 .
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
Top Experts
Last 24hrsThis month

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