European Windows 2012 Hosting BLOG

BLOG about Windows 2012 Hosting and SQL 2012 Hosting - Dedicated to European Windows Hosting Customer

Free ASP.NET Hosting with OWIN Support - HostForLIFE.eu :: How to Self Host the Web API using OWIN Custom Hosting?

clock April 16, 2015 06:47 by author Peter

In this short tutorial, I will tell you how to self-host the Web API, using OWIN custom host. First step that you must do is Add a new Console application as you can see on the below picture.

Next, in order to make a web API and use OWIN custom host, we tend to add references to Microsoft.AspNet.WebApi.OwinSelfHost, using the Nuget Package Manager. this may not only add the references for the web API however also the OWIN components, along with the opposite needed dependencies.

Add a replacement web API controller class. we tend to take away all the methods and add an easy GET method to get the sum of two random numbers.

Add a new class named Startup.cs. this can be as per the OWIN specifications. Use the HttpConfiguration class to make the web API routing template and add it to the request pipeline using the appBuilder.UseWebApi method, where appBuilder is of type IAppBuilder.

Open the Program.cs file and begin the server using the WebApp.Start method, specifying the StartUp class because the entry point for the settings needed. this is the OWIN specification of starting the server within the custom host.

Now simply run the application and the server is started.

To test the Web API, we’ll use the Chrome browser Postman extension. So type the URL of the Web API that we specified in the Program.cs and Send the request. See the results received as you can see on the following picture:

Free ASP.NET Hosting with OWIN Support

Try our Free ASP.NET Hosting with OWIN Support today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc. You will not be charged a cent for trying our service for the next 3 days. Once your trial period is complete, you decide whether you'd like to continue.



OWIN & Katana.NET Hosting - HostForLIFE.eu :: How to Build a Simple Server using OWIN and Katana

clock April 10, 2015 07:39 by author Rebecca

This article will show you how easy to build a simple server using OWIN and Katana Project. Usually, you use HTML, JavaScript, and CSS files over HTTP from a .NET desktop application to run the scenario code that you've created. But, Katana makes it easier to run you scenario code.

Here is an example using a console application:

First, use NuGet to install a couple packages into the project.

install-package Microsoft.Owin.StaticFiles
install-package Microsoft.Owin.SelfHost

The source for the entire application is 17 lines of code, including statements:

using System;
using Microsoft.Owin.Hosting;
using Owin;
 
namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = "http://localhost:8080";
            WebApp.Start(url, builder => builder.UseFileServer(enableDirectoryBrowsing:true));          
            Console.WriteLine("Listening at " + url);
            Console.ReadLine();
        }
    }
}

Then, the FileServer middleware will serve files from the same directory as the executable.

But, if you don’t want to use the default location, you can provide your own IFileSystem and serve files from anywhere. Katana currently provides two implementations of IFileSystem – one to serve embedded resources and the other one serve files from the physical file system. You can construct a PhysicalFileSystem for an arbitrary location on the hard drive, like this following code below:

static void Main(string[] args)
{
    var url = "http://localhost:8080";
    var root = args.Length > 0 ? args[0] : ".";
    var fileSystem = new PhysicalFileSystem(root);
 
    var options = new FileServerOptions
    {
        EnableDirectoryBrowsing = true,
        FileSystem = fileSystem                           
    };
 
    WebApp.Start(url, builder => builder.UseFileServer(options));          
    Console.WriteLine("Listening at " + url);
    Console.ReadLine();
}

The FileServer middleware is actually a composite wrapper around three other pieces of middleware:

  • DefaultFiles (to select a default.html file, if present, when a request arrives for a directory).
  • DirectoryBrowser (to list the contents of a directory if no default file is found).
  • StaticFile (to reply with the contents of a file in the file system).

All three pieces of middleware are configurable through the UseFileServer extension method.

For example, the static file middleware will only serve files with a known content type. Although the list of known content types is extensive, you might need to serve files with uncommon extensions, in which case you can plug a custom IContentTypeProvider into the static files middleware.

public class CustomContentTypeProvider : FileExtensionContentTypeProvider
{
    public CustomContentTypeProvider()
    {
        Mappings.Add(".nupkg", "application/zip");
    }
}


Now the final program looks like the following:

static void Main(string[] args)
{
    var url = "http://localhost:8080";
    var root = args.Length > 0 ? args[0] : ".";
    var fileSystem = new PhysicalFileSystem(root);
    var options = new FileServerOptions();
    
    options.EnableDirectoryBrowsing = true;
    options.FileSystem = fileSystem;       
    options.StaticFileOptions.ContentTypeProvider = new CustomContentTypeProvider();
 
    WebApp.Start(url, builder => builder.UseFileServer(options));          
    Console.WriteLine("Listening at " + url);
    Console.ReadLine();
}

HostForLIFE.eu OWIN and Katana.NET Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



OWIN & Katana.NET Hosting - HostForLIFE.eu :: First Look at OWIN and Katana Project

clock March 31, 2015 06:26 by author Rebecca

Today, i'm gonna show you how to get started with OWIN and Katana Project. Open Web Interface for .NET (OWIN) is a specification for the interaction between web servers and web applications. It has its roots in the open source community. By decoupling the web server from the application, OWIN makes it easier to create middleware for .NET web development. Also, OWIN makes it easier to port web applications to other hosts, for example, self-hosting in a Windows service or other process. OWIN is a community-owned specification, not an implementation. The implementation of OWIN is called as Katana Project. The Katana project is a set of open-source OWIN components developed by Microsoft.

Katana is a webframework which is really lightweight and much more modular then ASP.NET. ASP.NET applications always include System.Web, which contains all functionality like caching, authorization, etc. Katana is way more modular. It let’s you build a application where you only add the references you really need. Katana lets you compose a modern Web application from a wide range of different Web technologies and then host that application wherever you wish, exposed under a single HTTP endpoint. This provides several benefits:

  • Deployment is easy because it involves only a single application rather than one application per capability.
  • You can add additional capabilities, such as authentication, which can apply to all of the downstream components in the pipeline.
  • Different components, whether Microsoft or third-­party, can operate on the same request state via the environment dictionary.

So, Katana is a flexible set of components for building and hosting OWIN-based web applications. And this post is just a quick start to give you an impression of the power that Katana gives you.

The code below shows how easy is to get started with Katana. Create a new console application and install the nuget package: Microsoft.Owin.SelfHost. And If you now paste the code, you will already have a working website at localhost:8080.

class Program
{
    static void Main(string[] args)
    {
        var url = "http://localhost:8080";
        using (WebApp.Start<Startup>(url))
        {
            Console.ReadLine();
        }
 
    }
}
 
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Run(x =>
        {
            x.Response.ContentType = "text/plain";
            return x.Response.WriteAsync("Hello world");
        });
    }
}

We define a url and a startup class. The Startup class needs a configuration method in which we can set the run method with a Func on your IAppBuilder. This appbuilder is the “configuration” of your web application and the run method adds a piece of middleware to your OWIN pipeline which doesn’t invoke a next piece of middleware. Look at it at as the endpoint of all the web requests for this  small web application.

The appbuilder also allows you to add more middleware to your OWIN pipeline. If we for instance want to have some middleware that handles cookies we can add it with a few lines of code.

public class CookieMonster : OwinMiddleware
{
    public CookieMonster(OwinMiddleware next) : base(next)
    {
    }
 
    public async override Task Invoke(IOwinContext context)
    {
        context.Response.Cookies.Append("testcookie", "hello world");
        await Next.Invoke(context);
    }
}

And now, we need to inherit from OwinMiddleware, add a constructor and a async Invoke method. In the invoke method we add our cookie (or whatever logic you want) and make it invoke the next middleware down the pipeline. To  plug this middleware into our application, we only need our appbuilder to use it. Just add the following line in your Startup class above the app.Run line and your done!!

app.Use<CookieMonster>();

Now, you have a working Katana Web Application that adds an annoying cookie. Easy right? This was a really simplified example and there is a lot more to Katana.

For the best OWIN and Katana.NET Hosting, HostForLIFE.eu is the right answer!

HostForLIFE.eu OWIN and Katana.NET Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2012 Hosting, ASP.NET 4.5 Hosting, ASP.NET MVC 5 Hosting, SQL 2014 Hosting and SQL 2014 Hosting.

Tag cloud

Sign in