Search code examples
c#asp.netasp.net-core.net-coreiis-express

Unable to connect to web server IIS Express - ASP.NET CORE


My team has developed a asp.net core application in a windows vm on their macbook. I have a windows machine so, I was asked to update the App URL and Launch browser from localhost to my windows IP address. However, when I try to run the application it throws error and says 'Unable to connect to web server IIS Express'.

I've tried to run visual studio as administrator but still the error doesn't seems to go away. I'm attaching the screenshot for the reference. I would appreciate any help thank you.

enter image description here

By the way here's how my program.cs file look like.

    public class Program
    {
        /// <summary>
        /// Thread Main.
        /// </summary>
        /// <param name="args">Command Line args.</param>
        public static void Main(string[] args)
        {
            // Configuration builder to load ASPNETCORE env variables
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddEnvironmentVariables(prefix: "ASPNETCORE_")
                .Build();

            // WebHost Configuration
            // var host = WebHost.CreateDefaultBuilder(args)

            var host = MfHostBuilder.CreateHostBuilder(args).UseConfiguration(config)
               // .UseUrls("http://0.0.0.0:7785")
                .UseStartup<Startup>()
                //.UseKestrel()
                .Build();

            // Start and run the host.
            host.Run();
        }
    }
}

and here's my startup.cs file looks like.

public class Startup : IntegrationService
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<GenetecServerControllerOptions>();
        services.ConfigureOptions<ConfigureGenetecServerControllerOptions>();
        services.AddGenetec();
        services.AddSingleton<ServerProcessorOptions>();
        services.ConfigureOptions<ConfigureServerProcessorOptions>();
        base.ConfigureServices(services);
    }
}

Solution

  • You need to add IIS integration into your app, this will configure what is required for IIS: This is what I have and use in my Program.cs

    return Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder => {
                webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                webBuilder.UseIISIntegration();
                webBuilder.UseStartup<Startup>();
            });