Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Can Netduino call a WCF Dataservice 4.0


  • Please log in to reply
6 replies to this topic

#1 John Feeney

John Feeney

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts
  • LocationAuburn, ON, Canada

Posted 04 March 2011 - 11:46 PM

I have looked at the WCF REST Template 4.0 and also the WCF DataService. The WCF DataService says that it can be accessed by simple Http GET, POST. However, every example that I see for the WCF DataService client starts off by added a service reference to the WCF DataService. I don't believe that .Net Micro Framework or the NetduinoPlus template has the ability to added a service reference. Does anyne know if you can access a Visual Studio WCF Dataservice using the NetduinoPlus as a client. For instance say that you have some Temperature sensors wired to the Netduino and would want to upload data to a SQL website database. If it can be done does anyone know of a simple example that I could follow. Thanks, John

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 05 March 2011 - 03:11 AM

Hi John, I'd recommend using a REST web service. You won't be able to consume SOAP web services on tight-resource-contrained microcontrollers like the ARM7 chip used on Netduino. Chris

#3 Fred

Fred

    Advanced Member

  • Members
  • PipPipPip
  • 302 posts
  • LocationUK

Posted 05 March 2011 - 08:48 AM

I'd suggest keeping even simpler than that. Get your Netduino to call (for example) http://myserver/temperature/save/20.0 and have your service work from that. I know it's not what you asked, but should be easier to implement. Basically you'll be coding the "plumbing" on the Netduino end so keep it as simple as you can. I'm fairly certain that creating a HttpPost can be done too. Something like Fiddler will let you spy on exactly what communication is going on.

#4 Nicolas Dorier

Nicolas Dorier

    Member

  • Members
  • PipPip
  • 24 posts

Posted 05 March 2011 - 10:07 AM

From what I have seen, the best and easiest way has been to code an reader/interpreter.

The reader is an interface like ReadInt, ReadCommand, ReadID etc..., it just get bytes from the underlying stream and convert it to an int/command/id.

The interpreter, just call the reader's method in the right order.

var command = reader.ReadCommand();
switch(command)
{
   case Command.DoSomething: .... break;
   case Command.DoSomething2: .... break;
}

The code is really readable, compact, and do not waste any ressources.
(I'm myself a "business developer", I'm very used to WCF, but we don't need all of its flexibility here)

#5 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 315 posts
  • LocationIndiana, USA

Posted 06 March 2011 - 10:07 PM

...if you can access a Visual Studio WCF Dataservice using the NetduinoPlus as a client.

If it can be done does anyone know of a simple example that I could follow.


YES YES YES - of course you can call a WCF Data Service from the Netduino Plus! Holy cow, I can't believe the lack-luster responses you got John! First of all, WCF Data Services quickly exposes a database (via an Entity Data Model) as an OData REST service. Also, with the WCF Data Services Toolkit, now, you can expose anything as a Data Service.

Calling a WCF Data Service is just like calling a REST service because it IS a REST service! But, there is not client library to make it super easy, so you will have to do the string parsing to make it work. But it's a simple HTTP Web Get to read anything. Unfortunately, we don't have the System.Net.WebClient class in the .NET MicroFramweork that would have made it a one liner to call a service, but it is almost that easy still.

First of all, you should get familiar with the XML/Atom output format returned by an OData Service. A great example is Netflix. First, if you are using IE, disable the Feed reading view (Tools > Internet Options > Content > Feeds Settings (button) and uncheck "Turn on feed reading view"). Just Click the links and study it, it's fairly straightforward:

Sample 1 - A Netflix Movie Entry:
http://odata.netflix.com/Catalog/Titles(BVIuO')

Sample 2 - Getting just a Property of a Movie Entry (notice you just add the Synopsis property to the end of the URL):
http://odata.netflix.com/Catalog/Titles(BVIuO')/Synopsis

Sample 3 - Avoid parsing the XML completely by adding $value to the URL:
http://odata.netflix.com/Catalog/Titles(BVIuO')/Synopsis/$value

Now, to make that same call the browser makes is still a one-liner, but with a small helper function. Here is the code:

using System;
using Microsoft.SPOT;

namespace NetduinoApplication10
{
    public class Program
    {
        public static void Main()
        {
            Debug.Print(HttpGet("http://odata.netflix.com/Catalog/Titles('BVIuO')/Synopsis/$value"));
        }

        public static string HttpGet(string URI)
        {
            // Based on http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx
            var req = System.Net.WebRequest.Create(URI);
            var resp = req.GetResponse();
            var sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }
    }
}

Here it is running on my Netduino, and you can see the results in the Output window.
Posted Image

For persisting changes to a database with WCF Data Services, you could figure out how to do an HTTP POST and learn the OData syntax. Or, you could just use the Data Service to read and add some simple Web Methods to do the data changes. I frequently will add a few [WebGet] methods to the data service class to do specialized tasks.

-Valkyrie-MT

#6 Nicolas Dorier

Nicolas Dorier

    Member

  • Members
  • PipPip
  • 24 posts

Posted 07 March 2011 - 11:16 AM

Valkyrie-MT,
I did not said it was impossible, I just don't think it's a good solution.
Especially since I've seen OutOfMemoryException popping out.

I really think that classic custom reader/interpreter, is not very hard to do and the way to go with the netduino.
Even with that, I had OutOfMemoryException... so I don't recommend to test with XML/JSON over HTTP webservices.

#7 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 315 posts
  • LocationIndiana, USA

Posted 08 March 2011 - 01:00 AM

Valkyrie-MT,
I did not said it was impossible, I just don't think it's a good solution.
Especially since I've seen OutOfMemoryException popping out.


If you'll look, the REST calls I used in the example employ the $value so there is no overhead from JSON or XML markup. So these calls are as efficient as possible without using compression. Besides that, I have a service running on a Netduino that communicates over HTTP with JSON and it has run in the past for DAYS making a call every 5 seconds without a single memory exception. I had an OutOfMemoryException in the past, but now I call Debug.GC(true); and have not had a problem since. The thread was here. I think the Netduino is easily capable of calling services like this without running into resource issues.

-Valkyrie-MT




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.