Deploying a plain ASP.NET Core 2.2 Web App in Azur

2020-03-13 05:05发布

问题:

I went to publish an asp.net core web application using Azure through the Publish screen in Visual Studio 2017. I used all of the defaults, but my app uses migrations so I had to tell it to run them in the publish profile.

When I try to access the site I get:
The page cannot be displayed because an internal server error has occurred.

I feel like there is something I need to do with the connection string and the ASPNETCORE_ENVIRONMENT variable.

I still have the default appsettings.json and appsettings.Development.json that you get when creating a new asp.net core web app. The appsettings.json is pointing to my local development db and the appsettings.Development.json is pointing to the azure db from the publish profile.

Or does the publish profile automatically take care of the connection string and I don't have to do any of the above?

回答1:

By default ASP.NET Core 2.2 apps are configured to use the new In Process hosting model. This will not be available on Azure in all regions until sometime in December 2018. They mention it here.

The solution for now is to add the following at the top of your web app's .csproj file:

  <PropertyGroup>
    <AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
  </PropertyGroup>


回答2:

You can check and set your app settings and connection strings in Application settings section of you service:



回答3:

One easy solution would be to put the azure-db connectionstring in the appsettings.json instead of the appsettings.Development.json.

When running the default generated asp.net core application in VS2017, you can find the launchSettings.Json file in the Properties folder with the profiles which will be run locally. There, under the profiles section you can see that the ASPNETCORE_ENVIRONMENT property is set to Development.

"WebApp": {
  "commandName": "Project",
  "launchBrowser": true,
  "applicationUrl": "http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

This means that if you define the local db connectionstring in your appsettings.Development.json it will use its connectionstring when running locally.

If you don't define the ASPNETCORE_ENVIRONMENT property then the runtime will only use the appsettings.json values (Runtime will set the ASPNETCORE_ENVIRONMENT to Production as default but if you don't have any appsettings.Production.json defined then only the values in appSettings.json will be used).

So when running the Azure Web App you don't need to specify the ASPNETCORE_ENVIRONMENT at all if you put the azure-db connectionstring in the appsettings.json.



回答4:

I deployed an AspNet Core 3.0 website to Azure and received an HTTP 500 server error.

This was in the web.config:

<system.webServer>
  <handlers>
    <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
  </handlers>
  <aspNetCore processPath="dotnet" arguments=".\BedtimeWeb.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>

I changed AspNetCoreModuleV2 to AspNetCoreModule and then the error became HTTP 502.3.

I then removed the hostingModel="InProcess" attribute and the website then started working correctly.