JSON Serializing (request/responces) in WCF on Azure
-
יום ראשון 29 יולי 2012 08:45
Hello,<o:p></o:p>
I am looking for some help with a WCF sample. I need to have the ResponceFormat and the RequestFormat in JSON format. I have an Azure WCF project and can get to the OData (xml) but cannot seem to grasp the JSON serialization. I have the project on SkyDrive at Azure WCF Trivializer I have tryed using the [WebGet(RequestFormat = WebMessageFormat.Json , ResponseFormat = WebMessageFormat.Json, UriTemplate="test")] and I'm abviously missing something. I need to get access to my methods in jQuery from a mobile phone application.
Do I need to do the serialization for each response within the each method?<o:p></o:p>
Thanks,<o:p></o:p>
Rich<o:p></o:p>
כל התגובות
-
יום ראשון 29 יולי 2012 09:06Have you added service endpoints with webHttpBinding?
-
יום ראשון 29 יולי 2012 09:58
Hi Richard,
First, the problem happens due to you are trying to serialize "EntityObject" into JSON. Instead, use POCO object as DTO in your service contract.
In the sample you have posted on SkyDrive, doesn't have any configuration related to webHttpBinding. You should add.
Regards,
Sheik
-
יום ראשון 29 יולי 2012 10:07
Hi Richard,
First, the problem happens due to you are trying to serialize "EntityObject" into JSON. Instead, use POCO object as DTO in your service contract.
In the sample you have posted on SkyDrive, doesn't have any configuration related to webHttpBinding. You should add.
Regards,
Sheik
For example, let us have DTO for Game:
public class GameDto { public int Id { get; set; } public string Name { get; set; } public string Desc { get; set; } }
The service contract would be:
IList<GameDto> GetVenueData()
Inside the implementation:
games = (from game in Db.Games select new GameDto { Id = game.Id, Name = game.Name, Desc = game.Description }).ToList();
This will produce response as:
HTTP/1.1 200 OK
Content-Length: 58
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Sun, 29 Jul 2012 10:06:16 GMT
[{"Desc":"mikes awesome game","Id":1,"Name":"mikes game"}] -
יום ראשון 29 יולי 2012 16:00
Hello Sheik,<o:p></o:p>
Thank for the help.... I do have a question about the “Dto” class. Do I just add it to the method definition or in a separate class file, or iether one, I would imagine that it works like any other class with properties..
<o:p>How is the request serialized to the service? Or is it just by making a JQuery call to the WCF service?</o:p>
Thanks,
<o:p></o:p>
-
יום ראשון 29 יולי 2012 16:19
Hi Richard,
It is always good to define DTOs in separate class files. By specifying JSON format in service contract is enough. WCF takes care the rest for REST :)
BTW, I also recommend you to use WCF Data Services which is based on OData specification. When you access the service from jQuery, you can specify which format you want either in request header (contenttype) or query string parameter ($format). The WCF returns appropriate format. JSON is one of the supporting format.
For more about WCF Data Services, visit http://msdn.microsoft.com/en-us/library/cc668810.aspx.
Regards, Sheik (http://udooz.net/blog)
-
יום ראשון 29 יולי 2012 17:01
Just to clarify, are you saying that the default OData (by way of Entities) service access will let me use JQuery against it? Or are you telling me to access the service via OData (request / return Xml).
Thanks....
-
יום שני 30 יולי 2012 15:54
The WCF Data Services supports JSON format also. You can access them from jQuery.Regards, Sheik (http://udooz.net/blog)
- הוצע כתשובה על-ידי Veerendra Kumar יום שני 30 יולי 2012 16:38
- סומן כתשובה על-ידי Arwind - MSFTModerator יום שני 06 אוגוסט 2012 07:27