SignalR 使用 GlobalHost.ConnectionManager.GetHubContext 从外部集线器调用客户端方法不起作用.但是从集线器内部调用确实可以 [英] SignalR calling client method from outside hub using GlobalHost.ConnectionManager.GetHubContext doesn't work. But calling from within the hub does

查看:41
本文介绍了SignalR 使用 GlobalHost.ConnectionManager.GetHubContext 从外部集线器调用客户端方法不起作用.但是从集线器内部调用确实可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to call a client method from within a .net Web API controller action.

我可以这样做吗?

我能找到的唯一一个接近我想要做的帖子是这个:

The only post I can find that comes close to what I am looking to do is this one:

SignalR + 发帖通过操作方法向 Hub 发送消息

其中使用 GlobalHost.ConnectionManager.GetHubContext 从 asp.net MVC 控制器操作中发送消息.

In there a message is sent from within an asp.net MVC controller action using GlobalHost.ConnectionManager.GetHubContext.

当我在 Web API 操作中尝试该操作时,不会引发任何错误,但从未在客户端调用方法methodInJavascript".

When I try that inside my Web API action no errors are thrown, but the method "methodInJavascript" is never invoked on the client side.

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

当我在该操作中设置断点时,我看到正在到达并执行代码.不过,javascript 客户端没有任何反应.

When I set a break point inside that action, I see that the code is being reached and executed. Nothing happens on the javascript client side though.

为什么?Web API 在幕后如此不同以至于这行不通吗?有没有其他人尝试过并取得了成功?

Why? Is Web API so different under the hood that this won't work? Has anyone else tried it and had success?

当我从集线器内部"调用methodInJavascript"时,它运行良好.从 .net Web API 控制器操作中调用时将无法正常工作.

When I call the "methodInJavascript" from "within" my hub, it works perfectly. Just won't work when called from within a .net Web API controller action.

更新:

在研究了这个问题之后,我没有解决方案.我只能假设这样的例子中缺少一些东西 服务器到客户端的消息在 ASP.NET MVC 4 中没有通过 SignalR 和这个 从 WebAPI 控制器问题调用 SignalR 集线器 就像可能有一个额外的配置步骤来启用从 HubContext 或其他东西的调用.我最初在此处发布的代码与这些示例中出现的代码类似,但并未证明有任何缺陷.任何人都可以看到代码中的缺陷吗?从 html 调用工作.我在我的应用程序中广泛使用它并且从未遇到过问题.我从未在 API 控制器工作中看到来自 HubContext 的调用.没有错误.只是在客户端上没有结果.

After researching this issue I have no solution. I can only assume there is something missing from examples like this Server to client messages not going through with SignalR in ASP.NET MVC 4 and this calling SignalR hub from WebAPI controller issues like maybe there is an additional configuration step to enable calling from a HubContext or something. The code I initially posted here is like that which appears in those examples has not been demonstrated to be flawed in any way. Can anyone see a flaw in the code? Calling from html works. I do it extensively in my apps and never experience an issue. I have never seen a call from the HubContext in an API controller work. No errors. Just no results on the client.

已解决(某种):

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

Code above does indeed work as is when published. Does not work in Visual Studio dev environment via localhost though. No errors but no result on the client end. Publishing the code as is to a real server on the web does indeed work. I never thought there'd be a difference so I never tried. Figured if it didn't work locally it wouldn't work published. It's working live now but I'm wondering why it doesn't work via localhost in the dev environment. Can't test locally with breakpoints and such.

我有一种感觉,那就是信号器虚拟目录.本地运行与发布时有些不同.不知道是什么,但我看到很多帖子,比如 http://www.bitwisejourneys.com/signalr-hosting-in-iis-a-nasty-gotcha/.立即阅读,看看是否有办法让它在本地和发布时都能运行.

I have a feeling it's that signalr virtual directory. Something is different when run locally vs published. Not sure what but I see lots of posts like http://www.bitwisejourneys.com/signalr-hosting-in-iis-a-nasty-gotcha/. Reading now to see if there's a way to have it work both locally and published.

推荐答案

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

I came across with same issue couple days ago. That took my 2 days to find solution and resolve it. After some serious investigate the problems root cause was the signalr dependency resolver that I set customly.

最后我找到了这个链接,内容是这样的:

At the end I found this link and that was saying this:

替换依赖解析器

您可以更改 DependencyResolver 以使用您的 DI 容器通过设置 GlobalHost.DependencyResolver 来选择.

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

注意:不要覆盖 PreApplicationStart 中的全局解析器,它不会起作用,或者它只会在某些时候起作用.在做PostApplicationStart(使用 WebActivator)或在 Global.asax 中.

注意这里的重要位置.当然,在 signalr 2.0 之后,此文档不推荐使用.所以我将这里的一些内容与新的 SignalR API 混合在一起.在新的 SignalR API 中不再使用 WebActivatorEx.首选 OwinStartup 而不是 WebActivator.

The important place here the NOTE. Of course after signalr 2.0 this documentation become deprecated. So I mixed some of here with the new SignalR API. In new SignalR API not using WebActivatorEx anymore. OwinStartup preferred instead of 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

And in your 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);
        }
    }
}

我不想把所有的代码都发到这里来,以显示真正的问题.

I dont want to send all the code here, for showing up the real problem.

所以对我来说,现在一切正常.依赖注入也有效.但不好的地方到处都是我搜索的大卫福勒说它是设计的".我开始思考这个设计真的是必要的还是错误的.

So for me everything works fine for now. Dependency injection also works too. But the bad part is everywhere that I searched David Fowler was saying "Its by design". I started to think is this design really a necessary or a mistake.

希望它可以帮助其他研究相同问题的人.

Hope it helps somebody else who makes research for same problem.

这篇关于SignalR 使用 GlobalHost.ConnectionManager.GetHubContext 从外部集线器调用客户端方法不起作用.但是从集线器内部调用确实可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆