Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WCF entity JSON REST
hi,
 
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
Comments
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
good
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.
  Permalink  
Comments
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
good
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;
            }
        }            
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 443
1 Sergey Alexandrovich Kryukov 197
2 PhilLenoir 145
3 RyanDev 125
4 Richard Deeming 120
0 Sergey Alexandrovich Kryukov 6,415
1 OriginalGriff 5,856
2 CPallini 2,473
3 Richard MacCutchan 1,667
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | 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