-1

I would like to change this:

var context = GlobalHost.ConnectionManager.GetHubContext<ControlHub>(); 
context.Clients.All.sentMessage(room, username, message);

to something like this using a variable as part of the call;

var variableHere = "<ControlHub>";
var context = GlobalHost.ConnectionManager.GetHubContext(variableHere)(); 
context.Clients.All.sentMessage(room, username, message);

I'm having trouble getting this pulled together. I' m thinking reflection or delegate work flow will get me there but have floundered in getting either approach to work.

I am trying to figure out how to insert a variable as part of a method/function call. In C# (specifically a MVC framework)

Any advise or assistance is appreciated.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
H D Dang
  • 23
  • 2
  • 1
    [ConnectionManager.GetHubContext](https://learn.microsoft.com/en-us/previous-versions/aspnet/jj908439(v=vs.118)?WT.mc_id=DT-MVP-5003235) – Reza Aghaei Jan 28 '21 at 17:40
  • you guys got me there! Thanks. Just need a few more brain cells (donated by others) to finish the solution. This was the magic: var context = GlobalHost.ConnectionManager.GetHubContext(hubString); – H D Dang Jan 28 '21 at 17:43
  • 1
    I reopened the question as the proposed duplicate is not a proper solution for this problem. – Reza Aghaei Jan 28 '21 at 17:49
  • @HongDongDang: `var context = GlobalHost.ConnectionManager.GetHubContext(typeof(ControlHub).Name); `. Does it work? –  Jan 28 '21 at 17:52

1 Answers1

0

You don't need reflection here. There is another overload for ConnectionManager.GetHubContext which accepts the hub name as parameter.

You can your code you use the other overload like this:

string hubName = "the hub name";
var context = GlobalHost.ConnectionManager.GetHubContext(hubName);

Just in case you want to extract the hub name based on the type, take a look at how it's being done internally in SignalR source code. If you need a similar functionality, change the below GetHubName extension methods to public extension methods and use it:

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. 
// See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNet.SignalR.Hubs
{
    internal static class HubTypeExtensions
    {
        internal static string GetHubName(this Type type)
        {
            if (!typeof(IHub).IsAssignableFrom(type))
            {
                return null;
            }

            return GetHubAttributeName(type) ?? GetHubTypeName(type);
        }

        internal static string GetHubAttributeName(this Type type)
        {
            if (!typeof(IHub).IsAssignableFrom(type))
            {
                return null;
            }

            // We can still return null if there is no attribute name
            return ReflectionHelper
             .GetAttributeValue<HubNameAttribute, string>(type, attr => attr.HubName);
        }

        private static string GetHubTypeName(Type type)
        {
            var lastIndexOfBacktick = type.Name.LastIndexOf('`');
            if (lastIndexOfBacktick == -1)
            {
                return type.Name;
            }
            else
            {
                return type.Name.Substring(0, lastIndexOfBacktick);
            }
        }
    }
} 
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398