1
0
mirror of synced 2025-12-20 02:19:14 -05:00
Files
docs/content/codespaces/setting-up-your-project-for-codespaces/setting-up-your-dotnet-project-for-codespaces.md
2022-10-25 20:14:27 +01:00

9.9 KiB
Raw Blame History

title, shortTitle, allowTitleToDifferFromFilename, intro, redirect_from, versions, topics, hasExperimentalAlternative, hidden
title shortTitle allowTitleToDifferFromFilename intro redirect_from versions topics hasExperimentalAlternative hidden
Setting up your C# (.NET) project for GitHub Codespaces Setting up your C# (.NET) project true Get started with your C# (.NET) project in {% data variables.product.prodname_github_codespaces %} by creating a custom dev container.
/codespaces/getting-started-with-codespaces/getting-started-with-your-dotnet-project
fpt ghec
* *
Codespaces
true true

Introduction

This guide shows you how to set up your C# (.NET) project {% data reusables.codespaces.setting-up-project-intro %}

Prerequisites

  • You should have an existing C# (.NET) project in a repository on {% data variables.product.prodname_dotcom_the_website %}. If you don't have a project, you can try this tutorial with the following example: https://github.com/2percentsilk/dotnet-quickstart.
  • You must have {% data variables.product.prodname_github_codespaces %} enabled for your organization.

Step 1: Open your project in a codespace

  1. Under the repository name, use the {% octicon "code" aria-label="The code icon" %} Code dropdown menu, and in the Codespaces tab, click Create codespace on main.

New codespace button

When you create a codespace, your project is created on a remote VM that is dedicated to you. By default, the container for your codespace has many languages and runtimes including .NET. It also includes a common set of tools like git, wget, rsync, openssh, and nano.

{% data reusables.codespaces.customize-vcpus-and-ram %}

Step 2: Add a dev container configuration to your repository from a template

The default development container, or "dev container," for {% data variables.product.prodname_github_codespaces %} comes with the latest .NET version and common tools preinstalled. However, we recommend that you configure your own dev container to include all of the tools and scripts that your project needs. This will ensure a fully reproducible environment for all {% data variables.product.prodname_github_codespaces %} users in your repository.

{% data reusables.codespaces.setup-custom-devcontainer %}

{% data reusables.codespaces.command-palette-container %}

  1. For this example, click C# (.NET). If you need additional features you can select any container thats specific to C# (.NET) or a combination of tools such as C# (.NET) and MS SQL. Select C# (.NET) option from the list
  2. Click the recommended version of .NET. .NET version selection
  3. Accept the default option to add Node.js to your customization. Add Node.js selection {% data reusables.codespaces.rebuild-command %}

Anatomy of your dev container

Adding the C# (.NET) dev container template adds a .devcontainer folder to the root of your project's repository with the following files:

  • devcontainer.json
  • Dockerfile

The newly added devcontainer.json file defines a few properties that are described after the sample.

devcontainer.json

{
	"name": "C# (.NET)",
	"build": {
		"dockerfile": "Dockerfile",
		"args": {
			// Update 'VARIANT' to pick a .NET Core version: 2.1, 3.1, 5.0
			"VARIANT": "5.0",
			// Options
			"INSTALL_NODE": "true",
			"NODE_VERSION": "lts/*",
			"INSTALL_AZURE_CLI": "false"
		}
	},

	// Set *default* container specific settings.json values on container create.
	"settings": {
		"terminal.integrated.shell.linux": "/bin/bash"
	},

	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [
		"ms-dotnettools.csharp"
	],

	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	// "forwardPorts": [5000, 5001],

	// [Optional] To reuse of your local HTTPS dev cert:
	//
	// 1. Export it locally using this command:
	//    * Windows PowerShell:
	//        dotnet dev-certs https --trust; dotnet dev-certs https -ep "$env:USERPROFILE/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere"
	//    * macOS/Linux terminal:
	//        dotnet dev-certs https --trust; dotnet dev-certs https -ep "${HOME}/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere"
	//
	// 2. Uncomment these 'remoteEnv' lines:
	//    "remoteEnv": {
	// 	      "ASPNETCORE_Kestrel__Certificates__Default__Password": "SecurePwdGoesHere",
	//        "ASPNETCORE_Kestrel__Certificates__Default__Path": "/home/vscode/.aspnet/https/aspnetapp.pfx",
	//    },
	//
	// 3. Start the container.
	//
	// 4. Drag ~/.aspnet/https/aspnetapp.pfx into the root of the file explorer.
	//
	// 5. Open a terminal in VS Code and run "mkdir -p /home/vscode/.aspnet/https && mv aspnetapp.pfx /home/vscode/.aspnet/https".
	//

	// Use 'postCreateCommand' to run commands after the container is created.
	// "postCreateCommand": "dotnet restore",

	// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
	"remoteUser": "vscode"
}
  • name - You can name our dev container anything, this is just the default.
  • build - The build properties.
    • dockerfile - In the build object, dockerfile contains the path to the Dockerfile that was also added from the template.
    • args
      • variant: This file only contains one build argument, which is the .NET Core version that we want to use.
  • settings - These are {% data variables.product.prodname_vscode %} settings.
    • terminal.integrated.shell.linux - While bash is the default here, you could use other terminal shells by modifying this.
  • extensions - These are extensions included by default.
    • ms-dotnettools.csharp - The Microsoft C# extension provides rich support for developing in C#, including features such as IntelliSense, linting, debugging, code navigation, code formatting, refactoring, variable explorer, test explorer, and more.
  • forwardPorts - Any ports listed here will be forwarded automatically. For more information, see "Forwarding ports in your codespace."
  • postCreateCommand - Use this to run commands that aren't defined in the Dockerfile, after your codespace is created.
  • remoteUser - By default, youre running as the vscode user, but you can optionally set this to root.

Dockerfile

# [Choice] .NET version: 5.0, 3.1, 2.1
ARG VARIANT="5.0"
FROM mcr.microsoft.com/vscode/devcontainers/dotnetcore:0-${VARIANT}

# [Option] Install Node.js
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"
RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Option] Install Azure CLI
ARG INSTALL_AZURE_CLI="false"
COPY library-scripts/azcli-debian.sh /tmp/library-scripts/
RUN if [ "$INSTALL_AZURE_CLI" = "true" ]; then bash /tmp/library-scripts/azcli-debian.sh; fi \
    && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

You can use the Dockerfile to add additional container layers to specify OS packages, node versions, or global packages we want included in our container.

Step 3: Modify your devcontainer.json file

With your dev container configuration added and a basic understanding of what everything does, you can now make changes to customize your environment further. In this example, you'll add properties to install extensions and your project dependencies when your codespace launches.

  1. In the Explorer, select the devcontainer.json file from the tree to open it. You might have to expand the .devcontainer folder to see it.

    devcontainer.json file in the Explorer

  2. Update your the extensions list in your devcontainer.json file to add a few extensions that are useful when working with your project.

    "extensions": [
     	  "ms-dotnettools.csharp",
     	  "streetsidesoftware.code-spell-checker",
       ],
    
  3. Uncomment the postCreateCommand to restore dependencies as part of the codespace setup process.

    // Use 'postCreateCommand' to run commands after the container is created.
    "postCreateCommand": "dotnet restore",
    

    {% data reusables.codespaces.more-info-devcontainer %}

{% data reusables.codespaces.rebuild-command %}

{% data reusables.codespaces.rebuild-reason %}

  1. Check your changes were successfully applied by verifying the "Code Spell Checker" extension was installed.

    Extensions list

Step 4: Run your application

In the previous section, you used the postCreateCommand to install a set of packages via the dotnet restore command. With our dependencies now installed, we can run our application.

  1. Run your application by pressing F5 or entering dotnet watch run in your terminal.

  2. When your project starts, you should see a "toast" notification message at the bottom right corner of {% data variables.product.prodname_vscode_shortname %}, containing a prompt to connect to the port your project uses.

    Port forwarding "toast" notification

Step 5: Commit your changes

{% data reusables.codespaces.committing-link-to-procedure %}

Next steps

You should now be ready start developing your C# (.NET) project in {% data variables.product.prodname_github_codespaces %}. Here are some additional resources for more advanced scenarios.

{% data reusables.codespaces.next-steps-adding-devcontainer %}