This is the Heroku buildpack for ASP.NET Core.
The Buildpack supports C# and F# projects. It searchs through the repository's folders to locate a Startup.*
or Program.*
file. If found, the .csproj
or .fsproj
in the containing folder will be used in the dotnet publish <project>
command.
If repository contains multiple Web Applications (multiple Startup.*
or Program.*
), PROJECT_FILE
and PROJECT_NAME
environment variables allow to choose project for publishing.
heroku buildpacks:set jincod/dotnetcore
heroku buildpacks:set https://github.com/jincod/dotnetcore-buildpack
heroku buildpacks:set https://github.com/jincod/dotnetcore-buildpack#preview
heroku buildpacks:set https://github.com/jincod/dotnetcore-buildpack#version
Available releases
More info
You cannot run migrations with the dotnet ef
commands using .NET Local Tools once the app is built. Alternatives include:
- Ensure there is a .NET local tool manifest file(dotnet-tools.json) which specifies the dotnet-ef tool:
dotnet new tool-manifest
dotnet tool install dotnet-ef
- Ensure the
ASPNETCORE_ENVIRONMENT
environment variable is set toProduction
. ASP.NET Core scaffolding tools may create files that explicitly set it toDevelopment
. Heroku config will override this (heroku config:set ASPNETCORE_ENVIRONMENT=Production
). - Configure your app to automatically run migrations at startup by adding the following to the
.csproj
file:
<Target Name="PrePublishTarget" AfterTargets="Publish">
<Exec Command="dotnet ef database update" />
</Target>
- Manually run SQL scripts generated by Entity Framework Core in your app's database. For example, use PG Admin to connect your Heroku Postgres service.
If you are using Heroku Postgres addon, then Heroku provides the database connection string as environment variable DATABASE_URL
. In order to use this in an Npgsql connection string, do something like following:
using System.Text.RegularExpressions;
...
// Replace AppContext with your own DbContext Class
builder.Services.AddDbContext<AppContext>(options =>
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production")
{
var m = Regex.Match(Environment.GetEnvironmentVariable("DATABASE_URL")!, @"postgres://(.*):(.*)@(.*):(.*)/(.*)");
options.UseNpgsql($"Server={m.Groups[3]};Port={m.Groups[4]};User Id={m.Groups[1]};Password={m.Groups[2]};Database={m.Groups[5]};sslmode=Prefer;Trust Server Certificate=true");
}
else // In Development Environment
{
// So, use a local Connection
options.UseNpgsql(builder.Configuration.GetValue<string>("CONNECTION_STRING"));
}
});
heroku buildpacks:set jincod/dotnetcore
heroku buildpacks:add --index 1 heroku/nodejs
Using Multiple Buildpacks for an App
heroku config:set HEROKUISH=true
If you have multiple projects in a monorepo and wish to treat them as separate process types:
- Build the projects simultaneously by setting the
PROJECT_FILE
config var to a solution file that references them. - Provide a custom
Procfile
that enumerates the desired process types, for example:
web: cd $HOME/heroku_output && ./MySolution.Web
worker: cd $HOME/heroku_output && ./MySolution.Worker
If this project help you, you can give me a cup of coffee ☕