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

查看:994
本文介绍了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:

<一个href=\"http://stackoverflow.com/questions/7549179/signalr-posting-a-message-to-a-hub-via-an-action-method?rq=1\">SignalR +通过操作方法发布消息到集线器

在有一个消息是从使用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");
    }

当我设置里面的动作一个破发点,我看到正在达到并执行code。什么也没有发生在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.

更新:

研究这个问题后,我已经没有办法了。我只能假设有东西从这样的<一个例子失踪href=\"http://stackoverflow.com/questions/13648605/server-to-client-messages-not-going-through-with-signalr-in-asp-net-mvc-4\">Server以客户端消息与SignalR不会通过ASP.NET MVC 4 这<一个href=\"http://stackoverflow.com/questions/16695975/calling-signalr-hub-from-webapi-controller-issues\">calling从控制器的WebAPI问题SignalR枢纽的喜欢,也许有一个额外的配置步骤,以使从HubContext什么打来的。在code我最初张贴在这里就是这样出现在这些例子还没有被证明在任何方面有缺陷。任何人都可以看到在code缺陷?从HTML调用工作。我做了广泛我的应用程序,从来没有遇到问题。我从来没有见过在一个API控制工作Hu​​bContext打电话。没有错误。在客户端上只是没有结果。

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.

解决(种):

code以上确实工作作为为公布时。没有在Visual Studio开发环境通过本地主机的工作,虽然。没有错误,但在客户最终没有结果。出版code为的就是能在网络确实工作在一个真正的服务器。我从来没有想到会出现如此我从来没有尝试过的差异。如果它没有在本地工作就不会出版工作想通。现在的工作生活,但我不知道为什么它不会在开发环境通过本地主机工作。不能与断点和这样的本地测试。

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.

我有一种感觉那就是signalr虚拟目录。当VS公布本地运行的东西是不同的。不知道,但我看到很多像<一个职位href=\"http://www.bitwisejourneys.com/signalr-hosting-in-iis-a-nasty-gotcha/\">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天找到解决方案,并解决它。经过一番认真调查问题的根本原因是signalr依赖解析器,我设置customly。

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

Replacing the DependencyResolver

您可以更改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后,该文件成为德precated 。所以我混了一些在这里与新SignalR API。在新的API SignalR不使用WebActivatorEx了。 OwinStartup preferred而不是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);
        }
    }
}

我不希望这里给所有的code,用于显示了真正的问题。

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天全站免登陆