0
votes

I built with Visual Studio 2017 a new project: "ASP.NET Core Web Application", then "API" project (ASP.NET Core 2.1) and I added SignalR as shown in this guide: https://code-maze.com/netcore-signalr-angular/

I'm able to launch the project with console by commenting the IIS Express profile in launchSettings.json as follows:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:64155",
      "sslPort": 0
    }
  },
  "profiles": {
    /*"IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": false,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },*/
    "WebApplication2": {
      "commandName": "Project",
      "launchBrowser": false,
      "applicationUrl": "http://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

In that case my (Ionic 4) client succeeds to connect:

[2020-08-22T10:41:21.539Z] Information: WebSocket connected to ws://localhost:5001/testHub?id=iUNCgO8mFOt7MjdQB-Cn_Q.

However, if i start debugging in IIS Express (without commeting the IISExpress profile, i tried also to use different values for launchBrowser property), the API is working but it seems that SignalR does not get launched.

EDIT:

This is the error shown on console when i launch Visual Studio project in IIS Express, the error is the same that i would get if i launch the client without starting the debug on Visual Studio 2017 so, since it is working when the project is launched as a console app, i thought I was missing some configuration.

Client error

What is the correct configuration to start SignalR keeping the IIS Express default launch method? I would like at the end to be able to publish solution and deploy it to IIS. Starting site from IIS is enough to setup SignalR configuration?

Also, is it possible at the end to log SignalR messages (handling events) in a given log file?

This is my Startup.cs file:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WebApplication2.SignalR;

namespace WebApplication2
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddSignalR();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());

            app.UseMvc();

            app.UseSignalR(routes =>
            {
                routes.MapHub<TestHub>("/testHub");
                
            });
        }
    }
}

And this is Program.cs:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

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

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

Is it possible to launch SignalR from Main method?

2
What does it mean that "signalr doesn't get started"? What happens when you attempt to connect? An error, if so which?Camilo Terevinto
@CamiloTerevinto, i updated question with client errorAnsharja
@Ansharja Looks like your client application is running on url which is different than the API url. Check the solution provided here: stackoverflow.com/questions/54762611/…Mohsin Mehmood
@MohsinMehmood: Thank you, that did not work but I found another answer with solution, i was calling the wrong URL once server was running on IIS profile, check my editAnsharja

2 Answers

0
votes

Have you tried adding cors to your startup.cs file?

0
votes

I posted solution as an edit but maybe it was not easy to see.

I found the correct answer to my issue here: https://stackoverflow.com/a/57654495/6449672: after moving to IIS Express profile I had to change the url called by client from http://localhost:5001/testHub to http://localhost:64155/testHub.