What if i tell you that you can built a web application that is
would you believe me? If not you should
Owin stands for - Open Web Interface for .NET its basically allows you to built a fast light weight host to run your applications. Link to more Owin explanations
Owin also allows you to have a direct access you every cycle an http request has to go through unlike technologies like asp.net web applications that need the IIS hosting services
Notice that Owin is a specification that defines the relation between a framework and a web server, and Katana is the implementation of Owin using Microsoft components , this components are referenced at the top of the program ,if you want to implement Owin on maybe Linux , you will have to use other implementation then Katana Link to Katana project
This is a very simple demonstration of using Owin
what we have here is a consol application, notice that we do not use any IIS services and the only things we need to do are to reference the Katana components like Microsoft.Owin.Hosting and Owin through Nuget or other way, and run this application in a release or debug mode
after you have ran the application you need to open you web browser and go to http://localhost:6060 url
if all goes well you should see you consol application output looking like this:
here is the output of the consol application after you have typed the url on your web browser
here is the output of the browser after you have typed in the url
I will try to explain the code in short just to show the basic things that are going on, if you are interested in more information you can google for more Owin examples or ask me on the comment section below
Because it is a consol application we need a main method, we define the the url that this application will listen to
in line 5 we basically start our server and let every body know that by printing "starting!"
in line 13 we define the AppFunc - this is how components (will see one later) process and interact with request to the server
it is a delegate to a function that takes a dictionary that represents the request environment and returns a task
in the environment you can find out every thing about the request and response to the client
The delegate returns a task to keep the Async behavior of the application(see my async and await topic for more info)
One important thing we must know before moving on is that Owin uses the pipeline model
this pipeline consists of different components (middlewares) that do not know about each other, one component can do the logging the other can check for authorization and the third one can be our component that writes our massage in the browser
lets look at our component that writes the massage to the browser, we call that component in line 44 in the big program
first we see a constructor that gets an appFunc element (next) that represents the next component in the pipeline and saves it in a privet appFunc variable(_next)
this constructor is must, if we don't add him we would get an exception because Katana uses reflection to search for it
this component must have this constructor because Katana needs to involve this component in to the pipeline
although we don't use the _next element our selfs, it is being used behind the scenes
Next, in the invoke function, we write the response in the response body using the environment dictionary
using a stream writer we can asynchronously write to that stream and return a task
here we create an extension to the appFunc element to include our function
most importantly we register our component to this application, this registration will form Katana to look for the invoke method in the component and instantiate the component and make it ready to use in the pipe line
The component we wrote also called a middleware because it seats in the middle of a processing pipeline, we can assume that they might be other components before or after ours, unlike our component, components may interact with one another
Now to our final and the most important piece of code that actually runs it all
here we actually have two middlewars, the one we saw before and the middlware in line 5
this middlware (component) is written in a higher abstraction and doesn't use a class
it uses the app.run function that takes a lambda expression, this lambda expression takes the environment and the next component and returns a task (the same idea as before)
this middlware is different form the previous "myAppCommponent" because of line 9 "await.next()"
await.next() means that we need to wait for the next middleware to finish its job
it is important to understand that every thing before the await.next() is done in the request pipeline and every thing after the await.next() is don in the respond pipeline
these are the stages for one default path access, is possible to write middleware that respond to different paths by defining different routes for every middleware