C#

OWIN and Katana – what’s the big deal?

OWIN stands for The Open Web Interface for .NET. It is a standard for communication between .NET web servers and web applications. It defines required elements for HTTP request (details). It is inspired by Rack from Ruby on Rails World. Katana is implementation of this standard. We can say that it is a lightweight web server for .NET. In fact, it is more than that (more info here).

Demo

First, we need to create application project. Let’s create ‘Empty Web Application’ (it might be also Console App).

OWIN - empty Project

Next, we will install two NuGet packages (using Package Manager Console):

Install-Package Microsoft.Owin.Host.SystemWeb

Install-Package Owin.Extensions

Then, we need to create ‘Startup class’.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Owin;

namespace OwinDemo
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseHandlerAsync((req, res) =>
            {
                res.ContentType = "text/plain";
                return res.WriteAsync("Hello Katana!");
            });
        }
    }
}

Now we are ready to run our web server, but you may get following error:

OWIN - error

Fortunately there is easy fix for that. You need to modify Web.config file, adding following code in configuration section:

<appSettings>
    <add key="owin:HandleAllRequests" value="true"/>
</appSettings>

Then you can run server (CTRL+F5) and you should see:

OWIN - Hello Katana

Summary

So, what is big deal? We have web server in 7 lines of code! We do not need IIS as only one, right choice.

Of course we can do much more sophisticated things. Such as routing, WebAPI or even SignalR. You can also debug it easily.

More info about OWIN and Katana on ASP.NET website: An Overview of Project Katana
There is also screencast on Channel9: The Katana Project – OWIN for ASP.NET (it shows e.g. how to use WebAPI from 35:40).
Here is very nice article how to use SignalR with Katana.

Katana is Open Source and available on CodePlex.


scriptcs – C# in console

It was always bothering me, when I wanted to run one simple command, and I needed to create new C# console project in Visual Studio to do that. With scriptcs I can finally do that in console. Project scriptcs allows you to run single commands and also C# script files.

Installation

The easiest way to install scriptcs is Chocolatey (‘apt-get’ for windows). If you didn’t hear about it, you should definitely try it out. To install Chocolatey run the following command in console:

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

Once you have Chocolatey installed, you can install scriptcs:

cinst scriptcs

Chocolatey will install scriptcs to %APPDATA%\scriptcs\. You need to update your PATH accordingly, to easily run it from command line.

Getting started

Once scriptcs is installed and added to your PATH, you can run it with scriptcs command:

C:\> scriptcs
scriptcs (ctrl-c or blank to exit)

> var message = "Hello, world!";
> Console.WriteLine(message);
Hello, world!
>

C:\>

You can also create a script hello.csx:

var message = "Hello, world!";
Console.WriteLine(message);

And run it from command line:

C:\>scriptcs hello.csx
Hello, world!

You can find more about scriptcs on scriptcs.net.

EDIT: You don’t even need Console.WriteLine to print variables (thanks Filip W.):

C:\>scriptcs
scriptcs (ctrl-c or blank to exit)

> var message = "Hello, scriptcs!";
> message
Hello, scriptcs!
> int four = 2 + 2;
> four
4
>


Desktop Watcher

Have you ever forgotten to lock your computer and went for a lunch? If so then you know what can happen. Your coworkers can send invitation for a party at your place to all co-workers (using your e-mail). They can also mess up with your desktop icons and much, much other fun stuff. The best solution is always lock the system. However sometimes we forget about it.

Once I was bored after work I created WinForms application, which starts playing scary sound when somebody move the mouse or push some key on the keyboard (while I am out of my desk). Usually when you want to mess up with somebody’s machine your heart rate is higher than normal (because of adrenaline that you can be caught). Then not expected scary sound can cause even heart attack.

I named my app: Desktop Watcher. It looks like that:

Desktop Watcher

When you hit Play, you get file dialog to choose some scary sound (like this) from your hard drive. Then you need to put cursor in the program area and leave your machine. You have 5 seconds for that. Every mouse move or keyboard’s key push after that will start sound playing and lock the machine. If you caught somebody the machine will be locked and sound will be playing. To quit the app you need to unlock the machine and hit ALT-F4  immediately (two keys together – because hit only ALT will cause lock screen again) or close app by mouse if you are quick hand person.

No worries that somebody close your app by ALT-F4 before he moves the mouse. If so then system will be locked anyway (but no sound will be played). You do not need to worry about it, because it means that somebody knew you prepared a trap 🙂

There is an issue that mouse needs to be in the program area to detect mouse moves. I may fix it in the future, but for now you can just hide the app somewhere (e.g. on right bottom corner):

Desktop Watcher hidden

Source (and sample scary sound) is available on github: https://github.com/jj09/DesktopWatcher.