Tsoobame

software, japanese, kyudo and more

Self-Hosted OWIN - Hello World!

We are going to create a new self-hosted website using owin.

0) Requirements;

  • Visual Studio 201x (I am using the VS 2015) in "Run as Administrator" mode (required to open ports).
  • Internet connection (assumed that you have it since you are reading this post).

1) Create a new Console application:



2) Install the Owin packages:

Open the Package Manager Console by clicking the Tools menu and going to Tools/Nuget Package Manager/Package Manager Console

Execute the following command:
Install-Package Microsoft.Owin.SelfHost

It will install the Owin libraries to reference them in your project.

3) Configure the application to host the web:

Add the following content to the Program.cs class:
On top  of usings:
using Microsoft.Owin;
using Microsoft.Owin.Hosting;

Above the namespace declaration:
[assembly: OwinStartup(typeof(Owin.HelloWorld.Startup))]

Add the Startup class :
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello, world.");
            });
        }
    }

Add the content to the body of the Main method:
        static void Main(string[] args)
        {
            using (WebApp.Start("http://localhost:9000"))
            {
                Console.WriteLine("Press [enter] to quit...");
                Console.ReadLine();
            }
        }

4) Check it!

Press F5 to execute the Console Application in Debug mode.

5) Further information: