The Heroku Command Line Interface (CLI) makes it easy to create and manage your Heroku apps directly from the terminal. It’s an essential part of using Heroku. Deploying dotnet core to Heroku. When I use dotnet run from the CLI (I'm on a mac) everything runs fine. I've included my Project.json below in case that helps.
This is the Heroku buildpack for ASP.NET Core.
Usage
The Buildpack will search through the repository's folders to locate a
Startup.cs
or Program.cs
file. If found, the .csproj
in the containing folder will be used in the dotnet publish <project>.csproj
command.If repository contains multiple Web Applications (multiple
Startup.cs
), PROJECT_FILE
and PROJECT_NAME
environment variables allow to choose project for publishing..NET Core latest stable
.NET Core edge
Previous releases
Available releases
More info
Entity Framework Core Migrations
You cannot run migrations with the
dotnet ef
commands once the app is built. Alternatives include:Enabling Automatic Migrations
![Core Core](/uploads/1/2/4/7/124796873/338292787.jpg)
- 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:
- Configure your connection string string appropriately, for example, for PostgreSQL:
sslmode=Prefer;Trust Server Certificate=true
Manually Running Migration Scripts on the Database
- 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.
Node.js and NPM
Example
Donation
If this project help you, you can give me a cup of coffee ☕
I've recently been curious about switching to a time API for my time stamps and removing any dependency the app might have on the server for a timestamp. Upon Googling I found some paid services, some free and of the free ones, I noticed one was hosted on Heroku. I've heard of Heroku, but never had a reason to attempt to use it. This was the perfect chance.
How I Created a Small 'GetTime' API
First, I created a free account on Heroku, nothing special. After verifying my email, I logged in to my Heroku Dashboard and up on the right hand corner, selected Create New App. I named it my company-api and out popped an app.
I decided on just plain, legacy PHP and a simple DateTime string passed thru JSON encode, just to get started. No authentication, no timezone, just a simple spit out if a request to the site came, like this:
I created a Git repo for this brand new file and pushed it out. Then, I went back to Heroku, Dashboard, My App and Deploy. I selected Github as my deploy 'resource' and selected the new repo I just made along with correpsonding branch.
I hit manual deploy and Heroku runs off to my GitHub repo, grabs the code, compiles and publishes.
It failed.
Beginner Troubles
My first problem was that Heroku could not determine what language I had chosen for my app (you'd think the <?php would give it away ...). You need either one of two things: a composer.json file or an index.php file (for legacy, like mine). I renamed my file to index.php and all I needed now was a 'builder pack'.
To add a builder pack, I went back to Heroku, Dashboard, My App and Settings. Under builder pack, I added one for: 'php'. Save settings and done.
I went back to Deploy, Manual Deploy and had a successful output. Yay! First Heroku app!
Adding Some Security
I want to make sure this API is receiving and sending JSON, so there's a few IF's I make the request go through before I hit logic on my PHP page. I also want to (lightly) secure the requests made to this API and monitor our usage of it for metrics information (and future investment). Since this itty bitty API is just relying on 1 index.php file, I figure this can be a sort of 'router' for future API's. So, this is what I added to the final PHP file:
- Verify the request is in JSON
- Require two variables passed:
- a secret api 'key'
- an api 'request'
- Use a switch statement to forward to needed method
The final, simple 1 page PHP Heroku API:
Â
Verifying the Response
I used Postman to send a raw JSON request to my Heroku app (used their default/free url). I wanted to make sure all my problems were resolved with this new toy, first, and then move on. Here's what the raw request and response look like on Postman:
Heroku + my PHP are responding nicely!
Handling the Request and Response in C#
So here's how I did the same request and received the response in C# (I use dotnet Core):
Â
If you see notes, yes, I decided to have some fun and create maybe two other functions, just like the one above, where I query some 'free' time api's until that 'success' flag turns true (of course, times are controlled to same format). After x tries, I reluctantly call my last function, that gets the server timestamp. As said, I wanted my timestamp to be independent of server. So, if it hits this last method, I also send myself an error email that the time API is failing.
In the future, I could use environment variables (config vars) more wisely, instead of hardcoding. There's also so much clean up to do, but this was a very fun intro into Heroku!