Consuming the Twitter REST API with HttpClient (WCF REST Starter Kit)

Consuming the Twitter REST API with HttpClient (WCF REST Starter Kit)

A piece of code is worth a thousand words, right? 

try
{
    // initialize HttpClient for Twitter REST API
    HttpClient http = new HttpClient("http://twitter.com/statuses/");
    http.TransportSettings.Credentials =
        new NetworkCredential(twitterUsername, twitterPassword);
    HttpResponseMessage resp = null;

    // retrieve Twitter friends timeline
    resp = http.Get("friends_timeline.xml");
    resp.EnsureStatusIsSuccessful();

    // print all friends statuses
    var statuses = resp.Content.ReadAsXElement().Descendants("status");
    foreach (XElement status in statuses)
        Console.WriteLine("{0}: {1}",
            status.Element("user").Element("screen_name").Value,
            status.Element("text").Value);

    // update your Twitter status
    string newStatus = "writing my first HttpClient app";
    HttpUrlEncodedForm form = new HttpUrlEncodedForm();
    form.Add("status", newStatus);
    System.Net.ServicePointManager.Expect100Continue = false;
    resp = http.Post("update.xml", form.CreateHttpContent());
    resp.EnsureStatusIsSuccessful();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Consuming the Twitter REST API with HttpClient

Consuming the Twitter REST API with HttpClient

A piece of code is worth a thousand words, right?  HttpClient is a new feature in the WCF REST Starter Kit Preview 2.

try
{
    // initialize HttpClient for Twitter REST API
    HttpClient http = new HttpClient("http://twitter.com/statuses/");
    http.TransportSettings.Credentials =
        new NetworkCredential(twitterUsername, twitterPassword);
    HttpResponseMessage resp = null;

    // retrieve Twitter friends timeline
    resp = http.Get("friends_timeline.xml");
    resp.EnsureStatusIsSuccessful();

    // print all friends statuses
    var statuses = resp.Content.ReadAsXElement().Descendants("status");
    foreach (XElement status in statuses)
        Console.WriteLine("{0}: {1}",
            status.Element("user").Element("screen_name").Value,
            status.Element("text").Value);

    // update your Twitter status
    string newStatus = "writing my first HttpClient app";
    HttpUrlEncodedForm form = new HttpUrlEncodedForm();
    form.Add("status", newStatus);
    System.Net.ServicePointManager.Expect100Continue = false;
    resp = http.Post("update.xml", form.CreateHttpContent());
    resp.EnsureStatusIsSuccessful();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Presentations from 2009 Microsoft SOA/BPM Conference Available Online

Presentations from 2009 Microsoft SOA/BPM Conference Available Online

Hadn’t noticed this before, but found the complete collection of videos and presentations from this year’s SOA & BPM Conference.  I didn’t make it up to Redmond for this, so it’ll be nice to peruse the content which covers topics such as:

Customer case studies
Designing services for “Dublin”
Using BAM for Service and SLA monitoring
Supporting WS*, REST […]

WCF REST Starter Kit Preview 2 is on CodePlex

WCF REST Starter Kit Preview 2 is on CodePlex

Go get it here.  This release is significant because they've finally provided a bunch of client-side features through a new generic REST client API called HttpClient.  It makes consuming real-world REST services much easier in .NET.  I've got several screencasts coming shortly.

Preview 1 focused on providing server-side features for building RESTful services while Preview 2 is focused on providing equally compelling client-side features.  The following paragraphs outline these differences (from the CodePlex site):

WCF REST Starter Kit Preview 1

The first set of features in the starter kit is server-side features for building WCF REST services, which enable or simplify various aspects of using the REST capabilities in WCF. These include declarative caching, security, error handling, help page support, conditional PUT, push style streaming, type based dispatch and semi-structured XML support. This functionality is exercised by a set of Visual Studio templates for creating REST services such as an Atom Feed service, a REST-RPC hybrid service, a resource singleton or collection service, and an Atom Publishing Protocol service.

WCF REST Starter Kit Preview 2

Preview 2 of the starter kit introduces a second set of client-side features for accessing WCF and third-party REST services from within .Net applications. The new HttpClient class provides the REST developer with a uniform extensible model for sending HTTP requests and processing HTTP responses, in a variety of formats. The new "Paste Xml as Type" Visual Studio add-in enhances the serialization support in HttpClient by generating serializable types based on XML examples or XSD schema.

Preview 2 only made a few minor changes to the Preview 1 server-side features.  The biggest changes were: 1) moving the code from the Service.svc.base.cs file into Microsoft.ServiceModel.Web.SpecializedCollections within the assembly, and 2) moving the REST templates so they now show up as “official” templates (e.g., they're no longer under My Templates). Other than that, the server side features remain the same for now.

I've done a bunch of screencasts on Preview 1 that you can find in the Pluralsight screencast library.  New ones on Preview 2 are coming soon.

New version of the REST starter kit

New version of the REST starter kit

Its available now on Codeplex 
– I showed off both the client and server pieces of the starter kit at my full-day
REST seminar at SDWest this week.   There are some very useful features
in the kit like:

  • WebProtocolException and an IErrorHandler implementation that returns correct error
    codes

  • The HttpClient class and the “Paste as Xml Serializable” feature to help minimize
    the work necessary when building RESTful clients

Those are just two of my favorites but there are a lot more.  I’ll be using the
HttpClient to help do Conditional GET when I do my Caching in REST talk next Friday
at Mix! 
If you are going to be in Vegas – some by and say hello.



Check out my new book on REST.

ESB Configuration Tool Not Working in the ESB Source Install?!

When you install ESB 2.0 guidance from the source (CTP Build 1), the ESB Configuration tool does not compile. This means that in order to configure the Databases and Web Services, you have to manually configure each of these sections by hand. When trying to compile the ESBConfigurationTool solution, the error you get is here:

The issue is that the DirectoryObjectPicker C# Class library project is referencing an Interop assembly named ActiveDs.dll. This assembly, in this build, does not have the correct signature. To circumvent this issue, one option is to try to refresh this interop assembly by re-referencing the Active DS Type Library as show here:

By doing this, an interop assembly is created using the .NET Framework SDK utility called TlbImp.exe. This process invokes the utility to create an interop assembly. However, this assembly that is created by the TlbImp utility is not signed by default.

You may be asking, "what does have to do with the DirectoryObjectPicker project?"

It just so happens that the DirectoryObjectPicker project needs to be signed and eventually installed into the GAC. Any project that needs to be signed and installed into the GAC has a constraint that says any referenced assemblies also need to be signed as well. We reach the real problem here. The original ActiveDs.dll is not signed correctly and the one created by re-referencing and using the Utility is not signed at all. There are ways in which we could just force a signature on an interop assembly, but we should not because we would be, in effect, “spoofing” the assembly. We should use the original manufacturer’s signature and use its interop version of the assembly.

Here’s the fix. Search your Root drive, and look for any file with the name ActiveDs. Chances are that you have installed other libraries or frameworks that use this fairly common assembly. It can be found inside the BizTalk Accelerators, WCF LOB adapter pack, and other .NET Frameworks. The one that worked for me is the Interop.ActiveDS.dll located inside the BizTalk RossettaNet Accelerators A4RN Msi folder:

Once you’ve found the right match, copy this assembly and place it into your DirectoryObjectPicker source folder. Add a reference to this assembly instead.

What you’ll notice at this point is that the project still doesn’t compile. The reason is because the namespaces are not quite correct. Compile the project and inside the source code file (NameTranslator.cs to be specific), where the few errors occur, add a using statement such as this:

using ActiveDs  = Interop.ActiveDS;

Adding this line of code will create a Namespace alias that points to the correct namespaces. Compile the project and remove the delay signing on each of the two projects: (DirectoryObjectPicker, EsbConfigurationTool). Build and install the DirectoryObjectPicker project into the GAC, and run the EsbConfigurationTool. You’re done.

Happy ESB'ing!

WCF Channel Mini Book – revived

You want to know the ins and outs of WCF at a glance – then the mini-book is a winner.
(Just let this puppy fall out of your back pocket in the office and watch the guys
instantly want to Sync up their Complete Series of Star Trek with you)

Seriously – great guide, easy to flick through and welcome to another 8 million lines
of code you thought you could live without 🙂

Thanks to the efforts of Cliff Simpkins and his team for their dedication
on this.

6 Chapters + Code.are you man enough?

http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=netfxsamples&DownloadId=4962