Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: WCF entity JSON REST
I am trying to create json enabled wcf services with entity framework 4.0.
when i return the response in XML format using
[WebInvoke(Method = "GET", UriTemplate = "/getStudents", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
the method is working fine and giving the data from the database. but when i convert the same method to return json format using
[WebInvoke(Method = "GET", UriTemplate = "/getString", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
i am not getting any thing. browser displays the default page which asks to check my url.
i have created self tracking entity also..
can any one help me with this problem???
thnks in advance.. please help soon...
Posted 13-Aug-12 6:39am
Oleksandr Kulchytskyi at 13-Aug-12 15:35pm
Try to use ASP.NET WebApi in hot favor of it's flexibility and scalability , it can gives to you more control over JSON , and standart HTTP protocol.
As concersn your issue , show implementation of your method, how you convert it to JSON ? At first sight , with Rest declaration of your WCF, it seems like it fine.... but what behind the scene ?
db7uk at 13-Aug-12 17:56pm
How are you returning the data? Your web invoke attribute is not enough to work out what is going on.
A_sachin at 15-Aug-12 0:03am
Here is the implementation of my method...
public List getStudents()
using (StudentsEntities ctx=new StudentsEntities())
return (from s in ctx.Students select s).ToList();
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Check you binding first. Use webHttpBinding instead of basicHttpBinding, when you are using jSon as response type.
A_sachin at 15-Aug-12 0:01am
yes , i have done all the changes that are required to convert WCF into REST service...!!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

hi i got the solution for my problem...i just need to make some changes in web.config file for my endpoint behavior...
<webhttp defaultoutgoingresponseformat="Json" automaticformatselectionenabled="True" />
and now it is working well...Thnks for your consideration..
and in order to serialize the list() into Json i used JavaScriptSerializer
 public string getStudentArray()
            using (StudentsEntities ctx = new StudentsEntities())
                var oSerializer = new  System.Web.Script.Serialization.JavaScriptSerializer();
                string sJSON = oSerializer.Serialize((from s in ctx.Students select s).ToList());
                return sJSON;

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 598
1 OriginalGriff 280
2 George Jonsson 258
3 CPallini 190
4 Animesh Datta 130
0 OriginalGriff 5,985
1 Sergey Alexandrovich Kryukov 5,341
2 CPallini 4,760
3 George Jonsson 3,400
4 Gihan Liyanage 2,522

Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100