服务堆栈+ SignalR - 自托管

时间:2014-06-04 18:02:56

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

我正在构建一个使用ServiceStack进行restful api的应用程序。我尝试将SignalR集成到实时功能中,因为此应用程序将在客户端桌面上运行。长时间的民意调查或任何此类工作都不是一种选择。要求是Console App,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");
        }
    }
}

Program.cs&gt;&gt;

    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的副本并尝试让它发挥作用。

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

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

1 个答案:

答案 0 :(得分:1)

基于Rock Star项目。我安装了以下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);
        });
    }
}

enter image description here

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