Automate your infrastructure to build, deploy, manage, and secure applications in modern cloud, hybrid, and on-premises environments.
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.
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.
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:
using
Owin;
Telerik.Sitefinity.Owin;
namespace
SitefinityWebApp
{
public
class
Startup
void
Configuration(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.
<
add
key
=
"owin:appStartup"
value
"SitefinityWebApp.Startup"
/>
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:
// Configure SignalR middlewares
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.
Microsoft.AspNet.SignalR;
SitefinityWebApp.Hubs
ChatHub : Hub
Hello()
Clients.All.hello();
Send(
string
name,
message)
// 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.
Model:
SitefinityWebApp.Mvc.Models
/// <summary>
/// The chat message.
/// </summary>
Room
/// Gets or sets the sender of the message.
/// <value>
/// From.
/// </value>
Title {
get
;
set
; }
Controller:
System.Web.Mvc;
Telerik.Sitefinity.Mvc;
SitefinityWebApp.Mvc.Controllers
[ControllerToolboxItem(Name =
"Chat"
, Title =
"Chat client"
, SectionName =
"MVC"
)]
ChatController : Controller
ActionResult Index()
var room =
new
SitefinityWebApp.Mvc.Models.Room() { Title =
"Common"
};
return
View(
"Index"
, room);
View:
@model SitefinityWebApp.Mvc.Models.Room
style
type
"text/css"
>
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
</
div
"container"
>Room: @Model.Title</
input
"text"
id
"message"
"button"
"sendmessage"
"Send"
"hidden"
"displayname"
ul
"discussion"
></
<!--Script references. -->
<!--Reference the jQuery library. -->
script
src
"~/Scripts/jquery-1.6.4.min.js"
<!--Reference the SignalR library. -->
"~/Scripts/jquery.signalR-2.2.2.min.js"
<!--Reference the autogenerated SignalR hub script. -->
"~/signalr/hubs"
<!--Add script to update the page and send messages.-->
"text/javascript"
var jq164 = jQuery.noConflict(true);
(function ($) {
$(document).ready(function () {
//Set the hubs URL for the connection
$.connection.hub.url = "http://your.site.address/signalr";
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.addNewMessageToPage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<
/>').text(name).html();
var encodedMsg = $('<
/>').text(message).html();
// Add the message to the page.
$('#discussion').append('<
li
><
strong
>' + encodedName
+ '</
>: ' + encodedMsg + '</
>');
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
}(jq164));
The widget will appear in your toolbox and will be ready for usage. Here is how it looks in action:
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 Product Manager focused on expanding Sitefinity ecosystem. His prior experience as a Developer Advocate and Manager of Engineering helps him to understand the customers’ and market needs of Sitefinity. He also is passionate about being active and healthy 24/7 😊.
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and Privacy Policy for California Residents and uses it for the purposes stated in that policy.
You have the right to request deletion of your Personal Information at any time.
You can also ask us not to pass your Personal Information to third parties here: Do Not Sell My Info
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Copyright © 2021 Progress Software Corporation and/or its subsidiaries or affiliates.All Rights Reserved.
Progress, Telerik, Ipswitch, 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.