5

Trying to create a POC for azure signalr service. I found the github samples, which appeared to have some solid examples. I chose this one. Basically, my problem is that when I run the code locally, works like a champ with the localhost url, but when I try to run using an Azure SignalR Service using a url that I copied from azure portal keys which is in this format: Endpoint=<service_endpoint>;AccessKey=<access_key>;, I get an error stating that "Invalid URI: The URI scheme is not valid.". How do I transform the url from what I copy from keys and use it to connect to a signalr service?

class Program
{
    private const string DefaultHubEndpoint = "Endpoint=http://someFakesrsname.service.signlar.net;AccsssKey=thisseemslikeagoodaccesskeytouseformyquestion";//"http://localhost:5000/ManagementSampleHub";
    private const string Target = "Target";
    private const string DefaultUser = "User";

    static void Main(string[] args)
    {
        var app = new CommandLineApplication();
        app.FullName = "Azure SignalR Management Sample: SignalR Client Tool";
        app.HelpOption("--help");

        var hubEndpointOption = app.Option("-h|--hubEndpoint", $"Set hub endpoint. Default value: {DefaultHubEndpoint}", CommandOptionType.SingleValue, true);
        var userIdOption = app.Option("-u|--userIdList", "Set user ID list", CommandOptionType.MultipleValue, true);

        app.OnExecute(async () =>
        {
            var hubEndpoint = hubEndpointOption.Value() ?? DefaultHubEndpoint;
            var userIds = userIdOption.Values != null && userIdOption.Values.Count > 0 ? userIdOption.Values : new List<string>() { "User" };

            Console.WriteLine("hubEndpoint: " + hubEndpoint);
            Console.WriteLine("DefaultHubEndpoint: " + DefaultHubEndpoint);
            foreach (var userId in userIds)
            {
                Console.WriteLine("UserId: " + userId);
            }

            var connections = (from userId in userIds
                               select CreateHubConnection(hubEndpoint, userId)).ToList();

            await Task.WhenAll(from conn in connections
                               select conn.StartAsync());

            Console.WriteLine($"{connections.Count} Client(s) started...");
            Console.ReadLine();

            await Task.WhenAll(from conn in connections
                               select conn.StopAsync());
            return 0;
        });

        app.Execute(args);
    }

    static HubConnection CreateHubConnection(string hubEndpoint, string userId)
    {
        var url = hubEndpoint.TrimEnd('/') + $"?user={userId}";
        var connection = new HubConnectionBuilder().WithUrl(url).Build();
        connection.On(Target, (string message) =>
        {
            Console.WriteLine($"{userId}: gets message from service: '{message}'");
        });

        connection.Closed += async ex =>
        {
            Console.WriteLine(ex);
            Environment.Exit(1);
        };

        return connection;
    }
}
enter code here
CodeMonkey619
  • 53
  • 1
  • 3
  • You don't need to change your client code when connection to the Azure SignalR Service. It should continue pointing to your server and you just need to change your server to reference the Azure SignalR Server and use the Endpoint info on the server. See this doc for info on updating the server: https://learn.microsoft.com/en-us/azure/azure-signalr/signalr-quickstart-dotnet-core#add-azure-signalr-to-the-web-app – Brennan Apr 20 '20 at 15:27

0 Answers0