Home > ASP.NET > CodeFluent Entities introduces JSON/REST native support

CodeFluent Entities introduces JSON/REST native support


Here’s a quick post regarding JSON/REST support in CodeFluent Entities which was introduced in the latest developer build (603).

Starting from this build, the Service Object Model sub-producer which generates the complete WCF-based communication layer (service, contracts, and proxy) now supports natively JSON/REST. Enable the JSON support, and it will automatically generate WCF services decorated with the proper attributes to be consumed in JSON/REST.

Here’s how to enable the feature:

JSONOptions

Once you configured this sub-producer and built your model over again here’s the kind of code you should see:

public partial interface ICustomerService
{
(…)
[System.ServiceModel.OperationContractAttribute()]
[System.ServiceModel.Web.WebInvokeAttribute(UriTemplate="LoadAll", ResponseFormat=System.ServiceModel.Web.WebMessageFormat.Json, RequestFormat=System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle=System.ServiceModel.Web.WebMessageBodyStyle.Wrapped)]
Sample.CustomerCollection LoadAll();
(…)

As you can see, each method:

  • has its own URI corresponding to its name (defined by the UriTemplate attribute) (e.g. to call the LoadAll method we will call the following URL: http://server/EntityName.svc/LoadAll),
  • expects JSON objects as parameters (RequestFormat), so the content-type of our requests will always be application/json,
  • will send a JSON object as a response (ResponseFormat),
  • will wrap the response in an object (BodyStyle). This object will have one only field named [MethodName]Result,
  • expects to be called with the Http POST method (this is the default value of the WebInvoke “Method” parameter).

To expose those services on a web server use ".svc" files such as the following:

<%@ServiceHost language="C#" Debug="true" Service="Sample.Services.CustomerService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>       

This allows, without a single line of code behind nor any particular configuration, to call the appropriate service from a URL.
From the client’s point of view you’ll then be able to call the service using the following ways.
For instance, to load all customers from our customer service, the client must call:
– URI: http://localhost/JsonSample/Services/Sample.Customer.svc/LoadAll,
– With the http POST method and the header Content-Type set to application/json,
– With an empty content parameter.
And the client will receive a JSON object such as this one: {“LoadAllResult”:[{…CustomerJsonObjects…}]}.

Note: in this example my web application is hosted in IIS, in a “JsonSample” virtual application in my default site. Moreover, my .svc files are all placed in a "Services" sub-folder. The url will of course differ depending on where you’re hosting your web application.

Here’s an implementation example in an ASP.NET web page using JQuery:

(…)
<script type="text/javascript" src="scripts/jquery-1.6.3.min.js"></script>
(…)
    <div id="Container">
        <div id="Content">
            <p>
                Hello!<br />
                Here’s a sample page calling the LoadAll method of the customer service and printing the results returned by the service using JQuery:
            </p>
        </div>
        <div id="CustomerBlock">
            <div id="CustomerContent">
            </div>
        </div>
    </div>
(…)
<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: "http://localhost/JsonSample/Services/Sample.Customer.svc/LoadAll&quot;,
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data, textStatus, jqxhr) {
                $(‘#CustomerContent’).html("<p>" + jqxhr.responseText + "</p>");
            },
            error: function(jqxhr, errorText, ex) {
                $(‘#CustomerContent’).html("<p>Error: " + ex + "</p>");
            }
        });
    });
</script>

Consuming methods with parameters is almost the same.
For instance to load a customer of Id=1, the client must call:

And the client will receive a response like: {"LoadResult":{…CustomerJsonObject…}}.

Enjoy Smile

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s