This is a step-by-step to setup a .NET Core (2.0+) app using Visual Studio Code (non-Windows environment, e.g. MacOS), and how to control code with Github and auto-deploy to Heroku.
Found anything wrong or that can be improved? Please contribute!
- .NET Core & SDK version 2.0+
- Visual Studio Code
- C# Extension for Visual Studio Code
On Terminal, run dotnet --version
to check the .NET version.
- Create a new Github repo, add
.gitignore
forVisual Studio
- Clone repo locally
- Open with Visual Studio Code
-
Using Visual Studio Code Integrated Terminal, run
dotnet new webapi -n ForgeSample
to create a new project. -
As a standard on Forge samples, adjust the port number
3000
. Under.vscode\launch.json
file, addASPNETCORE_URLS
:"env": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_URLS" : "http://localhost:3000", },
-
Add support for static files, like
.html
,.js
and.css
. UnderStartup.cs:Configure()
, add the following (see sample):app.UseDefaultFiles(); app.UseStaticFiles();
-
Press F5 (debug), the application should compile and open the browser. If the debugger is not set, at the top-center, where it prompts for Select Environment select .NET Core. A
.vscode
folder is created withlaunch.json
andtask.json
. Press F5 again, the app should run athttps://localhost:3000/api/values
(Values is the default controller on webapi projects template)
Requires Heroku CLI.
Using the Integrated Terminal, run:
It may be required to stop debugging to unlock the terminal, press Ctrl + C
-
heroku login
-
Create app using .NET Code Buildpack (there are many options of buildpack, this was one choice):
heroku create ForgeSampleAppName --buildpack https://github.com/jincod/dotnetcore-buildpack.git
-
Go to Heroku Dashboard and select the newly create app. Under Deploy menu, go to Deployment method and select Connect to Github. Next, go to Connect to Github and search the respective repository and click Connect. Next, go to Automatic deploys and and Enable Automatic Deploys for the brach (e.g.
master
). -
Go to the app Settings (on Heroku Dashboard), under Config Vars click on Reveal Config Vars and add your FORGE_CLIENT_ID, FORGE_CLIENT_SECRET and FORGE_CALLBACK_URL (if 3-legged).
As the Github repo is connected to Heroku, all new commits to the selected branch will automatically deployed to the Heroku app.
- As
launch.json
contains the ID/Secret, add.vscode
folder to.gitignore
- Commit & Push to Github
At the Heroku dashboard, for this app, see Logs, it should say: Build started by username, then Build succeeded. Now the app should live, try the same Values endpoint.
Prepare this project to use Forge:
-
On the Integrated Terminal, run
dotnet add ForgeSample package Autodesk.Forge
A popup should appear asking to Restore, which downloads the newly added package. Confirm that under ForgeSample.csproj the newly added line
<PackageReference Include="Autodesk.Forge" Version="1.2.0" />
(the actual varion may be higher) -
Under .vscode folder, open launch.json, search for
ASPNETCORE_ENVIRONMENT
env var and add the Forge variables, as shown below:
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS" : "http://localhost:3000",
"FORGE_CLIENT_ID": "your client id here",
"FORGE_CLIENT_SECRET": "your client secret here",
"FORGE_CALLBACK_URL": "http://localhost:3000/api/forge/callback/oauth"
}
Now the project is ready for Forge! See Controllers/OAuthController.cs for an example (2-legged token). With this controller, the /api/forge/oauth/token
should also work (localhost and live on Heroku). Static files should be placed on wwwroot
folder.
Create a app.json
and specify the buildpack for .NET Core:
"buildpacks": [
{
"url": "https://github.com/jincod/dotnetcore-buildpack.git"
}
]
This is a personal choice, I prefer to NOT open a new browser/tab when start debugging, this can be disabled under launch.json
, change to false
. You can start debugging with Cmd + Shift + F5.
"launchBrowser": {
"enabled": false,
...
}
If you want to reduce the output, here are a few tips:
- To ignore messages when modules are loading, open
launch.json
and add (underconfigurations
):
"logging":{
"moduleLoad": false
},
- Change the logging level to
error
at theappsettings.development.json
:
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Error",
"Microsoft": "Error"
}
}
}
Follow this workaround. In summary, create the file ~/Library/LaunchAgents/UseSharedCompilation.plist
with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>UseSharedCompilation</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>launchctl setenv UseSharedCompilation false</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
This sample is licensed under the terms of the MIT License. Please see the LICENSE file for full details.
Augusto Goncalves @augustomaia, Forge Partner Development