Katana is a flexible set of components for building and hosting OWIN-based web applications.OWIN defines a standard interface between .NET web servers and web applications. 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.

If you want to have a nice welcome screen instead of “Hello world !” just add below line of code and ignore the rest of the code from Configuration method.

C#

1

app.UseWelcomePage();

Under the hood

Owin describes one main component which is the following interface:

C#

1

Func<IDictionary<string,object>,Task>

This is a function that accepts a simple dictionary of objects, keyed by a string identifier. The function itself returns a task. The object in the dictionary in this instance will vary depending on what the key is referring to.

More often, you will see it referenced like this:

C#

1

usingAppFunc=Func<IDictionary<string,object>,Task>

An actual implementation might look like this:

C#

1

2

3

4

5

6

publicTask Invoke(IDictionary<string,object>environment)

{

varsomeObject=environment[“some.key”]asSomeObject;

// etc…

}

This is essentially how the “environment” or information about the HTTP context is passed around. Looking at the environment argument of this method, you could interrogate it as follows:

C#

1

2

varhttpRequestPath=environment[“owin.RequestPath”]asstring;

Console.WriteLine(“Your Path is:[{0}]”,httpRequestPath);

if a HttpRequest was being made to ‘http://localhost:8080/Content/Main.css” then the output would be:

Your Path is [/Content/Main.css]

In addition, while not part of the spec, the IAppBuilder interface is also core to the functioning of an Owin module (or ‘middleware’ in Owin speak):

C#

1

2

3

4

5

6

7

8

publicinterfaceIAppBuilder

{

IDictionary<string,object>Properties{get;}

objectBuild(Type returnType);

IAppBuilder New();

IAppBuilder Use(objectmiddleware,paramsobject[]args);

}

The IAppBuilder interface acts as the ‘glue’ or host to bring any registered Owin compatible libraries/modules together. With support of this simple mechanism, you can now write isolated components that deal with specific parts of functionality related to Http requests. You can then chain them together to build capabilities of my Http server. You can literally chain together Owin components to form a pipeline of only the necessary features you want. Basically you can build or use a custom host, then you can insert whatever custom modules into the Http request processing pipeline. Owin provides the specification for writing those modules that make it easy to insert into the chain.

Sample Katana component

Owin components are also known as middleware.

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

usingSystem;

usingSystem.IO;

usingSystem.Collections.Generic;

usingSystem.Threading.Tasks;

usingOwin;

usingMicrosoft.Owin.Hosting;

namespaceKatanaDemo

{

usingAppFunc=Func<IDictionary<string,object>,Task>;

classProgram

{

staticvoidMain(string[]args)

{

stringuri="http://localhost:8080";

using(WebApp.Start<Startup>(uri))

{

Console.WriteLine("Started !");

Console.ReadKey();

Console.WriteLine("Stopping !");

}

}

}

publicclassStartup

{

publicvoidConfiguration(IAppBuilder app)

{

// how to use your customer component

app.Use<HelloWorldComponent>();

}

}

publicclassHelloWorldComponent

{

AppFunc _next;

publicHelloWorldComponent(AppFunc next)

{

_next=next;

}

publicTask Invoke(IDictionary<string,object>environment)

{

varresponse=environment["owin.ResponseBody"]asStream;

using(StreamWriter writter=newStreamWriter(response))

{

returnwritter.WriteAsync("Hello !!");

}

}

}

}

Wrap component in extension method and invoke it like this which resembles the Welcome page invocation.