Friday, 8 February 2013

Make your path through OWIN with Flux

Introducing OWIN

If you never heard about OWIN, I'll introduce it quoting what appear in the website:

OWIN defines a standard interface between .NET web servers and web applications. 

Most of peoples (included me first time) would think immediately to a new open source web stack. But it would be wrong.
OWIN is a specification and hence never an implementation will be the official one.

At the moment of writing it is one year old; so more time passes and more it is possible that some implementations become reference implementations (but this is something completely different).
To clarify I'll post the remaining introductory explanation:
The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.

Anyway the intent of this article is not to deepen every aspect (it would take a book), but I want just to share with you the path that made me easier to understand this emerging specification, which I find very interesting.

So said that, how are defined OWIN data structures that let developers write applications, middleware and OWIN compliant-host? For a full reference please jump into full specification.

Here I'll present you how OWIN define an application:
using AppFunc = Func<
  IDictionary<string, object>, // Environment
  Task>; // Done
As you can see from previous C# alias, the application is defined using a Func delegate and standard .NET standard types.

You do not depends on any particular assembly and any OWIN compliant-host must be able to load and execute your application.

But how looks a simple OWIN application? (Here, full GitHub project).

This application does nothing than printing some HTTP request headers when pointed by the browser.

As promised this post leaves to the reader further investigation on the subject. So how can you run this OWIN application?

Flux, an OWIN compliant-host

Flux is lightweight web server written in .NET using C# and implements the actual OWIN specification, hence we can say that it's an OWIN compliant-host.

It's author is Mark Rendle (author also of SimpleWeb framework) and you can find full sources on GitHub.

Running this post companion sample is very easy with Flux.
  • clone and then build the sample repository
    • git clone
  • clone and build also Flux
    • git clone
  • copy SimpleOwinApp.dll in same directory of the Flux.exe executable
  • start Flux with the desidered port number
    • flux 1234
  • and finally point your browser on:
    • http://localhost:1234
Was it difficult? I think it wasn't...

Flux relies on another project (of the same author) to locate and load assemblies that contains OWIN applications.
This project is named Fix and you can find it here.

This article and sample application was born on my personal investigation on OWIN that started with this thread on .NET HTTP Abstractions Group.

I hope that this can be a good starting point also for you.

Have fun with OWIN and Flux!

Monday, 28 January 2013

Learn by need

Sometimes when you want learn a new language, you may find difficult find opportunity to use.

Using it in a new project at work, could be an unacceptable risk during the learning phase.
But in an open source project you've no time constraints and no boss screaming against you!

These days I'm heavily refactoring Command Line Parser Library, as you know written in C#.
I decided to change methods naming convention from camel-case to a normal phrase separated by underscore.

Here is the chance! Finally a simple task to be coded in F#, a language I'm starting to love.
Maybe that the following is not the best F# snippet ever (and probably is not).

Anyway this post is not about the code, but about a learning technique that always worked for me.


Thursday, 10 January 2013

Command Line Parser Library verb commands

Hence we're finally here. Command Line Parser Library supports verb commands. This was a feature promised some time ago to the community and now with help of contributors is finally implemented.
It allows you to extend the grammar supported by your application. Anyway when you switch to version Beta one small change is required to your code (and it doesn't relate to verbs).

Short name given to options is now modeled on a character rather than a string constrained to one single character. Community asked for it and after all this is a minor breaking change that made the API cleaner.

It impacts following options definition:
that should become:
So it will not  makes you lose sleep...
Coming back to verb commands, these allow you to define a very familiar syntax to *nix world. If you use git, you probably typed
$ git commit -a
a lot of times or for example
$ zpool status tank
to request ZFS storage pool status in Solaris or SmartOS.

We are at the point; backing to git example commit is the verb command and -a is a switch (in our jargon a BaseOptionAttribute derived type that maps an ordinary option). It applies only to the preceding verb command. If you have another verb, say add, that does not support -a option, parsing will fail.

The following class define options for the verb command named add:
And this is the master options class that must be passed directly to the parser:
It is interesting to note that the parser can create instances for you as these are defined with a parameterless constructor. If this is not the case or you want initialize types by your own (e.g. using a Inversion of Control / Dependency Injection framework) you're free to do that; the library will not overwrite an existing object instance.

As final thought remember that to use the facilities provided by HelpText::AutoBuild you still need that master class and now its child types inherit from CommandLineOptionsBase (this thing should change in a very near release).
Following snippet illustrates how to call the helper method in a verb scenario:
I hope this new feature give you even more freedom when designing command line syntax for your brand new application or important upgrade!
Last thing, don't forget to have fun while programming...