Service Stack + Signal - Self Hosted
-
21-12-2019 - |
質問
RESTFUL APIのためにServiceStackを使用するアプリを構築しています。このアプリはクライアントデスクトップ上で実行されるため、Real Time機能のためにSignal Reを統合しようとしています。長いポーリングやそのような回避策はオプションではありません。要件はコンソールアプリ、ServiceStack、SignalRです。Signal ServerとServiceStackが一緒にホストされているのは可能ですか?
次の点を考慮してください(1つのコンソールアプリ):
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);
}
}
}
.
あなた自身をあなた自身で試してみたい場合は、
この脳のティーザーのあらゆる援助は大いに役立ちます。
最も重要なことに、私はこれが可能かどうか、そうでなければ知りたい。
解決
そのロックスタープロジェクトに基づく
。次の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を有効にしました、それはあなた次第です。
所属していません StackOverflow