0

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...

  • WCF to CLIENT: How can I make my MVC accept JSON data from WCF service and parse it into C# primitive/complex types?
  • CLIENT to WCF: How can I send JSON formatted data from MVC to WCF and have it parsed to C# primitive/complex types?

  • side question: How can I make WCF use REST as its protocol and transmit JSON format data? Do I use REST starter kit or is it built in on WCF?

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! :)

Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63

2 Answers2

1

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 ;))

Community
  • 1
  • 1
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • i heard that Data Services doesn't support many LINQ features.. how is it in your experience? – Jan Carlo Viray Dec 21 '11 at 22:07
  • Data Services is strongly tied to Linq to Entities. What linq features are you concerned about being left out? – M.Babcock Dec 21 '11 at 22:48
  • 1
    Server side paging can be implemented using: http://blogs.msdn.com/b/astoriateam/archive/2010/02/02/server-paging-in-data-services.aspx, or on the client side: http://solidcoding.blogspot.com/2007/11/paging-with-linq.html. While I haven't used the standard LINQ paging approach in the past against Data Services I don't see why it wouldn't work. – M.Babcock Dec 22 '11 at 14:47
  • 1
    @JanCarloViray - On second thought, you're talking about consuming a WCF Data Service from an MVC page (presumably using AJAX), so if you are looking for an OData client library for Javascript, take a look at http://www.odata.org/developers/odata-sdk. It talks about OData clients for various languages including Javascript. In that case I would probably suggest utilizing server side paging because you don't really have Linq in Javascript to help you out. – M.Babcock Dec 22 '11 at 19:00
1

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.

hawkke
  • 4,242
  • 1
  • 27
  • 23