Implement OWIN inside of .NET Core

It seems ironic that up until pretty recently, adding OWIN—an open source standard of decoupling server and application code—wasn’t all that easy to do inside the same open source concept of .NET Core. Mostly it appears we had to rely on Kestrel do the serving, particularly whenever we would create a .NET Core web application from the CLI. What I’m going to present for your inspection today is how easy it is to convert a .NET Core application that is pointing to the full .NET Framework to utilize OWIN to remove the application/server dependencies from one another without major breakage. Or… er, well, ahem… at least to minimize the heartache and cursing.

What the !@#$ is OWIN?

Good question, it’s one I had before I looked deeper and what’s more I had to ask, “Why would I want to even use it?” The main reason is if you’ve ever had to wrestle with IIS settings for your .NET web applications or the Windows OS (think WebSockets before Windows Server 2014) you definitely want to take a look. OWIN is standard interface that provides a layer of abstraction between web applications and the web server. Note that there is NO CODE here: OWIN is a specification, nothing more.

For a more comprehensive but concise dive into OWIN, I recommend downloading the free e-book OWIN Succinctly from Syncfusion.

You implement OWIN in frameworks like Katana or Nowin. We’ll be using Katana (Microsoft’s implementation of OWIN standards) in this post, alongside Visual Studio 2017 to create a .NET Core application that points to the full .NET Framework. Note: Applications using only ASP.NET Core components should not use the OWIN package or adapters.

In Visual Studio 2017, create a new web project. I’m calling mine Mustafar but you can call yours whatever you like.

ej4m5ls

You’ll choose the empty template and click OK.

wbupcnj

Interestingly if we run this project, you should see the ubiquitous “Hello, World!” as it’s returned from an asynchronous HttpResponse object via this code in the Startup.cs file.

app.Run(async (context) =>
{
   await context.Response.WriteAsync("Hello World!");
});

If you run this project right now, you should see “Hello World!” displayed in your browser. Now launch the Package Manager Console from the Tools > NuGet Package Manager menu, and run this command line to install the ASP.NET Core component for running OWIN middleware:

Install-Package Microsoft.AspNet.Owin -Version 1.1.2

Once these packages are successfully installed, open the Startup.cs file and comment out the app.Run() code block.

vpmjpyc

Before we can implement the OWIN middleware via Katana, we have to define a task with the method to call. The primary OWIN interface is an application delegate that takes an IDictionary<string, object> returned in a task method. Let’s implement this in the following code using a method called SayHello:

public Task SayHello(IDictionary<string, object> env)
{
  string message = "This executes via the OWIN pipline!";
  byte[] totalBytes = Encoding.UTF8.GetBytes(message);

  var resHeaders = (IDictionary<string, string[]>)env["owin.ResponseHeaders"];
  var resStream = (Stream)env["owin.ResponseBody"];

  resHeaders["Content-Length"] = new string[] { totalBytes.Length.ToString(CultureInfo.InvariantCulture) };
  resHeaders["Content-Type"] = new string[] { "text/plain" };

  return resStream.WriteAsync(totalBytes, 0, totalBytes.Length);
 }

From here, we can then rewrite the method so that the IApplicationBuilder app parameter we declared implements the UseOwin() method and passes that to a lambda expression called pipeline. From there, we call the new SayHello task.

app.UseOwin(pipeline =>
 {
   pipeline(next => SayHello);
 });

Build your project and then run it. You should get back the new message:

3rtbiyc

It’s noteworthy to point out that multiple calls to run OWIN can impact performance seriously, so you’re encouraged to return OWN components together. Happy coding.

Advertisements