JC JC
JC JC

Reputation: 12132

How to build a WCF to ASP.net MVC (client) architecture using JSON format/endpoint as way of communication?

I am leaning towards WCF as my main source of service (I may need multiple end-points in the future), and here are the things that I have been stuck at...

Basically, this is my architecture:

WCF === (format: JSON) ===> ASP.net MVC 3 (...and back)

WCF === (format: JSON) ===> misc client (...and back)

code samples would help greatly!

Thanks in advance for the help! :)

Upvotes: 0

Views: 1743

Answers (2)

hawkke
hawkke

Reputation: 4262

A RESTful WCF service will work, like M.Babcock said, but you can just use Ajax to call your controller action; you call your controller, which in turn calls your WCF service and returns a JsonResult. Something like this...

Controller:

public JsonResult GetData() 
{
    var result = wcf.GetSomeData();
    return Json(result); 
}

View:

<script type="text/javascript">
    $(function() {
        $('#mybutton').click(function() {
             $.getJSON("/Home/GetData", null, function(data) {
                 alert(data);
             });
          });
    });
</script>

Here's a link to a better tutorial.

Upvotes: 1

M.Babcock
M.Babcock

Reputation: 18965

WCF RESTful web services are going to be your friend. In order to force the web service to return JSON take a look at this related answer.

Update: If you have control over both the client and the service it may be worth looking into WCF Data Services as an alternative. Less code = more productivity (in some cases ;))

Upvotes: 1

Related Questions