Search code examples
c#iiswebdeploy

Development Mode Error after web deploying ASP.Net Core to ISS


I deployed my ASP.NET Core app (v5) to a local ISS, however when I try to access one of my API Controllers thought the link : http://my.website/api/controllersapi. I get the following Error

enter image description here

I already have the environment variable as Development, so I'm not really sure what else I should do. Here are some classes from my app so you can have an idea on how it's configured.

Program.cs:

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

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();

                   webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                    webBuilder.UseIISIntegration();
                });
    }

StartUp.cs:

public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<AutoPatchManagementContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("AutoPatchManagementContext")));

            services.AddSignalR();
            services.AddControllersWithViews().AddNewtonsoftJson(options =>
               options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
           );

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();
           
            app.UseEndpoints(endpoints =>
            {
                
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Homepage}/{id?}");
                endpoints.MapHub<MyHub>("/myHub");
            });
        }

launchSetting.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:53043",
      "sslPort": 44373
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "AutoPatchManagement": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000;http://localhost:53043",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Here is my IISProfile.pubxml

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish>http://my.website</SiteUrlToLaunchAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>14712f10-a37a-441a-ad5e-e37f85eff5f4</ProjectGuid>
    <SelfContained>false</SelfContained>
    <MSDeployServiceURL>localhost</MSDeployServiceURL>
    <DeployIisAppPath>my.website</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>InProc</MSDeployPublishMethod>
    <EnableMSDeployBackup>False</EnableMSDeployBackup>
    <EnableMsDeployAppOffline>True</EnableMsDeployAppOffline>
    <TargetFramework>net5</TargetFramework>
  </PropertyGroup>
</Project>

I've searched about this error and the only solution that I have found was to change de enviorment variable to Development, which already is. Is there something wrong with my current configurations?

Thank you in advance


Solution

  • You need to add development in web.config, not any other file. Web.config should look like this:

    <aspNetCore processPath="dotnet" arguments=".\TestWebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
       <environmentVariables>
           <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
       </environmentVariables>
    </aspNetCore>