Wednesday, February 9, 2022

Docker: Publishing a .NET Core (.NET 6) Console Application with Visual Studio 2022 (a Containerization Prereq)

Before a Docker image can be created that hosts an application (such as a console app created with Visual Studio 2022), the artifacts (files) needed to run the application must be created. The process for creating the artifcatds needed to run an application is called publishing. Simply put, an application must be published before it can be copied to a Docker image and the application subsequently containerized.

The console application to be published was created in the post Docker: Create a .NET 6 Console Application in Visual Studio 2022 for future Containerization. The newly created project and the solution that contains the project are as follows (opened in Visual Studio 2022):


Configure Publishing

To configure publishing the project, right-click on the project in Solution Explorer and select Publish from the context menu:


The first time the Publish menu item is selected, a wizard is displayed that allows the configuration associated with publishing to be set up. Subsequently clicking on publish will build the project and copy the files that are needed to run the console application to a target folder.

When the Publish menu item is selected for the first time for a given project, the Publish dialog is displayed as follows:


From the Publish menu, select target, Folder, and click Next which still displays the Publish dialog but allows the "Specific target" to be selected (see below): 


For the "Specific target," select Folder and click Next which expands the Publish dialog to include Location as follows:


The "Folder location" is filled in by default with bin\Release\net6.0\publish\. This means that the artifacts required to run the application will be placed in this folder.

Click Finish to complete the configuration of the publishing for the console application. The 

Publishing

To publish the project (after publishing has been configured), right-click on the project in Solution Explorer and select Publish from the context menu which displays the Publish dialog. It is also possible to initiate publishing using Visual Studio 2022's menus and selectin gthe  Build | Publish Selection menu item:


After clikcing on the "Publish Select" menu item, the Publish dialog is displayed as follows:


To the right side of the dialog is a Publish button. Clicking on the Publish button will publish the application by creating the set of files needed to run the application and placing them under the target location (bin\Release\net6.0\Publish).

After a project is published, the Publish dialog is update and will show and entry for "Publish succeeded" or "Publish failed" (see below):


The Publish button was clicked and it is important to note that Visual Studio's current Solution Configuration was set to Debug:


When the Publish button was clicked, a build was run which built Release as the publishing process is associated with the Release Soluiton Configuration. After the build succeeded, the file required to run the application were copied to the publish folder.

The built output shows that publishing invoked a Release build even though the Solution Configuration was set to Debug:


Publishing Configuration File

The file used to store publishing configuration information is FolderProfile.pubxml. This file is found in the project's Properties\PublishProfiles folder: 



The contents of FolderProfile.pubxml reflect the configuraton specified when publishign was setup:


Publish Folder

As discussed in "Visual Studio: Displaying all Files in Solution Explorer," Visual Studio 2022 does not show bin/obj files by default. The published files are under bin folder so to see the files from Solution Explorer toggle on the "Show all Files" button and navigage in Solution Explorere to the publish folder (bin\Release\net6.0\publish):









No comments :

Post a Comment