.NET Core Fundamentals

Your comprehensive guide to building modern applications with .NET Core.

Introduction to .NET Core

Welcome to the core fundamentals of .NET Core, a powerful, open-source, cross-platform framework for building a wide range of applications. Developed by Microsoft, .NET Core is designed for high performance, modularity, and cloud-native development.

Key characteristics of .NET Core include:

  • Cross-Platform: Runs on Windows, macOS, and Linux.
  • Open-Source: Developed and maintained by Microsoft and the community.
  • High Performance: Optimized for speed and efficiency.
  • Modular: Applications are built using NuGet packages, allowing for lean deployments.
  • Cloud-Native: Designed with microservices, containerization, and cloud deployment in mind.

Architecture Overview

.NET Core is built on a foundation of the Common Language Runtime (CLR) and the .NET Standard library. It supports multiple languages like C#, F#, and Visual Basic.

  • Runtime: The managed execution environment that handles memory management, exception handling, and other services.
  • Base Class Library (BCL): Provides fundamental types and utility classes.
  • .NET Standard: A formal specification of .NET APIs that are intended to be available on all .NET implementations.

The .NET SDK

The .NET Software Development Kit (SDK) is essential for developing .NET Core applications. It includes the .NET runtime, the C# compiler, and the dotnet command-line interface (CLI).

To check your .NET Core SDK version, use the following command in your terminal:

dotnet --version

Common SDK commands include:

  • dotnet new: Creates a new project.
  • dotnet build: Compiles a project.
  • dotnet run: Runs a project.
  • dotnet publish: Publishes an application for deployment.

Project Structure

A typical .NET Core project is defined by a .csproj (or .vbproj, .fsproj) file, which uses a newer SDK-style format. This file defines project metadata, dependencies, and build settings.

A simple console application structure might look like this:


MyConsoleApp/
├── MyConsoleApp.csproj
├── Program.cs
└── obj/ (generated by the build)
└── bin/ (build output)
    └── Debug/
        └── net6.0/
            └── MyConsoleApp.dll
                

Dependency Injection

.NET Core has built-in support for Dependency Injection (DI), a design pattern that promotes loose coupling and testability. The framework manages the creation and lifetime of services.

You can configure services in your application's startup class:


// In Startup.cs or Program.cs (depending on .NET version)
public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IMyService, MyService>();
    services.AddControllers();
}
                
Note: Dependency Injection is a cornerstone of modern .NET development, especially for web applications.

Configuration Management

Configuration settings can be loaded from various sources, including JSON files, environment variables, command-line arguments, and Azure Key Vault. The Microsoft.Extensions.Configuration package provides the necessary tools.

Example configuration in appsettings.json:


{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "MyCustomSetting": "SomeValue"
}
                

Logging

.NET Core provides a flexible logging API that can be configured to output to various destinations (console, file, database, etc.).

Injecting an ILogger into a class:


public class MyController : Controller
{
    private readonly ILogger<MyController> _logger;

    public MyController(ILogger<MyController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation("Index action accessed.");
        return View();
    }
}
                

Environments

.NET Core applications can be configured to run in different environments (e.g., Development, Staging, Production). This allows for varying configurations and behaviors.

You can set the ASPNETCORE_ENVIRONMENT environment variable.

Tip: Use different appsettings.{EnvironmentName}.json files to manage environment-specific settings.

Cross-Platform Development

Develop and deploy your .NET Core applications on any operating system. The unified .NET platform ensures consistency across Windows, macOS, and Linux.

Hosting Applications

.NET Core applications can be self-contained (including the runtime) or framework-dependent (relying on a system-installed runtime). They can be hosted in various ways:

  • IIS on Windows
  • Nginx or Apache on Linux
  • Docker containers
  • Azure App Service
  • Kubernetes
Warning: Ensure you choose the appropriate hosting model based on your deployment environment and requirements.