【问题标题】:Self-host ASP.NET Web API and Self-host SignalR together windows service application自托管 ASP.NET Web API 和自托管 SignalR 一起 Windows 服务应用程序
【发布时间】:2016-01-21 08:21:36
【问题描述】:

我想构建一个 Windows 服务,通过自托管 ASP.NET Web API 提供一些服务。另外,我想通过自托管 SignalR 通知客户一些更改。我认为 ASP.NET SignalR 将是通知中心的完美解决方案。

当我同时运行这两个服务时,它们不能一起工作。如果我删除 SignalR,自托管 API 将开始正常工作。反之亦然:删除 windows 服务,SignalR 可以正常工作。

我不确定我的问题是什么,是否可以同时为 asp.net Web API 和 SignalR 提供一个自托管的 Windows 服务?

我在相同和不同的端口上都试过了,但都不起作用。

还有一个问题,是否可以同时拥有两个端口?

我安装的包:

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

我的代码

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


    }
}

【问题讨论】:

标签: c# asp.net api windows-services signalr


【解决方案1】:

我在我的一个 API 上运行类似这样的配置 - ApiControllers 和 Signalr hub 在同一个 URI 上。我认为您的问题与 app.MapSignalR() 部分有关。

这是我在配置中的操作方式:

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

【讨论】:

  • 这帮助我让我的系统正常工作。需要提及的一件事可能会有所帮助:确保将 appBuilder.UseWebApi 调用置于 appBuilder.Map 调用之后。相反,它并不适合我。
猜你喜欢
  • 2015-12-15
  • 2013-07-14
  • 2014-02-06
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
相关资源
最近更新 更多