Monday, September 23, 2013

Getting Started With Custom Web Server in Visual Studio 2013 RC

Introduction
In the world of developing applications everyone is familiar with debugging your application by F5 in Visual Studio. It is very easy and clean. After OWIN and Katana we use the new development environment by IIS Express. We can also run the application with the command line by the use of IIS Express and also by F5.
So, in the current scenario we are using the new experience of developing the application by OWIN. OWIN makes building and hosting your application much simpler. We can also use the OWIN Host as the server to build and host the application.
In that context, in this article I am introducing the way to host our application by OWIN Host. Before starting you must read the article to self-host your application in OWIN . So, let's start the application development using the procedure given below.
Creating a Web Application
Step 1: "File" -> "New" -> "Project...".
KatanaDemo-in-Katana.jpg
Step 2: Select an Empty template to develop.
EmptyApp-in-Katana.jpg
Step 3: Create a class in your application named "Startup.cs".
startup.jpg
Step 3: Delete all the System.Web related references from your application.
SolutionExplorer.jpg
We delete all System.Web references to run the application purely by OWIN.
Step 4: Delete your Web.Config file. Now, your Solution Explorer will look as follows:
Solution2.jpg
Use Owin Host
To use Owin Host as a server we need to make some changes in our application. Have a look at the following procedure.
Step 1: In your Solution Explorer, select your application and right-click to select Properties. Open the Web option.
Web1.jpg
Step 2: Open Package Manager Console and write the following command:
install-package OwinHost -pre
pmc2.jpg
Step 3: Select the OwinHost as shown below:
Web.jpg
Step 4: You can see the changes after selecting the OwinHost as the server.
Web2.jpg
Step 5: Open the "Startup.cs" file and replace the code with the following code:
using KatanaAppDemo;
using Microsoft.Owin;
using Owin;
using System.IO;

[assembly: OwinStartup(typeof(Startup))]
namespace KatanaAppDemo
{
    public class Startup
    {
        public void Configuration(IAppBuilder MyKatanaApp)
        {
            MyKatanaApp.Use(async (ctx, next) =>
            {
                TextWriter Op = ctx.Get<TextWriter>("host.TraceOutput");
                Op.WriteLine("{0} {1}: {2}:", ctx.Request.Scheme, ctx.Request.Method, ctx.Request.Path);
                await ctx.Response.WriteAsync("Hello");
            });
        }
    }
}
Step 6: Press Ctrl+F5 to run the application.
Final.jpg
Step 7: You can also debug the application by F5. When you choose F5, Visual Studio will ask to enable the Web.Config file as shown below:
Final2.jpg
Step 8: Click Yes. You will see the output as shown below:
Final3.jpg
Summary
In this article you saw how to build and host an application using the OWIN Host. OWIN Host is generally not installed by default but if you want it then you need to install it from the Package Manager Console. So just enjoy this feature with Visual Studio 2013 Release Candidate.
Thanks for reading my article.

No comments:

Post a Comment