0

I created a simple WebAPI service in .NET4 and Visual Studio 2010.

I need to consume that service in a Windows CE / CF 3.5 app

I do have HttpWebRequest available to me, but am not sure if this is the way to go, or I should use RestSharp. Does anybody have insight/experience that would help me decide? I prefer not using 3rd party code when possible to avoid it, but am willing to do so if there is a clear advantage over the "raw" offerings.

Does anyone have, or know of, examples for accessing WebApi services using HttpWebRequest OR RestSharp from CF 3.5?

I have this code (adapted from from http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api) for sample WebAPI methods:

public class VendorItemsController : ApiController
{
    VendorItem[] vendorItems = new VendorItem[] 
    {   
        new VendorItem { VendorId = "1", VendorItemId = "Tomato Soup", ItemId = "Groceries", PackSize = 1 }, 
        new VendorItem { VendorId = "2", VendorItemId = "V8", ItemId = "Groceries", PackSize = 6 }, 
        new VendorItem { VendorId = "3", VendorItemId = "Garlic", ItemId = "Groceries", PackSize = 1 }, 
    };

    public IEnumerable<VendorItem> GetAllProducts()
    {
        return vendorItems;
    }

    public VendorItem GetProductById(string id)
    {
        var vendorItem = vendorItems.FirstOrDefault((p) => p.VendorId == id);
        if (vendorItem == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return vendorItem;
    }
}

...but don't know how to consume this using, if possible, HttpWebRequest.

Note: HttpClient is not available to me (HttpWebRequest is, though).

UPDATE

I start the VS2010 app that has the WebAPI method; but when I run the VS2008 Windows CE / Compact Framekwork 3.5 app with this code:

Uri _baseAddress = new Uri("http://localhost:48614/");
string localFile = "fetchedVendorItems.txt";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_baseAddress + "api/vendoritems/");
req.Method = "GET";
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();

// Retrieve response stream and wrap in StreamReader
Stream respStream = resp.GetResponseStream();
StreamReader rdr = new StreamReader(respStream);

// Create the local file
StreamWriter wrtr = new StreamWriter(localFile);

// loop through response stream reading each line and writing to the local file
string inLine = rdr.ReadLine();
while (inLine != null)
{
    wrtr.WriteLine(inLine);
    inLine = rdr.ReadLine();
}

rdr.Close();
wrtr.Close();

(which I adapted from here: http://msdn.microsoft.com/en-us/library/aa446517.aspx)

...I get, "Unable to connect to the remote server"

UPDATE 2

This does work directly in the browser on the dev machine:

http://localhost:48614/api/redemptions/

It returns these values from a Controller:

readonly Redemption[] redemptions =
{   
    new Redemption { RedemptionId = "1", RedemptionName = "Old", RedemptionItemId = "ABC", RedemptionAmount = 0.25M, RedemptionDept = "2.0", RedemptionSubDept = "42" }, 
    new Redemption { RedemptionId = "2", RedemptionName = "Damaged", RedemptionItemId = "BCD", RedemptionAmount = 5.00M, RedemptionDept = "42.0", RedemptionSubDept = "76" }, 
    new Redemption { RedemptionId = "3", RedemptionName = "Rebate", RedemptionItemId = "DEF", RedemptionAmount = 42.75M, RedemptionDept = "76.0", RedemptionSubDept = "112" }
};

...like so:

<ArrayOfRedemption xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/HHSServerWebAPI.Models">
<Redemption>
<RedemptionAmount>0.25</RedemptionAmount>
<RedemptionDept>2.0</RedemptionDept>
<RedemptionId>1</RedemptionId>
<RedemptionItemId>ABC</RedemptionItemId>
<RedemptionName>Old</RedemptionName>
<RedemptionSubDept>42</RedemptionSubDept>
</Redemption>
<Redemption>
<RedemptionAmount>5.00</RedemptionAmount>
<RedemptionDept>42.0</RedemptionDept>
<RedemptionId>2</RedemptionId>
<RedemptionItemId>BCD</RedemptionItemId>
<RedemptionName>Damaged</RedemptionName>
<RedemptionSubDept>76</RedemptionSubDept>
</Redemption>
<Redemption>
<RedemptionAmount>42.75</RedemptionAmount>
<RedemptionDept>76.0</RedemptionDept>
<RedemptionId>3</RedemptionId>
<RedemptionItemId>DEF</RedemptionItemId>
<RedemptionName>Rebate</RedemptionName>
<RedemptionSubDept>112</RedemptionSubDept>
</Redemption>
</ArrayOfRedemption>

...even when the VS2008 project is not running - is that because this data was cached (the first time I entered:

http://localhost:48614/api/redemptions/

...in the browser, the Web API app was running)?

I get that the emulator won't recognize "localhost" as the desktop instance, considering itself someone/somewhere else. So how can I test this on an emulator? What IP address can I use?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • "localhost" is definitely wrong. That would point to the device itself, even on an emulator (which is in effect a totally separate device from your PC). – ctacke Oct 08 '13 at 01:20
  • The address of the machine actually hosting the service. Probably your PC during development, unless you stood it up on a server, in which case it would be the server's address and port. – ctacke Oct 08 '13 at 16:25
  • Yes, it's my local dev machine; according to http://whatismyipaddress.com/, it is like this (altered the actual address): 133.64.25.27, so I changed the code to: Uri _baseAddress = new Uri("http://133.64.25.27:48614/"); ...but I still get, "Unable to connect to the remote server" – B. Clay Shannon-B. Crow Raven Oct 08 '13 at 17:50
  • Correction: the error now is, "Could not establish connection to network" – B. Clay Shannon-B. Crow Raven Oct 08 '13 at 17:56
  • Why would you need "whatismyipaddess.com?" Just look at the IP address of the PC on the same network as the device. Unless you have that PC on a pulic address, you're seeing the address of your gateway and it's likely NATted. The port also might now be right - in fact I'm pretty sure it's not. Port 80 would be common for REST. So would 8080. The port you have is the temporary debugging port Studio assigns the test web server it uses to host the service when you go into debug mode. – ctacke Oct 08 '13 at 17:57
  • Then what's the use of that web site? So I tried "ipconfig" from the command line, and see a value for "IPv4 Address" Is that what I need? – B. Clay Shannon-B. Crow Raven Oct 08 '13 at 18:00
  • Both http://128.112.83.33:80/ and http://128.112.83.33:8080/ still say, "Could not establish connection to network" – B. Clay Shannon-B. Crow Raven Oct 08 '13 at 18:08
  • This really is a separate question of "How do I get the Emulator on the same network as my PC" – ctacke Oct 08 '13 at 22:35
  • Okay: http://stackoverflow.com/questions/19260075/how-do-i-get-the-emulator-on-the-same-network-as-my-pc – B. Clay Shannon-B. Crow Raven Oct 08 '13 at 23:04

1 Answers1

1

Avoiding 3rd party code out of hand is just plain silly. Why reinvent the wheel? If you are a company who's IP is making REST calls, then sure, roll it, but I suspect your core business offering is in solving some other problem. I mean why use the CF itself, and not C? Why use C and not assembly? Why use a third-party processor and not design your own?

All that aside, RestSharp comes with source and it's free, so there's little risk in using it. There are some things I like about it - primarily that most of the grunt work for REST calls done. I'm a big fan of not reinventing things. It has some quirks that I've "fixed" locally (I'm meaning to do a pull request, just haven't found the time yet) but they were minor and not what I'd consider to be "typical" cases.

As for calling Web APIs with RestSharp, there's a pretty thorough coverage over in this article.

ctacke
  • 66,480
  • 18
  • 94
  • 155