Click here to Skip to main content
15,029,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,

I am trying to create json enabled wcf services with entity framework 4.0.

when i return the response in XML format using
C#
[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

C#
[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
Comments
Oleksandr Kulchytskyi 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 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 15-Aug-12 0:03am
   
Here is the implementation of my method...

public List<student> getStudents()
{
using (StudentsEntities ctx=new StudentsEntities())
{
return (from s in ctx.Students select s).ToList();
}
}

Check you binding first. Use webHttpBinding instead of basicHttpBinding, when you are using jSon as response type.
   
Comments
A_sachin 15-Aug-12 0:01am
   
yes , i have done all the changes that are required to convert WCF into REST service...!!
hi i got the solution for my problem...i just need to make some changes in web.config file for my endpoint behavior...

C#
<webhttp defaultoutgoingresponseformat="Json" automaticformatselectionenabled="True" />


and now it is working well...Thnks for your consideration..

and in order to serialize the list<students>() into Json i used JavaScriptSerializer

C#
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)




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