Deploying .NET Core Applications
This section provides comprehensive guidance on deploying your .NET Core applications to various environments. Whether you're targeting Windows, Linux, macOS, or cloud platforms, understanding deployment strategies is crucial for successful application delivery.
Key Deployment Scenarios
Self-Contained Deployments
A self-contained deployment includes the .NET Core runtime and libraries along with your application. This approach allows your application to run on any machine, even if it doesn't have the .NET Core runtime installed. This is often preferred for maximum portability.
Advantages:
- No need to pre-install .NET Core runtime on the target machine.
- Guarantees the exact .NET Core version used for development.
Considerations:
- Larger deployment package size.
Steps:
- Run
dotnet publish -r
--self-contained true - Deploy the published output to your target machine.
For a list of runtime identifiers, see .NET Core Runtime Identifiers.
Framework-Dependent Deployments
A framework-dependent deployment relies on a shared .NET Core runtime installed on the target machine. Your application package only contains your application code and its dependencies. This results in a smaller deployment package but requires the .NET Core runtime to be present.
Advantages:
- Smaller deployment package size.
- Leverages existing .NET Core installations.
Considerations:
- Requires the .NET Core runtime to be installed on the target machine.
Steps:
- Run
dotnet publish -r
(or simply--self-contained false dotnet publish
as this is the default). - Deploy the published output.
Ensure the target machine has a compatible .NET Core runtime installed.
Deployment Targets
Windows Deployment
Deploying to Windows can be done in various ways, including standalone executables, IIS integration, or Docker containers. For IIS, ensure you have the ASP.NET Core Module installed.
To create a single executable for Windows, use:
dotnet publish -r win-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true
Linux Deployment
Linux is a first-class citizen for .NET Core. You can deploy as framework-dependent or self-contained. Common targets include Ubuntu, CentOS, and Alpine Linux. Ensure your target Linux distribution is supported.
To create a self-contained executable for Ubuntu (x64):
dotnet publish -r ubuntu.20.04-x64 --self-contained true
macOS Deployment
Similar to Linux, .NET Core runs natively on macOS. Deployments can be framework-dependent or self-contained. Be mindful of macOS Gatekeeper when distributing applications.
To create a self-contained executable for macOS (x64):
dotnet publish -r osx.11.0-x64 --self-contained true
Containerization with Docker
Docker is an excellent choice for packaging and deploying .NET Core applications consistently across different environments. Microsoft provides official Docker images for .NET Core.
A simple Dockerfile might look like this:
# Use an official .NET runtime as a parent image
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app
# Copy csproj and restore dependencies
COPY *.csproj ./
RUN dotnet restore
# Copy and build the application
COPY . .
RUN dotnet publish -c Release -o out
# Use a smaller base image for the runtime
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "YourApp.dll"]
Build and run your Docker image:
docker build -t my-dotnet-app .
docker run -p 8080:80 my-dotnet-app
Cloud Platform Deployments
Deploying to cloud platforms like Azure, AWS, or Google Cloud offers scalability, reliability, and managed services. Each platform has specific deployment methods and best practices.