10

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);
        }


    }
}
1

1 Answer 1

2

I run a configuration just like this on one of my APIs - ApiControllers and Signalr hub on the same URI. I think your issue is with app.MapSignalR() piece.

Here is how I do it in my config:

public void Configuration(IAppBuilder appBuilder)
{
    var config = new HttpConfiguration();

    //I use attribute-based routing for ApiControllers
    config.MapHttpAttributeRoutes(); 

    appBuilder.Map("/signalr", map =>
    {
        var hubConfiguration = new HubConfiguration
        {

        };

        map.RunSignalR(hubConfiguration);
    });

    config.EnsureInitialized(); //Nice to check for issues before first request

    appBuilder.UseWebApi(config);
}
1
  • This helped me get my system working. One thing to mention that might be helpful: Make sure you put the appBuilder.UseWebApi call AFTER the appBuilder.Map call. It didn't see to work right for me the other way around.
    – Gerfboy
    Commented May 5, 2017 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.