Friday, 26 September 2014

Windows Hosting | How to use SignalR With The Best, Cheap & Recommended ASP.NET MVC 5 Hosting

Leave a Comment
How to use SignalR With The Best, Cheap & Recommended ASP.NET MVC 5 Hosting - ASP.NET SignalR is a communication framework that lets developers create applications that provide real-time information. (The Chat application and the StockTicker app are the most common examples.) SignalR is based on the famous server push or push technology.

BestWindowsHostingASP.NET - As soon as any information or updates are available on the server, the information is pushed to the connected clients—which makes the operation appear to take place in real time. Today, I am going to show you How to use SignalR with ASP.NET MVC 5 Hosting.

Best SignalR Hosting

Basic Setup

There are 3 steps in using SignalR in MVC:
1. Adding SignalR library
2. Creating OWIN startup classes to setup SignalR route
3. Writing server side and client side code.

Then, you need to use NuGet to install Microsoft.AspNet.SignalR package.
install-package Microsoft.AspNet.SignalR
This will install both server side packages and client side JavaScript libraries into your MVC project.
You need to configure SignalR routes in application startup. Create a C# class file (the filename and location doesn’t matter but a recommended one is “SignalRStartup.cs” in App_Start folder) with the following code.
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(MyNamespace.SignalRStartup))]
namespace MyNamespace
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
 
In the server side, you need to create a Hub subclass in your controller folder. A sample is
using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
public class ChatHub : Hub
{
public void Send(string message)
{
// Call the broadcastMessage method to update clients.
Clients.Others.broadcastMessage(message);
}
}
}

In your Web application, the View file should include "~/Scripts/jquery-{version}.js",,"~/Scripts/jquery.signalR-{version}.js" and "~/signalr/hubs". In the client side, you need to call the server side method as well as define a method to be called by Server side method. The code may look like the following:
var chat = $.connection.chatHub;

$.connection.hub.start().done(function () {

$('#sendmessage').click(function () {

        chat.server.send('Hello World');

    });

});

 

chat.client.broadcastMessage = function (message) {

    alert(message);

};
The JavaScript code use camel-cased version of the server method name. The generated SignalR proxy automatically convert a method name to a correct one. In the server side, there is no method name conversion. A server calls the camel-cased JavaScript client method.

Working with Unity IoC container

After using NuGet to install Unity and Asp.Net SingalR packages, one needs to perform the following steps to resolve types for SignalR methods using a Unity container. There are two options to use Unity as dependency resolve in SignalR: one is to replace the whole SignalR dependency resolver; the other option is to only replace the IHubActivator component of the SignalR dependency resolve. It needs to implement two methods (GetService and GetServices) of SingalR IDependcyResolver interface. The second option only needs to implement one method of SingalR IHubActivator interface.

Not only the first option has more methods to be implemented, but also it needs future steps to allow type resolve in Hub classes. Therefore we choose to use the second option. First, You must create a class implementing IHubActivator interface. In the class, use a Unity container in the IHub Create (HubDescriptor descriptor) method.
using System;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.Practices.Unity;
namespace SignalRHostWithUnity.Unity
{
    public class UnityHubActivator : IHubActivator
    {
        private readonly IUnityContainer container;

        public UnityHubActivator(IUnityContainer container)
        {
            this.container = container;
        } 

        public IHub Create(HubDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }
            if (descriptor.HubType == null)
            {
               return null;
            }
            object hub = this.container.Resolve(descriptor.HubType) ?? Activator.CreateInstance(descriptor.HubType);
            return hub as IHub;
        }
    }
}

Now, you must set SingalR Hub resolver in the Unity configuration file. There is no need to register IHubActivator in Unity as described in the above reference because this interface is only used by SignalR and should be registered only in the SignalR dependency resolver.
var container = new UnityContainer();
GlobalHost.DependencyResolver.Register(
    typeof(IHubActivator), 
    () => new UnityHubActivator(container));

Finally, register every application Hub class in the Unity configuration file.
container.RegisterType<MyHub, MyHub>(new TransientLifetimeManager());

The Unity configuration file looks like below:
var container = new UnityContainer();
container.RegisterType<MyHub, MyHub>(new TransientLifetimeManager());
 // register other types
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
GlobalHost.DependencyResolver.Register(
    typeof(IHubActivator), 
    () => new UnityHubActivator(container));

Best Recommendation for SignalR Hosting

HostForLIFE.eu has great experience in providing excellent SignalR hosting support for their many happy customers. Whatever your budget, SignalR Hosting with HostForLIFE.eu means reliable hosting.
You will enjoy the full support of the experienced HostForLIFE.eu team, 24 hours a day, 7 days a week. Affordable Budget prices, full features, 99.9% Uptime Guarantee, No Risk Money-Back Guarantee - come and see for yourself why everyone is recommending HostForLIFE.eu for SignalR!

HostForLIFE.eu is Microsoft No #1 Recommended Windows and ASP.NET Hosting in European Continent. Their service is ranked the highest top #1 spot in several European countries, such as: Germany, Italy, Netherlands, France, Belgium, United Kingdom, Sweden, Finland, Switzerland and many top European countries. Click here for more information

0 comments:

Post a comment