Question

Je construis une application qui utilise ServiceStack pour une API reposante.J'essaie d'intégrer SignalR pour les fonctionnalités en temps réel, car cette application s'exécutera sur le bureau client.Une longue interrogation ou une telle solution de contournement n'est pas une option.Les exigences sont Console App, ServiceStack, SignalR.Est-il possible que SignalR et ServiceStack soient auto-hébergés ensemble ?

Considérez les éléments suivants (le tout dans une seule application console) :

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

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

Si vous souhaitez tenter ce défi vous-même, procurez-vous simplement une copie de https://github.com/ServiceStack/RazorRockstars/tree/master/src/RazorRockstars.SelfHost et essayez de le faire fonctionner.

Toute aide avec ce casse-tête serait grandement utile.

Plus important encore, je cherche à savoir si cela est possible ou non.

Était-ce utile?

La solution

Basé sur ce projet Rock Star.J'ai installé le package NuGet suivant :

Install-Package Microsoft.AspNet.SignalR.SelfHost

Facultatif si vous voulez CORS :

Install-Package Microsoft.Owin.Cors

Voici mon Program.cs déposer:

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

J'ai activé CORS et autorisé JSONP, c'est à vous de décider.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top