【问题标题】:Service Stack + SignalR - Self Hosted服务堆栈 + SignalR - 自托管
【发布时间】:2014-06-04 18:02:55
【问题描述】:

我正在构建一个将 ServiceStack 用于 restful api 的应用程序。我正在尝试集成 SignalR 以实现实时功能,因为此应用程序将在客户端桌面上运行。长时间轮询或任何此类解决方法都不是一种选择。要求是控制台应用程序、ServiceStack、SignalR。是否可以将 SignalR 和 ServiceStack 自托管在一起?

考虑以下(所有在一个控制台应用程序中):

StartUp.cs >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Owin;

namespace RazorRockstars.SelfHost
{
    public class StartUp
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }
}

AppHost.cs >>

namespace RazorRockstars.SelfHost
{
    public class AppHost : AppHostHttpListenerBase
    {
        public AppHost() : base("Test Razor", typeof(AppHost).Assembly) { }

        public override void Configure(Container container)
        {
            LogManager.LogFactory = new ConsoleLogFactory();

            Plugins.Add(new RazorFormat());

            container.Register<IDbConnectionFactory>(
                new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));

            using (var db = container.Resolve<IDbConnectionFactory>().OpenDbConnection())
            {
                db.CreateTableIfNotExists<Rockstar>();
                db.InsertAll(RockstarsService.SeedData);
            }

            this.CustomErrorHttpHandlers[HttpStatusCode.NotFound] = new RazorHandler("/notfound");
            this.CustomErrorHttpHandlers[HttpStatusCode.Unauthorized] = new RazorHandler("/login");
        }
    }
}

程序.cs >>

    using System.Threading;
    using Microsoft.Owin.Hosting;
    using ServiceStack.Logging;
    using ServiceStack.Text;

    namespace RazorRockstars.SelfHost
    {
        class Program
        {
            static void Main(string[] args)
            {
                string listingUrl = "http://*:2001/";
                string listingUrl2 = "http://*:2002/";
                LogManager.LogFactory = new ConsoleLogFactory();

                var appHost = new AppHost();
                appHost.Init();

                appHost.Start(listingUrl);
// the next line will result in error: "The process cannot access the file because it is being used by another process"

                WebApp.Start<StartUp>(listingUrl2);

                "\n\nListening on http://*:2001/..".Print();
                "Type Ctrl+C to quit..".Print();
                Thread.Sleep(Timeout.Infinite);
            }
        }
    }

如果您想自己尝试这个挑战,只需获取一份https://github.com/ServiceStack/RazorRockstars/tree/master/src/RazorRockstars.SelfHost 并尝试让它发挥作用。

对这个脑筋急转弯的任何帮助都会有很大帮助。

最重要的是,我想知道这是否可行。

【问题讨论】:

  • “长时间轮询或任何此类解决方法都不是一种选择。”当 websocket 在客户端和服务器上都不可用时,SignalR 使用长轮询。

标签: c# servicestack signalr self-hosting servicestack-razor


【解决方案1】:

基于那个摇滚明星项目。我安装了以下 NuGet 包:

Install-Package Microsoft.AspNet.SignalR.SelfHost

如果需要 CORS,则可选:

Install-Package Microsoft.Owin.Cors

这是我的Program.cs 文件:

class Program
{
    static void Main(string[] args)
    {
        LogManager.LogFactory = new ConsoleLogFactory();

        var appHost = new AppHost();
        appHost.Init();
        appHost.Start("http://*:2001/");

        using (WebApp.Start<Startup>("http://*:2002/"))
        {
            "\n\nListening on http://*:2001/..".Print();
            "SignalR listening on http://*:2002/".Print();
            "\n\nType Ctrl+C to quit..".Print();
            Thread.Sleep(Timeout.Infinite);
        }
    }
}

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);

            var hubConfiguration = new HubConfiguration
            {
                EnableDetailedErrors = true,
                EnableJSONP = true
            };

            map.RunSignalR(hubConfiguration);
        });
    }
}

我启用了 CORS 并允许 JSONP,这取决于你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-15
    • 2014-02-06
    • 2013-07-14
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多