gpt4 book ai didi

c# - SignalR 使用 GlobalHost.ConnectionManager.GetHubContext 从外部集线器调用客户端方法不起作用。但是从集线器内部调用确实

转载 作者:IT王子 更新时间:2023-10-29 04:09:44 27 4
gpt4 key购买 nike

我正在尝试从 .net Web API Controller 操作中调用客户端方法。

我可以这样做吗?

我能找到的唯一一篇贴近我想要做的文章是这篇文章:

SignalR + posting a message to a Hub via an action method

其中使用 GlobalHost.ConnectionManager.GetHubContext 从 asp.net MVC Controller 操作中发送一条消息。

当我在我的 Web API 操作中尝试这样做时,没有抛出任何错误,但从不在客户端调用方法“methodInJavascript”。

    Public ActionResult MyControllerMethod()
{
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.methodInJavascript("hello world");
// or
context.Clients.Group("groupname").methodInJavascript("hello world");
}

当我在该操作中设置一个断点时,我看到正在到达并执行代码。但是在 JavaScript 客户端没有任何反应。

为什么? Web API 的内幕如此不同以致于它不起作用吗?有没有其他人尝试过并取得成功?

当我从我的集线器“内部”调用“methodInJavascript”时,它运行良好。从 .net Web API Controller 操作中调用时将不起作用。

更新:

在研究了这个问题后,我没有解决办法。我只能假设像这样的例子中缺少一些东西 Server to client messages not going through with SignalR in ASP.NET MVC 4还有这个calling SignalR hub from WebAPI controller issues就像可能有一个额外的配置步骤来启用从 HubContext 或其他东西调用。我最初在此处发布的代码与那些示例中出现的代码类似,但未被证明有任何缺陷。任何人都可以看到代码中的缺陷吗?从 html 调用有效。我在我的应用程序中广泛使用它,从未遇到过问题。我从未在 API Controller 工作中看到来自 HubContext 的调用。没有错误。只是在客户端没有结果。

已解决(某种程度上):

上面的代码确实可以发布时。虽然不能通过本地主机在 Visual Studio 开发环境中工作。没有错误,但客户端没有结果。将代码按原样发布到 Web 上的真实服务器确实有效。我从没想过会有什么不同,所以我从未尝试过。认为如果它在本地不起作用,那么它就无法发布。它现在正在运行,但我想知道为什么它不能在开发环境中通过 localhost 运行。无法使用断点等进行本地测试。

我有一种感觉,就是那个 signalr 虚拟目录。在本地运行与发布时有些不同。不确定是什么,但我看到很多帖子,例如 http://www.bitwisejourneys.com/signalr-hosting-in-iis-a-nasty-gotcha/ .现在阅读,看看是否有办法让它在本地和发布时都有效。

最佳答案

几天前我遇到了同样的问题。我花了 2 天时间找到解决方案并解决它。经过认真调查,问题的根本原因是我自定义设置的信号器依赖解析器。

最后我找到了this link那就是说:

Replacing the DependencyResolver

You can change the DependencyResolver to use your DI container of choice by setting GlobalHost.DependencyResolver.

NOTE: DO NOT override the global resolver in PreApplicationStart, it will not work, or it'll work only sometimes. Do it in PostApplicationStart (using WebActivator) or in Global.asax.

这里重要的地方是NOTE。当然,在 signalr 2.0 之后,此文档将弃用。所以我将这里的一些与新的 SignalR API 混合在一起。在新的 SignalR API 中不再使用 WebActivatorEx。首选 OwinStartup 而不是 WebActivator。

[assembly: OwinStartupAttribute(typeof(YourNamespace.Startup))]
namespace YourNamespace
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
//IoC container registration process
UnityConfig.RegisterComponents();

UnityConfig.Container.RegisterType<AHub, AHub>();

HubConfiguration config = new HubConfiguration();
config.EnableJavaScriptProxies = true;


//You should remove your dependency resolver code from here to Global.asax Application_Start method. Before setting the MVC properties.
//config.Resolver = new SignalrDefaultDependencyResolver(UnityConfig.Container); // your dependency resolver
//


app.MapSignalR(config);
}
}
}

在你的 global.asax 中

namespace YourNamespace
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
//Here your SignalR dependency resolver
GlobalHost.DependencyResolver = new SignalrDefaultDependencyResolver(UnityConfig.Container);


//other settings goes on
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}

我不想在这里发送所有代码,因为它显示了真正的问题。

所以对我来说,目前一切正常。依赖注入(inject)也可以。但不好的地方无处不在,我搜索过 David Fowler 说“这是设计使然”。我开始思考这个设计真的是必要的还是错误的。

希望它能帮助其他研究相同问题的人。

关于c# - SignalR 使用 GlobalHost.ConnectionManager.GetHubContext 从外部集线器调用客户端方法不起作用。但是从集线器内部调用确实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20561196/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com