Products Technologies Demo Docs Blog Support Company

Creating an ASP.NET Core Web App with Docker Support and GitHub Packages

This article shows how to create an ASP.NET Core Web App with Docker support. The TX Text Control NuGet packages are hosted on GitHub Packages to get restored during the build process.

Creating an ASP.NET Core Web App with Docker Support and GitHub Packages

With the new licensing mechanism introduced in TX Text Control 32.0 SP2, ASP.NET Core projects can be restored and built in Docker. This tutorial shows you how to create an ASP.NET Core Web application with Docker support that uses ServerTextControl. In order to be restored during the Docker build process, the TX Text Control NuGet package is stored in GitHub Packages.

Creating the Application

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 6 SDK.

  1. In Visual Studio 2022, create a new project by choosing Create a new project.

  2. Select ASP.NET Core Web App (Model-View-Controller) as the project template and confirm with Next.

  3. Choose a name for your project and confirm with Next.

  4. In the next dialog, choose .NET 8 (Long-term support) as the Framework, disable Configure for HTTPS for effortless testing, check Enable Docker and confirm with Create. Make sure that Windows is selected as the Docker OS.

    Creating the .NET 6 project

Adding the NuGet Package

  1. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Install the latest versions of the following package:

    • TXTextControl.TextControl.ASP.SDK

    ASP.NET Core Web Application

Using ServerTextControl and MailMerge

  1. Find the HomeController.cs file in the Controllers folder. Replace the Index() method with the following code:

    public IActionResult Index() {
    
      using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
        tx.Create();
    
        // adding static text
        TXTextControl.Selection sel = new TXTextControl.Selection();
        sel.Text = "Welcome to Text Control\r\n";
        sel.Bold = true;
    
        tx.Selection = sel;
    
        // adding merge fields
        TXTextControl.DocumentServer.Fields.MergeField mergeField = 
          new TXTextControl.DocumentServer.Fields.MergeField() {
            Text = "{{company}}",
            Name = "company",
            TextBefore = "Company name: "
          };
    
        tx.ApplicationFields.Add(mergeField.ApplicationField);
    
        // alternatively load a template
        //TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings() {
        //  ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord
        //};
    
        //tx.Load("template.docx", TXTextControl.StreamType.WordprocessingML, ls);
    
        // merge fields with MailMerge engine
        using (TXTextControl.DocumentServer.MailMerge mailMerge = 
          new TXTextControl.DocumentServer.MailMerge()) {
            mailMerge.TextComponent = tx;
            mailMerge.MergeJsonData("[{\"company\": \"Text Control, LLC\" }]");
          }
    
        // return result as HTML
        string result = "";
        tx.Save(out result, TXTextControl.StringStreamType.HTMLFormat);
    
        // alternatively save as PDF
        //byte[] baPdf;
        //tx.Save(out baPdf, TXTextControl.BinaryStreamType.AdobePDF);
    
        ViewBag.Document = result;
      }
    
      return View();
    }

Displaying the Results

  1. Find the Index.cshtml file in the Views -> Home folder. Replace the complete file with the following code:

    @{
        ViewData["Title"] = "Home Page";
    }
    
    @Html.Raw(ViewBag.Document)

Configure NuGet in Project

We need to configure the NuGet package source in the project. This is required to restore the TX Text Control NuGet package during the Docker build process.

NuGet Package Server

Because the TX Text Control NuGet packages contain unique serial number information and are installed locally on your computer, they must be hosted in a NuGet hosting product. This tutorial uses the GitHub package registry, but there are other NuGet hosting products available, such as Azure Artifacts.

  1. Open an elevated (developer) command prompt to add GitHub as a package source for NuGet.

    dotnet nuget add source --username USERNAME --password [TOKEN] --store-password-in-clear-text --name github "https://nuget.pkg.github.com/[NAMESPACE]/index.json"
    • Replace [TOKEN] with your personal access token (classic). Learn here how to create this token.
    • Replace [NAMESPACE] with your GitHub username.

    Download NuGet

    The NuGet.exe CLI is not automatically installed by Visual Studio and can be downloaded directly here:

    https://www.nuget.org/downloads

  2. Use the following command to push the NuGet package to GitHub.

    dotnet nuget push "C:\Program Files (x86)\Text Control GmbH\NuGetPackages\TXTextControl.TextControl.ASP.SDK.32.0.2.nupkg"  --api-key [TOKEN] --source "github"
    • Replace [TOKEN] with your personal access token (classic).
  3. In your Visual Studio project, create a new file in the project's root folder named nuget.config and copy the following configuration into it:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <clear />
    
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
        <add key="github" value="https://nuget.pkg.github.com/[NAMESPACE]/index.json" />
        
      </packageSources>
      <packageSourceCredentials>
        <github>
          <add key="Username" value="USERNAME" />
          <add key="ClearTextPassword" value="[TOKEN]" />
        </github>
      </packageSourceCredentials>
    
    </configuration>
    • Replace [TOKEN] with your personal access token (classic).
    • Replace [NAMESPACE] with your GitHub username.

Project Settings

Some project settings are required.

  1. Select the project in the Solution Explorer and choose Edit Project File from the Project main menu. Make sure that the project settings match the settings in the following example:

    <PropertyGroup>
      <TargetFramework>net8.0-windows</TargetFramework>
      <Nullable>enable</Nullable>
      <ImplicitUsings>enable</ImplicitUsings>
      <DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
      <PlatformTarget>x64</PlatformTarget>
      <RuntimeIdentifier>win-x64</RuntimeIdentifier>
      <Platforms>AnyCPU;x64</Platforms>
    </PropertyGroup>

Editing the Dockerfile

Now, we need to edit the Dockerfile to restore the TX Text Control NuGet package during the build process.

  1. Open the Dockerfile in the project's root folder and replace it with the following content:

    FROM mcr.microsoft.com/dotnet/aspnet:8.0-windowsservercore-ltsc2019 AS base
    WORKDIR /app
    EXPOSE 8080
    
    FROM mcr.microsoft.com/dotnet/sdk:8.0-windowsservercore-ltsc2019 AS build
    ARG BUILD_CONFIGURATION=Release
    WORKDIR /src
    COPY ["NuGetDockerApp/NuGetDockerApp.csproj", "NuGetDockerApp/"]
    
    COPY NuGetDockerApp/nuget.config .
    
    RUN dotnet restore "./NuGetDockerApp/./NuGetDockerApp.csproj"
    COPY . .
    WORKDIR "/src/NuGetDockerApp"
    RUN dotnet build "./NuGetDockerApp.csproj" -c %BUILD_CONFIGURATION% -o /app/build
    
    FROM build AS publish
    ARG BUILD_CONFIGURATION=Release
    RUN dotnet publish "./NuGetDockerApp.csproj" -r win-x64 -c %BUILD_CONFIGURATION% -o /app/publish /p:UseAppHost=false
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    
    # Install Visual C++ Redistributables
    
    SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
    RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
            Invoke-WebRequest "https://aka.ms/vs/17/release/vc_redist.x64.exe" -OutFile "vc_redist.x64.exe"; \
            Start-Process "vc_redist.x64.exe" -ArgumentList "/passive" -wait -Passthru; \
            Remove-Item -Force vc_redist.x64.exe;
    
    ENTRYPOINT ["dotnet", "NuGetDockerApp.dll"]
    • Replace NuGetDockerApp with the name of your project.

Building the Docker Image

Press F5 to build and deploy the application using Docker.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Post

ASP.NETASP.NET CoreDocker

Using a Private NuGet Feed to Build and Restore .NET Apps for Docker Deployment

Learn how to set up a private NuGet feed for your .NET applications and use them in Docker containers. This guide covers the steps to create a private NuGet feed with GitHub, configure your .NET…