2

I am running a .NET Core SignalR server on a Console Application. With my code below, I can run the server. But since the clients of this server is another IP Address, I need to setup my SignalR server to run on the current PC's IP Address. Upon running this is what I get:

enter image description here

I need to change the http://localhost:5000 to current host pc's IP address which is 192.168.1.4 and set the port to 8090. Please see my code below:

Startup.cs

public class Startup
    {

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSignalR(hubOptions => {
                hubOptions.EnableDetailedErrors = true;
                hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
            });

            services.AddCors(options =>
            {
                options.AddPolicy("SomePolicy", builder => builder
                   .SetIsOriginAllowed(IsOriginAllowed)
                   .AllowAnyMethod()
                   .AllowAnyHeader()
                   .AllowCredentials()
                   .Build()
               );
            });
        }

        private bool IsOriginAllowed(string host)
        {
            var allowedOrigin = Configuration.GetSection("AcceptedOrigin").Get<string[]>();
            return allowedOrigin.Any(origin => origin == host);
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseCors("SomePolicy");
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<RimHub>("/rimHub");
            });
        }
    }

Program.cs

class Program
    {
        static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        private static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
    }

On the previous version of SignalR, we can use the .WithUrl('IPADDRESS'), but with this new version, I do not know how to implement it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ibanez1408
  • 4,550
  • 10
  • 59
  • 110
  • Thanks @gurkan for wanting to edit questions here. However, please note that proper nouns (e.g. .NET Core SignalR) are not themselves code, and should not be formatted as such. The `inline code device` is useful for small code snippets, short pieces of config, and machine IO. – halfer Aug 17 '20 at 07:30
  • It's worth noting also that sentence case is preferred for titles here (see the front page). – halfer Aug 17 '20 at 07:32
  • @halfer I prefer like that but of course my prefers are can't be reference for the community. You are right though using it is noticeable at first sight. – gurkan Aug 17 '20 at 08:55
  • Thanks @gurkan. Yes, if you can use standard formatting, rather than personal formatting preferences, that would be good. I do this too - for example I have to stop myself changing US spellings to UK ones! – halfer Aug 17 '20 at 08:57
  • 1
    @halfer Oh yes, thanks for your kind suggestions. I'll watch out your notices. – gurkan Aug 17 '20 at 09:09

1 Answers1

1

Because you are using WebHost.CreateDefaultBuilder, Kestrel is used as web server (see Microsoft Documentation).

You can find details at Microsoft to configure Kestrel (e.g. via appsettings.json or in Startup). Examples can also be found here.

How did you use .WithUrl on the server side in the past? I would rather think that the method is used on the client side.

J.Lehmann
  • 71
  • 3