Hook OWIN middleware into Sitefinity

OWIN stands for Open Web Interface for .NET. It is a specification that gives guidelines for how a web application and web server should decouple their communication. The implementation by Microsoft of this standard is called project Katana. Basically, it allows you to decouple your web application from IIS. To learn more about it I recommend you read this article, which summarizes the info very well.

Why it is important?

Using OWIN concepts allows you to handle requests in a different way than the traditional handler/module approach in ASP.NET. It makes everything faster and lightweight. By referencing the OWIN assemblies to your project, you will be able to plug many middlewares into your pipeline.

How to use it in Sitefinity?

The next question is “How to use OWIN middlewares in my Sitefinity project?” since this is a standard for an ASP.NET projects. Starting from version 10.2, Sitefinity supports easy OWIN pluggability (yes, this is a real word). The first step is to create a class and name it Startup. Second, create a method called Configuration and pass the IAppBuilder parameter. At the beginning of the method you must register the default Sitefinity middlewares. After this you can configure and register any middleware that you want, e.g. SignalR, Nancy etc. This example illustrates all of this in the best possible way:

usingOwin;

usingTelerik.Sitefinity.Owin;

namespaceSitefinityWebApp

{

publicclassStartup

{

publicvoidConfiguration(IAppBuilder app)

{

// Register default Sitefinity middlewares in the pipeline

app.UseSitefinityMiddleware();

app.MapSignalR();

app.UseNancy();

}

}

}

The last step to notify the Sitefinity app that it has to consider your class definition as a plug point. Add this setting in your web.config file. Its value contains the full namespace of the class.

<addkey="owin:appStartup"value="SitefinityWebApp.Startup"/>

Use SignalR in Sitefinity

With the following example, I want to show you how a SignalR OWIN implementation could be used in a Sitefinity web application. The demo is based on the tutorial, Getting Started with SignalR 2 and MVC.

Installing SignalR from NuGet will replace the already referenced OWIN assemblies with lower versions and this will break your website. The solution is simple: reference only Microsoft.AspNet.SignalR.Core.dll and Microsoft.AspNet.SignalR.SystemWeb.dll. Also, add the SignalR JS file (jquery.signalR-2.2.2.js in addition with jquery 1.6.4). This is the initial setup and you are ready for the next steps.

Plug the SignalR middleware into the Sitefinity pipeline and add the required setting in the web.config file. Define the Startup class and configure the SignalR:

usingOwin;

usingTelerik.Sitefinity.Owin;

namespaceSitefinityWebApp

{

publicclassStartup

{

publicvoidConfiguration(IAppBuilder app)

{

// Register default Sitefinity middlewares in the pipeline

app.UseSitefinityMiddleware();

// Configure SignalR middlewares

app.MapSignalR();

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

}

}

}

Once you are done, go and create a ChatHub class that will take care of the execution of the clients’ updates.

usingMicrosoft.AspNet.SignalR;

namespaceSitefinityWebApp.Hubs

{

publicclassChatHub : Hub

{

publicvoidHello()

{

Clients.All.hello();

}

publicvoidSend(stringname, stringmessage)

{

// Call the addNewMessageToPage method to update clients.

Clients.All.addNewMessageToPage(name, message);

}

}

}

Now the foundation of our sample is laid, and the last step is to create a Sitefinity Chat widget that encapsulates the logic and essence of SignalR.

The widget will appear in your toolbox and will be ready for usage. Here is how it looks in action:

Outcome

With Sitefinity 10.2 you can register 3rd party OWIN middleware in an easy and clean way. There are thousands of packages that could be found on NuGet with millions of downloads, e.g. SignalR, Firewall, LESS, Compression etc. Try to find the appropriate solution your problem and plug it into Sitefinity. I hope that this functionality will make your daily jobs easier.

If you have interesting examples, I would like to encourage you to share them with the community. Happy extending and coding!

Peter Filipov (Pepi) is a Developer Advocate focused on Sitefinity. He is passionate about web development and sports. Prior joining the DevRel team, Pepi was one of the team leaders in the Telerik Web Components division.

Choosing the Right Digital Experience Platform to Improve Business Outcomes

The Fastest Way to Build Mobile Apps With Cloud Data

Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.