Wednesday, 29 February 2012

C# HttpListener

Setting up your own HttpListener can be useful, particularly when running a Windows service or perhaps unit testing your RESTful services.

Here is a little console app project that serves to remind how to setup the HttpListener and also how to interact with the WebRequest and WebResponse objects.

HttpListenerExample

Source code:  http://stevenhollidge.com/blog-source-code/HttpListenerExample.zip

Tuesday, 28 February 2012

Metro styled UI

Here’s some offerings from 3rd party tool vendors Dev Express and Telerik to provide some inspiration for the new wave of metro styled apps.

I’ve also included some screen shots from Microsoft via their Azure and Visual Studio vNext offerings.

telerik2

 devexpress1 devexpress2 devexpress3 telerik1

azureazure0azure01azure2azure4azure5

teleriktelerik2telerik3

image

VS-11-splash

image

ddd3

ddd2

inkedin-ipad

image

image

Saturday, 18 February 2012

TryExecute with timeout and maxAttempts

A nice piece of code for calling services with a timeout and maximum attempts, in case the service should cause an exception.

It follows the standard practise for "Try" in .Net, like TryParse, of returning a boolean to indicate success and the result as an out parameter. Should you wish to find out the exception that may have occurred this is also passed as an out parameter.

I've also included an Execute mthod that will raise an exception should the service request timeout or fail more than the maximum number of attempts,

The source code solution includes unit tests in MBUnit, NUnit, xUnit and MSTest:

http://stevenhollidge.com/blog-source-code/TryExecute.zip

Friday, 17 February 2012

Rx Event Example

When subscribing to events Reactive Extensions (Rx) is a powerful way to filter, throttle and compose your .Net code. 

With this example I have a WPF application that listens to a FxRate pricing service, that’s pumping out 100 new prices a second via an event.

I’m going to make available the code to show how to hook up the front end Metro style application using MVVM with:

  • old skool event handlers
  • and then using Reactive Extensions (Rx)

Finally we update the Rx example to filter on just one of the exchange rates and also throttle back the rate of FxRates being received to one per second.

stockmarket-demo

The event signature

Old skool event handlers

Using MVVM, here we have my view model code stripped back just to show we have:

  • A pricing service
  • A subscribed command (data bound to the subscribed button on the UI) which toggles the subscribed property
  • A delegate (PriceUpdate) that handles the event when the subscribed button is clicked.  Click the button a second time to unsubscribe and remove the delegate from handling the event

Lines 25 and 30 show the adding and removal of the event handler, dependent on whether the user has clicked the subscribe button.

Rx Subscriptions

To do the same thing with Rx takes two lines of code (lines 18/19 and 21)

Filtering with Rx

Simply update our subscription to include a where filter that accepts the event coming in (e) and sets the predicate to Ccy (Currency) for the exchange to Euro to GBP.

Throttling with Rx

NOTE:  The code below was a workaround, please use Sample as shown here.

So we have filtered the events to only give us the Euro to GBP prices. Now let's enhance the solution just to give us the latest Euro to GBP price every two seconds.

Source code

Event handlers:  http://stevenhollidge.com/blog-source-code/standard-event-handler-wpf-metro-mvvm-stockmarket-pricing-app.zip

Reactive Extensions:  http://stevenhollidge.com/blog-source-code/Rx-wpf-metro-mvvm-stockmarket-pricing-app.zip

Wednesday, 15 February 2012

WPF Metro MVVM Application

This project is designed to get developers up and running on their WPF Metro MVVM projects.

Screenshots

image

 ScreenShot1 ScreenShot2 ScreenShot3 ScreenShot4

Frameworks

This project combines two excellent existing frameworks within the community:

Name Developer Link Comments
MahApps.Metro Paul Jenkins

http://www.theleagueofpaul.com/metro

Metro style and themes
MVVM Light Toolkit Laurent Bugnion http://mvvmlight.codeplex.com/ Originally based on ideas and source code from Microsoft PRISM

Concepts

The application contains working examples of the following:

  • MVVM with INPC (INotifyPropertyChanged), Messenger with messages (for event notifications), RelayCommands are available to use
  • Metro layout, styles and theming, easing and sliding from “outside” the visual area – all thanks to MapApps, great library!
  • Two way data binding for a combo box to a Enum list

image

ViewModel Example

Source code 

http://stevenhollidge.com/blog-source-code/wpf-metro-mvvm-app-stockmarket.zip

Tuesday, 14 February 2012

Reactive Extensions (Rx)

Overview

MSDN Definition

“Rx is a library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators.”

Rx provides extension methods on top of Linq to enable developers to implement the observer pattern on query results (IEnumerables e.g. a collection or sequence).

The benefits of Rx are in it’s enabling of concurrency and it’s composability.  It allows for easy concurrent processing over a collection along with simple declarative call backs on completion and exceptions.  This programming model allows for simple orchestration of async operations.

To get familiar with Rx, in the examples below we’ll look at simple enumerations of data to get a feel for the programming style.  We can then take a look at Rx for events, with events after all just being a sequence of data (found in the EventArgs).

How To Get Reactive Extensions

Use Nuget, search for Rx and bring in the Reactive.Main library (System.Reactive namespace).

rx-Nuget

Simple example

1.Convert the linq query into an observable (using the Rx extension method).

2. Write a delegate which will execute for:

  • Each item in the enumeration (OnNext method in the examples below)
  • When an error occurs (OnError method in the examples below)
  • When the enumeration completes (OnCompleted method in the examples below)

3. Subscribe to the query results.  This is whether you hook up each of your delegates and start the enumeration.

rx-Simple

“Finally” delegate

There is another delegate we can set, that runs after the processing has completed, regardless of whether the enumeration successfully got the end of the sequence or not e.g. An error occurred half way through processing so the completed delegate doesn’t run.  The Finally delegate will always run which might be useful for clean up.

rx-FinallyDelegate

How to Configure Threading

rx-HowToConfigureThreads

Notice how all the "getting" and "processing" are sequential and in keeping with the enumeration (within each group) even though the processing might be occurring on different threads.

Early Cancellation of Processing

You can cancel the processing of a collection, here is an example:

rx-EarlyCancellation

“Using” A Disposable Object

This example code disposes of an object after the completed delegate has run.

rx-UsingDisposableObjects

A real world example may look like this:

Demo Source Code

You can download the demo here:  http://stevenhollidge.com/blog-source-code/RxExtensionsDemo.zip