Search code examples
c#asp.net-mvc-4wcf-rest

Wcf returning ususual response


I am trying to return a list

public List<tblcourse> GetData(string value)
{
    testEntities1 db = new testEntities1();

    int v = Convert.ToInt32(value);
    testEntities1 t = new testEntities1();
    var u = (from g in t.tblcourses
             select new  { g.C_Id,  g.C_Name }).ToList();

    List<tblcourse> lisstt = new List<tblcourse>();

    foreach (var item in u)
    {
        tblcourse b = new tblcourse();
        b.C_Id = item.C_Id;
        b.C_Name = item.C_Name;
        lisstt.Add(b);
    }

    return lisstt;
}

From client when I consume this service , I get the following response from the service when I execute client code

string strServiceUrl1 = "htttp://localhost:1967/Service1.svc/GetData/" + id;

HttpWebRequest objHttpWebRequest1 = WebRequest.Create(strServiceUrl1) as HttpWebRequest;
objHttpWebRequest1.Method = "GET";
objHttpWebRequest1.ContentType = "application/json-urlencoded";

StreamReader onjStreamReader1 = new StreamReader(objHttpWebRequest1.GetResponse().GetResponseStream());
string strResponse1 = onjStreamReader1.ReadToEnd().ToString();

List<Course> cc = JsonConvert.DeserializeObject<List<Course>>(strResponse1);
return View(cc);

The following is the response I am getting as response and because of that I am not able to deserialize the response into a list.

<ArrayOftblcourse xmlns="htttp://schemas.datacontract.org/2004/07/app2" 
                  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <tblcourse>
      <C_Id>1</C_Id>
      <C_Name>DOt Net</C_Name>
   </tblcourse>
   <tblcourse>
      <C_Id>2</C_Id>
      <C_Name>EF</C_Name>
   </tblcourse>
   <tblcourse>
      <C_Id>3</C_Id>
      <C_Name>MVC</C_Name>
   </tblcourse>
   <tblcourse>
      <C_Id>4</C_Id>
      <C_Name>MS SQL</C_Name>
   </tblcourse>
</ArrayOftblcourse>

Contract

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}") ]
List<tblcourse> GetData(string value);

Error while deserializing is

Additional information: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

Autogenerated class file of tbclcourses

[DataContract]
[KnownType(typeof(tblstudent))]
public partial class tblcourse
{
        public tblcourse()
        {
            this.tblstudents = new HashSet<tblstudent>();
        }

        [DataMember]
        public int C_Id { get; set; }

        [DataMember]
        public string C_Name { get; set; }

        public virtual ICollection<tblstudent> tblstudents { get; set; }
}

web.config files webHttpBinding:

<webHttpBinding>
    <binding name="webHttpBinding" 
             maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" 
             transferMode="StreamedResponse">
       <security mode="None">
           <transport clientCredentialType="Basic"></transport>
       </security>
       <readerQuotas maxArrayLength="100000" maxStringContentLength="2147483647" />
   </binding>
</webHttpBinding>

Solution

  • I think your webinvoke should look like this. ( Specify response format)

    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "GetData/{value}",ResponseFormat = WebMessageFormat.Json) ]
    List<tblcourse> GetData(string value);