我在我的MVC4应用程序中使用SignalR hub.我添加了ELMAH来处理所有错误.问题是Hub中发生的错误没有记录在ELMAH axd中.有没有办法配置它?
如果我使用像OwinMiddleware和IOwinContext这样的Microsoft.Owin类型构建OWIN中间件,我的中间件是否可以与非Microsoft Owin主机/服务器一起使用?我正在研究Nancy和SignalR的中间件类,它们看起来与OwinMiddleware基类非常不同,中间件就像Cookie认证中间件和WebApi一样.我正在阅读规范,但我仍然不清楚非Microsoft Owin服务器是否可以使用OwinMiddleware和IOwinContext类型而不依赖于Microsoft.Owin(我猜这会破坏Owin的目的).
我一直在阅读有关SignalR集线器和组的相当数量.特别是,我注意到你无法计算特定组中的连接数.
是否在客户端或服务器上处理组的过滤?如果服务器,为什么SignalR不能公开计数?如果在客户端上,有没有办法只向特定客户端发送消息?
在SignalR Hub类中,您可以呼叫Context.ConnectionId用户.我希望将这些存储Dictionary<string, string>在一起,以便将用户连接在一起.将其他用户的clientid返回给用户的客户端是否存在风险或安全漏洞?
这就是我做的.
问题是没有找到/ signalr/hubs(抛出404).
我的项目有区域,结构如下所示:
signalR的所有脚本都在Scripts文件夹中,我的SignalRTestController.cs看起来像这样:
namespace SignalRTest.Controllers
{
public class SignalRTestController : Controller
{
public ActionResult Index()
{
// Do some work here
// Broadcasting over a Hub from outside of a Hub
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.say("Hello SignalR!");
return View();
}
}
[HubName("MyHub")]
public class MyHub : Hub
{
public void Say(string message)
{
Clients.sendMessage(message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何我的Index.cshtml都引用了所有的javascripts和/ signalr/hubs,如下所示://其他Javascripts
script type …
我正在评估SignalR的中等负载Web应用程序.
我们期待~500 msgs/sec,这对SignalR来说应该不是问题.
但是,我们担心这个解决方案的可靠性.我们的环境存在问题网络,客户端丢失网络连接约30秒并不罕见.是否有任何机制可以确保一旦客户端重新连接,它将获取在其脱机时间内发送的所有消息?
谢谢!
注意:其他人最初问过这个问题,但在我发布答案之前删除了它.由于这个问题涵盖了开发人员在尝试使SignalR跨域工作时遇到的许多问题,因此我决定复制它.另外,我已经写完了答案!
我在ASP.NET MVC .NET Framework 4项目中运行SignalR 1.0.1服务器.我在另一个域(不同的localhost端口)上尝试通过JavaScript客户端连接另一个ASP.NET应用程序.当我的应用程序尝试连接时,我得到了这个:
XMLHttpRequest cannot load http://localhost:31865/api/negotiate?_=1363105027533.
Origin http://localhost:64296 is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我已经按照所有步骤启用SignalR的跨域支持 - 我缺少什么?
jQuery.support.cors = true;$.connection('http://localhost:31865/api', '', false, { jsonp: true, xdomain: true });RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });RouteTable.Routes.MapConnection<ApiConnection>("/api", "api");我还在API项目的Web.config中添加了以下内容:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
我正在为我的SignalR服务器使用PersistentConnection,而不是集线器.
有任何想法吗?
我有一个使用Ninject依赖注入的现有MVC应用程序.我安装了Ninject.MVC3 nuget包,它在我的App_Start中创建了一个名为NinjectWebCommon的类,它完全隔离了内核并注册了我的所有绑定:
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IFoo>().To<Foo>();
}
Run Code Online (Sandbox Code Playgroud)
我们有一个新的要求,我们认为SignalR能够满足,所以我们将SignalR 2 nuget包安装到项目中.我创建了一个Hub并对如何在项目中实现依赖注入进行了一些搜索,并找到了一篇建议创建SignalRDependencyResolver的文章.http://www.asp.net/signalr/overview/signalr-20/extensibility/dependency-injection
本文让您在Startup.cs文件中创建一个内核,用于在OWIN中注册SignalR:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var kernel = new StandardKernel();
var resolver = new NinjectSignalRDependencyResolver(kernel);
kernel.Bind<IStockTicker>()
.To<Microsoft.AspNet.SignalR.StockTicker.StockTicker>() // Bind to StockTicker.
.InSingletonScope(); // Make it a singleton object.
kernel.Bind<IHubConnectionContext>().ToMethod(context =>
resolver.Resolve<IConnectionManager>().GetHubContext<StockTickerHub>().Clients …Run Code Online (Sandbox Code Playgroud) 摘自:http://docs.autofac.org/en/latest/integration/signalr.html:
"OWIN集成中的常见错误是使用GlobalHost.在OWIN中,您可以从头开始创建配置.在使用OWIN集成时,不应该在任何地方引用GlobalHost."
这听起来很合理.但是,如何IHubContext通过ApiController解析,就像通常的(非OWIN):
GlobalHost.ConnectionManager.GetHubContext<MyHub>()?
我无法在任何地方找到这个参考,我现在唯一的方法是HubConfiguration在同一个容器中注册实例并执行此操作:
public MyApiController : ApiController {
public HubConfiguration HubConfig { get; set; } // Dependency injected by
// PropertiesAutowired()
public IHubContext MyHubContext {
get {
return HubConfig
.Resolver
.Resolve<IConnectionManager>()
.GetHubContext<MyHub>();
}
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是,这对我来说似乎相当冗长.这样做的正确方法是什么?更具体一点,是否有一种干净的注册方式IConnectionManager?
编辑:
我最终做的是:
var container = builder.Build();
hubConfig.Resolver = new AutofacDependencyResolver(container);
app.MapSignalR("/signalr", hubConfig);
var builder2 = new ContainerBuilder();
builder2
.Register(ctx => hubConfig.Resolver.Resolve<IConnectionManager>())
.As<IConnectionManager>();
builder2.Update(container);
Run Code Online (Sandbox Code Playgroud)
但我觉得必须有一种更简单的方法来IConnectionManager注入控制器.
我的Startup.SignalR.cs中有以下代码:
using Microsoft.AspNet.SignalR;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Admin
{
public partial class Startup
{
public void ConfigureSignalR(IAppBuilder app)
{
var Config = new HubConfiguration()
{
EnableDetailedErrors = false,
Resolver = new DefaultDependencyResolver()
};
#if DEBUG
Config.EnableDetailedErrors = true;
#endif
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(600);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在应用程序的初始加载期间,一切都很顺利,但是当允许线程休眠(离开约10分钟)时,AppPool被回收并且线程卡在此行上:
app.MapSignalR();
Run Code Online (Sandbox Code Playgroud)
IDE被绿色箭头和This is the next statement to execute when this thread …
signalr ×10
c# ×4
asp.net ×3
asp.net-mvc ×3
owin ×2
autofac ×1
cross-domain ×1
elmah ×1
javascript ×1
katana ×1
ninject ×1
signalr-hub ×1