Using Visual Studio Codespaces with .NET Core
June 03, 2020 / super user
Reading Time: 10 minutes
The new releases of .NET Core make this an exciting time to be a .NET developer. Native cloud projects like Visual Studio Codespaces make it possible to use Visual Studio 19 with codespaces, allowing a developer to work entirely in the cloud. This capability is particularly useful now that .NET consulting often involves working from home and other remote locations, which often involves juggling multiple projects at the same time. Codespaces also allows developers to quickly create custom environments of each of their projects so they can allocate limited resources to tasks such as coding, debugging and adding new features to their projects.
.NET Framework is a software framework that Microsoft has developed primarily for the Microsoft Windows operating systems (OSs). It includes a class library called Framework Class Library (FCL) and allows several programming languages to use code written in other languages. NET Core supports four other platforms, including ASP.NET Core web apps, command-line apps, libraries and Universal Windows Platform.
.NET Framework programs run in a software environment called the Common Language Runtime (CLR), as opposed to the traditional hardware environment. CLR provides all the services that a programming environment requires such as exception handling, memory management and security. Code written in .NET Framework is therefore known as managed code.
Microsoft initially released .NET Framework in 2002, which has since evolved into a family of products that target many development platforms such as other OSs, embedded devices, mobile devices and web browser plug-ins. For example, .NET Core is a cross-platform framework for the Windows, Linux and macOS OSs. It uses open-source code and is primarily developed by Microsoft under the MIT License.
.NET Core 1.0 was released with Microsoft Visual Studio 2015 Update 3 in 2016, which enabled .NET Core development. Visual Studio 2017 15.3, released in 2017, included .NET Core 2.0, along with ASP.NET Core 2.0 and Entity Framework Core 2.0. .NET Core 3 was released in 2019, which supports the development of desktop applications, artificial intelligence (AI), machine learning (ML) and the Internet of Things (IoT). This version also implements the Visual Basic Runtime, Universal Windows Platform (UWP), Windows Forms and Windows Presentation Foundation (WPF).
The current version of .NET Core is v3.1 as of May 2020, which adds support for C++/CLI on Windows. .NET Core already supports VB.NET, C# and F# from previous versions. The next version of .NET Core will be .NET 5, which is scheduled for commercial release in September 2020. However, a preview of .NET 5 was released on March 16, 2020. Microsoft will deprecate .NET Framework when it formally releases .NET 5, which is why it’s removing the term “Core” from the name. It’s also skipping version 4 to avoid confusion with .NET Framework 4.x.
Microsoft Visual Studio is an integrated development environment (IDE) that develops both native and managed software in addition to mobile apps and web sites, including apps and services. It doesn’t compile source code into machine language the way traditional programming languages like C++ do. Instead, VS converts source code into formats like Common Intermediate Language (CIL) or Microsoft Intermediate Language (MSIL).
VS It uses many Microsoft software development platforms such as Windows API, Windows Forms, Windows Presentation Foundation, Microsoft Silverlight and Windows Store. Visual Studio (VS) includes an editor that supports Intellisense, a feature that completes code. It also has an integrated debugger that works at both the source and machine levels and a code profiler in addition to various design tools.
VS supports 36 programming languages including the following:
- Visual Basic .NET
It also supports other languages like M, Node.js, Python and Ruby via plug-ins. VS no longer supports J# and Java. Furthermore, VS’s code editor and debugger support virtually all programming language to varying degrees, provided a specific service exists for that language.
Microsoft originally released VS in 1997 as Visual Studio 97. It also released Visual Studio .NET in 2002, which introduced a managed code development environment with the .NET Framework. Microsoft has released many other versions of VS since then, generally naming the version after the year of its release.
Microsoft also released Visual Studio Online in 2013, which was a software-as-a-service (SaaS) offering of VS for Microsoft Azure. It expanded on the on-premises Team Foundation Server (TFS), now known as Azure DevOps Server, by making it available over the internet and using a rolling release model. These capabilities allowed users to subscribe to Visual Studio Online through the Azure portal. Microsoft renamed Visual Studio Online to Visual Studio Codespaces in 2020.
The current version of VS as of 2020 is Visual Studio 2019. It adds support for building .NET Core, ASP.NET and C++ projects in Windows-based codespaces. This feature allows developers to build, edit, debug and test applications for .NET Core projects, including console apps, libraries and web apps.
Visual Studio Codespaces provides a cloud-powered development environment that developers can access through various methods such as Visual Studio 2019, Visual Studio Code, browser-based editors and self-hosted environments. Codespaces provides many DevOps benefits to development environments like reliability and repeatability that have previously been available only to production environments. Furthermore, developers can personalize Codespaces so they can use their preferred configurations, processes and tools.
The .NET development team is working to enable .NET Core development in Codespaces. They have focused their initial efforts towards editing, testing and debugging for .NET Core application types such as class libraries, console apps and web apps. Windows Forms designer isn’t available for .NET Core at this time, although developers can edit project code with C#.
Visual Studio Codespaces features may be classified into the following categories:
- Remote development
- Personal configuration
- Custom configuration
Visual Studio Codespaces’ environment performs all the compute-intensive tasks that a software project requires, including compiling, debugging and restoring code. It also allows you to review existing projects and start new ones. Furthermore, the Codespaces environment configures the tools you need to work on a project such as source code, compiler, debugger, editor and runtime environment.
Developers can create cloud-hosted development environments like Codespaces quickly. They can create as many as they need up to the limits of their subscription, and dispose of them just as easily. Codespaces also manages these environments, reducing maintenance workload for developers. Furthermore, Codespaces allows you to register your own self-hosted environments and connect to them. This feature allows you to use existing environments, while still retaining some of Codespaces’ benefits.
Like other cloud-based platforms, Codespaces offers predictable pricing since you only pay for what you said. It also has an auto-suspend feature that prevents costs from exceeded a predefined limit. Moving project development to the cloud also frees limited resources on desktop computers. Codespaces is optimized to support large solutions that would be impractical to load locally. A Standard instance has four processor cores, 8GB of RAM and a fast SSD. Developers can also create a Premium instance with 8 cores and 16 GB of RAM for heavier workloads, which they can scale back when no longer needed.
Visual Studio Codespaces supports three editors, including Visual Studio Code, Visual Studio 2019 and a Visual Studio Code-based editor in your web browser. Windows environments are accessible from all three of these editors. Linux environments are accessible from Visual Studio Code and the browser-based editor. The Visual Studio Marketplace also provides extensions that enhance Codespaces’ editing capabilities. These options allow developers to use their preferred tools from any location.
Visual Studio Codespaces expands Visual Studio Code Remote Development extensions in both a conceptual and technical sense. Developers often set up development environments in containers on remote virtual machines (VMS) because it’s too disruptive to implement on a local machine. As long as that local machine is still usable, developers typically hesitate to change its environment with new development stacks.
Tools capable of creating a remote development environment already exists, although they usually increase the management overhead for VMs. Configuring this environment also complicates the process of onboarding new developers and switching projects. Visual Studio Codespaces removes these barriers by enabling multiple environments simultaneously, allowing developers to focus on productivity rather than environment setup. Developers who want to manage their own environment can do so with Visual Studio Code’s Remote Development.
Personal preferences are vital for making a cloud-hosted development environment feel familiar. Codespaces accomplishes this by implementing individual configuration on top of environment configuration. It also supports personal preferences for editors and terminals. Codespaces creates environments with user-specific dotfiles such as bashrc and gitconfig that automatically synchronize each developer’s settings and themes. Each environment that developers use has the look and feel they’re used to, regardless of its capabilities.
Codespaces accommodates a variety of projects or tasks by allowing developers to use configure their settings to common defaults or fine-tune environments with custom configurations from tools like Dockerfile and JSON. Developers can use their unique configurations that may be difficult to apply on a local machine, allowing them to quickly onboard a project. Furthermore, Codespaces’ easily reproducible development environments eliminate the common problem of that environment only working on the developer’s local machine.
Users can connect to Codespaces through Visual Studio 2019, Visual Studio Code and Codespaces’ built-in browser-based editor. Each of these codespaces is a separate environment, which may be public or private depending on the OS. They also have the same libraries and tools as VS’s standard workload. Additionally, Codespaces uses a devcontainer.json file that defines an environment’s configurations such as variables, forwarding ports and extensions for the web editor. These capabilities allow developers to easily configure a new machine, whether they’re a seasoned professional or completely inexperienced.
Developers often use more than one method of accessing Codespaces since they have different benefits. For example, Visual Studio Code as strong support for node.js, while Visual Studio provides a better experience for .NET Core and C++ developers. Users can access Codespaces’ built-in editor from any web browser. It supports all Visual Studio Code extensions, allowing users to build, edit, debug and navigate their apps directly from the web browser. Developers can now access their projects from any device with internet connectivity.
A new code space starts up and connects within seconds, with a pool of computing resources already available. Codespaces clones public repositories and customizes environments before users are connected, allowing them to quickly access their data. The total setup time depends on how long it takes to allocate resources from a hot pool, clone the repository and execute post-create commands. Users can also work with multiple projects at the same time by starting a separate code space for each project, since each code space has dedicated processing and storage resources.
All the necessary code and dependencies are installed in the code space, so developers don’t need to load the local machines with project-specific Software Development Kits (SDKs). Connecting to Codespaces now feels like users are connecting to a local machine due to the fast responses for coating, editing, debugging and testing. A thin client running on a local machine allows VS to overcome the latency that typically results from a slow connection.
Developers should experience their familiar coding environment once they connect to Codespaces with their .NET Core project. The latest productivity and code refactoring features from .NET are also available through the Codespaces service, ensuring that developer’s experience remains consistent with the previous platform.
The figure below shows a view of Codespaces for a weather forecasting program in C# and .NET:
Fig. 1: Codespaces view
The above figure shows how Codespaces supports the various ASP.NET Core projects. Developers can also use a preview version of Razor C#, HTML and Blazor/TagHelper, which complete lines of code automatically.
Codespaces allows users to hover their mouse over C#, HTML and Blazor/TagHelper tags to obtain more context when navigating Razor files. The following figure illustrates this feature:
Fig. 2: Hovering over tags
For example, hovering over the red keyword “IncrementBy” displays the text “Increment count IncrementBy”.
Developers can also learn more compiler errors in their Razor files, whether the code is in C#, HTML or Razor itself. The following figure illustrates this feature:
Fig. 3: Compiler errors
The items in red indicate a compiler error. The user can read the error message for each item by hovering over it.
Unit testing is another area where developers typically have very specific preferences. Microsoft enabled Codespaces’ test experience early in its development, allowing developers to preferred testing environment. For example, developers can continue to execute their tests with Test Explorer.
Developers should create a sufficiently large code space for projects that require iterative testing. This step allows testers to leverage the computing resources needed to complete these tests quickly, but they can also scale back the code space if needed to continue development. Increasing the compute resources available for just one loop can dramatically reduce testing time, depending on the code in that loop.
Building and Debugging
A developer’s personal experience in building and debugging with Codespaces is critical for determining that person’s willing to continue using it for these purposes. While Codespaces performs these tasks on the cloud, the local machine provides the interface. This architecture means that breakpoints, object inspection and watch windows still occur on the local machine as usual.
Developers will also encounter this experience when working on other client or console apps. They’ll still be able to interact with the app even though it’s actually running on a remote platform. This will also be the experience with other types of apps as Microsoft brings these client technologies online for Codespaces.
This functionality raises the issue of how developers can browse their .NET web apps from localhost when the app is actually running in the cloud. In particular, it may seem as if the browser’s launch settings would need to be changed. However, this isn’t necessary because Codespaces uses LiveShare technology to enable port forwarding from local host to the remote platform running the app. The only difference developers should notice is the few prompts needed to create and accept the trusted developer certificate on the code space for the local machine, which only occurs the first time the .NET app is launched. Once developers accept this certificate, they can interact with their local browser and debug code normally.
The support for the preview of Codespaces with VS will change quickly as Microsoft developers add functionality to it. As of May 2020, Microsoft recommends using Codespaces only for projects based on .NET Core, ASP.NET Core or library apps. Apps with GUIs that require designers will need to wait for later versions of Codespaces since no designers have been implemented yet. This restriction generally means that you should only develop middle-ware or backend apps in Codespaces at this time, since these apps typically have few dependencies other than a public NuGet feed.
The easiest apps to develop in Codespaces will therefore be those that are server-ready and run in relative isolation. For the time being, Azure services won’t be able to connect to Codespaces, including Azure Active Directory (AAD) and Key Vault. These restrictions are significant because both services are commonly used in Azure. AAD is a directory service is an identity service that provides single sign-on and multi-factor authentication for Azure users, while Key Vault safeguards cryptographic keys used by Azure apps and services. Additional features that are unavailable in the first preview of Codespaces include code actions with sub menus, CodeLens, ENC and NuGet Package Manager.
Microsoft plans to continue leveraging cloud computing for software development by providing Codespaces with more online functionality. Live Unit Testing (LUT) is one approach for powering Codespaces with cloud resources instead of local resources that Microsoft is currently testing. Targeting multiple OSs in certain scenarios is another capability that will benefit developers in future versions of Codespaces. This feature would allow developers to ensure their apps work in both Linux and Windows without leaving their development environment by simply changing the running target.
The ability to use cloud resources instead a local machine is one of the most obvious reasons for .NET Core developers to use VS Codespaces. Developers can quickly create a code space for a specific purpose and dispose of them just as easily when no longer needed. They will also appreciate the way Codespaces can have the same look and feel as their current environment. Real-time collaboration is another feature of Codespaces that benefits developers, especially shared sessions during debugging.
Microsoft is currently allowing developers to try out Codespaces with .NET Core projects for the purpose of obtaining feedback. Once they sign up, they can create their own code spaces and use Visual Studio 2019 to develop applications with cloud resources.