Can Netduino call a WCF Dataservice 4.0
#1
Posted 04 March 2011 - 11:46 PM
#2
Posted 05 March 2011 - 03:11 AM
#3
Posted 05 March 2011 - 08:48 AM
#4
Posted 05 March 2011 - 10:07 AM
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
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.
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
Posted 07 March 2011 - 11:16 AM
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
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