Frage

Ich baue eine App, die ServiceStack für eine erholsame API verwendet.Ich versuche, SignalR für Echtzeitfunktionen zu integrieren, da diese App auf dem Client-Desktop ausgeführt wird.Lange Abfragen oder ähnliche Workarounds sind keine Option.Die Anforderungen sind Console App, ServiceStack, SignalR.Ist es möglich, SignalR und ServiceStack gemeinsam selbst zu hosten?

Bedenken Sie Folgendes (alles in einer Konsolen-App):

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 >>

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

Wenn Sie diese Herausforderung selbst ausprobieren möchten, besorgen Sie sich einfach ein Exemplar von https://github.com/ServiceStack/RazorRockstars/tree/master/src/RazorRockstars.SelfHost und versuchen Sie, es zum Laufen zu bringen.

Jede Hilfe bei dieser Denkaufgabe wäre sehr hilfreich.

Vor allem möchte ich wissen, ob dies möglich ist oder nicht.

War es hilfreich?

Lösung

Basierend auf diesem Rock Star-Projekt.Ich habe das folgende NuGet-Paket installiert:

Install-Package Microsoft.AspNet.SignalR.SelfHost

Optional, wenn Sie CORS wünschen:

Install-Package Microsoft.Owin.Cors

Hier ist mein Program.cs Datei:

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

Ich habe CORS aktiviert und JSONP zugelassen, das liegt bei Ihnen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top