我正在构建一个将 ServiceStack 用于 restful api 的应用程序。我正在尝试集成 SignalR 以实现实时功能,因为此应用程序将在客户端桌面上运行。长时间轮询或任何此类解决方法都不是一种选择。要求是控制台应用程序、ServiceStack、SignalR。是否可以将 SignalR 和 ServiceStack 自托管在一起?
考虑以下事项(都在一个控制台应用程序中):
启动.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();
}
}
}
应用主机.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的副本并尝试让它工作。
对这个脑筋急转弯的任何帮助都会有很大帮助。
最重要的是,我想知道这是否可能。