2

I am new to WCF. I want to get Json formatted data using LINQ Query with Entity Framework. I am not able to return the value.

This is my code in Interface:

[OperationContract]
[WebInvoke(Method = "GET",
 ResponseFormat = WebMessageFormat.Json,
 BodyStyle = WebMessageBodyStyle.Wrapped,
 UriTemplate = "getcontact")]

List<Tbl_Users> JSONDataAll();

This is my class code:

public List<Tbl_Users> JSONDataAll()
{
    var users = (from u in db.Tbl_Users select u);
    return users.ToList<Tbl_Users>();
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Vetri
  • 347
  • 2
  • 6
  • 23
  • How are you calling the service? – DavidG Sep 17 '14 at 09:19
  • Like this i am calling. http://localhost:53835/FslService.svc/getcontact – Vetri Sep 17 '14 at 09:20
  • Please post the `Exception` being thrown and the `DataContract` for `Tbl_Users` class. – toadflakz Sep 17 '14 at 09:21
  • Have you looked at Newtonsoft.Json? You could then use return JsonConvert.Serialize(users) – David Watts Sep 17 '14 at 09:21
  • Are you putting that link in a browser window? You may need to send the header `Accept: application/json` to see it in the browser. – DavidG Sep 17 '14 at 09:22
  • @David Watts. Yes i am putting that link in a browser. – Vetri Sep 17 '14 at 09:24
  • @David Watts. can you please tel me where i need to send the header – Vetri Sep 17 '14 at 09:25
  • You can't do it with a browser, use a tool like Fiddler (every good web developer should use this! :) – DavidG Sep 17 '14 at 09:28
  • while returning my data, the url becoming like this http://www.localhost.com:53835/FslService.svc/getcontact and showing error like Server not found. I am not able to find how the localhost.com is adding to the url – Vetri Sep 17 '14 at 09:33
  • Oh, you don't need the `.com`, just use `localhost:53835/FslService.svc/getcontact`. – DavidG Sep 17 '14 at 09:39
  • No, i am putting the link like this localhost:53835/FslService.svc/getcontact . After that LINQ query executes and returns, the browser url becoming like this http://www.localhost.com:53835/FslService.svc/getcontact. – Vetri Sep 17 '14 at 09:44
  • In relation to returning C# classes as a JSON string. http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4 – Luke Narramore Sep 17 '14 at 10:01

2 Answers2

0

try call instead of localhost:53835/FslService.svc/getcontact

use the following method

https://127.0.0.0/FslService.svc/getcontact 

Perhaps this will help

Razack
  • 1,826
  • 2
  • 16
  • 37
  • Is there any particular reason to use this? – Vetri Sep 17 '14 at 10:16
  • Look at the following http://www.codeproject.com/Articles/627082/How-to-Consume-WCF-Service-using-JavaScript-Proxy or http://www.bendewey.com/index.php/186/using-jsonp-with-wcf-and-jquery – Razack Sep 17 '14 at 10:51
0

You need to add webHttpBehaviur to your endpoint and when sending the request to add Accept: application/json header, can be done easily with Fiddler, or with a browser add-on.

Here you can see a good walkthrough of WCF REST service with XML / JSON

Nati Dobkin
  • 81
  • 1
  • 4