Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-host ASP.NET Web API and Self-host SignalR together windows service application

Tags:

I wanted to build a Windows service providing some services via Self-host ASP.NET Web API. Also, I wanted to inform the clients about some changes via Self-host SignalR. I thought the ASP.NET SignalR would be the perfect solution for notification hub.

When I run both service they don't work together. If I delete SignalR, the Self-host API starts working perfectly. The other way around as well: delete the windows service, and SignalR work perfectly.

I'm not sure what my problem is, is it possible to have a windows service which is self-hosted for asp.net Web API and SignalR together at the same time?.

I tried both on the same and different ports but it doesn't work.

And the other question, is it possible to have both on same port?

My installed packages:

Microsoft.AspNet.WebApi.SelfHost
Microsoft.AspNet.SignalR.SelfHost
Microsoft.AspNet.WebApi.Owin
Microsoft.Owin.Host.HttpListener
Microsoft.Owin.Hosting
Microsoft.Owin.Cors

My code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.SelfHost;

using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
using Microsoft.Owin;

[assembly: OwinStartup(typeof(WindowsService_HostAPI.Startup))]
namespace WindowsService_HostAPI
{
    partial class SelfHostService : ServiceBase
    {
        IDisposable SignalR;

        EventLog myLog = new EventLog();
        private const string appId = "MYHUB";
        public SelfHostService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            myLog.Source = "MY HUB ";
            var config = new HttpSelfHostConfiguration("http://localhost:9090");

            config.Routes.MapHttpRoute(
               name: "API",
               routeTemplate: "{controller}/{action}/{id}",
               defaults: new { id = RouteParameter.Optional }
           );

            HttpSelfHostServer server = new HttpSelfHostServer(config);



            string CertLocation = ""; 
            server.OpenAsync().Wait();
            try
            {


                myLog.WriteEntry("Notification HUB Start " );
            }
            catch (Exception ex)
            {
                myLog.WriteEntry("Notification Failed TO Start : " + ex.Message + "  |||| " + CertLocation);
            }
            // SignalR
            string url = "http://localhost:9191";
            SignalR = WebApp.Start(url);

        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            try
            {
                push.StopAllServices(true);
                SignalR.Dispose();
            }
            catch (Exception ex)
            {
                myLog.WriteEntry("Notification Failed TO Stop : " + ex.Message);
            }

        }
    }
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }
    public class UserGroupNotification : Hub
    {
        public void Send(string UGID, string message)
        {

            Clients.All.addMessage(UGID, message);
        }


    }
}