Asked by:
The name GlobalHost does not exist in this context

Question
-
User-595439866 posted
public static void NotifyUI(string msg, int count)
{
var message = "Process completed for " + msg;
var hubContext = GlobalHost.ConnectionManager.GetHubContext<Chat>();
hubContext.Clients.All.sendMessage(string.Format(message), count);
}i am getting error on GlobalHost does not exist in this context while tring to call hub class method from controller
Tuesday, August 8, 2017 7:52 PM
All replies
-
User-335504541 posted
Hi Altaf11221,
Altaf11221
GlobalHost does not exist in this contextThe GlobalHost's namespace is Microsoft.AspNet.SignalR.
Do you add reference in your controller ?
using Microsoft.AspNet.SignalR;
You could install it by using
install-package Microsoft.AspNet.SignalR
in Package Manager Console.
Best Regards,
Billy
Wednesday, August 9, 2017 7:44 AM -
User-595439866 posted
Yes i have installed
using Microsoft.AspNetCore.SignalR;
Wednesday, August 9, 2017 8:10 AM -
User-335504541 posted
Hi Altaf111221,
I have tried to call the code in a controller, it works fine.
Do you have any other error message in your project? What's the version of the SignalR you are using?
Could you use GlobalHost in other place such as your hub class?
Best Regards,
Billy
Wednesday, August 9, 2017 9:48 AM -
User-595439866 posted
i am signalr with asp.net core with the following
using Microsoft.AspNetCore.SignalR;
Sunday, August 13, 2017 7:56 AM -
User61956409 posted
Hi Altaf11221,
I install Gray.Microsoft.AspNetCore.SignalR.Server in my .net core web application, and I can execute the following code in controller action to broadcast message to connecting clients.
IHubContext hubContext = Startup.ConnectionManager.GetHubContext<ChatHub>(); hubContext.Clients.All.addNewMessageToPage("robot", "hi");
And the following is my Startup.cs
public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); services.AddSignalR(options=> { options.Hubs.EnableDetailedErrors = true; }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public static IConnectionManager ConnectionManager; public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseWebSockets(); app.UseSignalR(); ConnectionManager = serviceProvider.GetService<IConnectionManager>(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }
With Regards,
Fei Han
Tuesday, August 15, 2017 9:47 AM