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 Server ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ServerTextControl Class
The ServerTextControl class implements a component that provide high-level text processing features for server-based applications. . 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.
-
In Visual Studio 2022, create a new project by choosing Create a new project.
-
Select ASP.NET Core Web App (Model-View-Controller) as the project template and confirm with Next.
-
Choose a name for your project and confirm with Next.
-
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.
Adding the NuGet Package
-
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
Using ServerTextControl and MailMerge
-
Find the HomeController.cs file in the Controllers folder. Replace the Index() method with the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characterspublic 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
-
Find the Index.cshtml file in the Views -> Home folder. Replace the complete file with the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters@{ 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.
-
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:
-
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).
-
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters<?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.
-
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters<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.
-
Open the Dockerfile in the project's root folder and replace it with the following content:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersFROM 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.