曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
C#

Developing Modern .NetCore Console Applications: (2) Creating a C# Project Template

Another .NET Core template? FluentConsole.Templates, one-click skeleton generation including DI, logging, and configuration. Installation? dotnet new install, then dotnet new flu-cli -n MyProject. Too simple, right? Who cares?

曦远 (DealiAxy)约 18 分钟
目录9 个章节

Preface

The previous article (Developing a Modern .NET Core Console Application with Dependency Injection, Configuration, Logging, and More) detailed the process of creating a modern .NET Core console application. However, this wasn't enough—I went ahead and created a scaffold template, named FluentConsole.Templates, to conveniently build "modern console applications" (现代化控制台应用).

Source Code: https://github.com/Deali-Axy/fluent-dotnet-console

NuGet Package: https://www.nuget.org/packages/FluentConsole.Templates/

Template Features

  • 🚀 Provides rapid development templates, one-click generation of project scaffolding for console applications
  • 🐴 Offers a reference solution for "best practices of modern console application project structure"
  • 💉 Dependency Injection - Dependency injection support based on Microsoft.Extensions.DependencyInjection
  • 📄 Logging - Logging framework based on Microsoft.Extensions.Logging, integrated with Serilog for log file output
  • 🔧 Configuration - Configuration framework based on Microsoft.Extensions.Configuration, extensible with components like dotenv.net

Quick Start

Install the template

dotnet new install FluentConsole.Templates

Create a project using the template

dotnet new flu-cli -n MyProject

Project Directory Structure

The project directory structure created using the template is as follows, with all code placed uniformly under the src directory.

MyProject
 ├─ src
 │  ├─ Utilities
 │  │  └─ ConsoleTool.cs
 │  ├─ Services
 │  │  ├─ MainService.cs
 │  │  └─ IService.cs
 │  ├─ MISC
 │  │  └─ SourceGenerationContext.cs
 │  ├─ Entities
 │  │  ├─ OutputResult.cs
 │  │  └─ AppSettings.cs
 │  └─ Program.cs
 ├─ MyProject.csproj
 ├─ Dockerfile
 └─ appsettings.json

Here is an introduction to each directory inside src:

  • Utilities - Stores general utility classes.
  • Services - Business logic code.
  • MISC - Miscellaneous.
  • Entities - Entity classes, strongly-typed configurations, and output result objects.

Therefore, after creating the project, you can directly write the business logic in the Services/MainService.cs file.

How to Create a Project Template?

Follow the official Microsoft tutorial—it's quite straightforward. The main details involve handling the directory structure.

Using the source code of this project as an example, we need a working directory for creating templates. In this project, that directory is src/Templates

Templates
 ├─ content
 │  └─ FluentConsole.Template
 │     ├─ FluentConsole.Template
 │     │  ├─ src
 │     │  ├─ FluentConsole.Template.csproj
 │     │  ├─ Dockerfile
 │     │  ├─ appsettings.json
 │     │  └─ .env
 │     └─ .template.config
 │        └─ template.json
 └─ FluentConsole.Templates.csproj

Then place all templates inside the content directory. You can think of each directory under content as a template. This project currently has only one template, but more may be added in the future.

The most important part under each template is .template.config. With this directory, dotnet will recognize it as a template.

Of course, you also need to write a template configuration template.json

{
  "$schema": "http://json.schemastore.org/template",
  "author": "DealiAxy",
  "classifications": [
    "Common",
    "Console"
  ],
  "identity": "FluentConsole.Template",
  "name": "FluentConsole.Template",
  "description": "Provides a "modern console application development experience" script scaffold (脚手架), enabling elegant integration of various components like dependency injection, configuration, and logging, similar to web applications.",
  "shortName": "flu-cli",
  "sourceName": "FluentConsole.Template",
  "tags": {
    "language": "C#",
    "type": "project"
  }
}

Pay attention to the sourceName field—this is the content that will be replaced when the template is used to create a project.

For example, if we use the following command to create a project from the template, specifying the project name as MyProject with -n

dotnet new flu-cli -n MyProject

Then dotnet will automatically replace the name set in sourceName with the user-specified project name MyProject.

OK, that's it for now. Creating a template is that simple.

Installing Templates

You can install templates directly from the source code

Using the same project code, navigate to the directory where the template code is located, and you can then install the template.

cd src/Templates/content/FluentConsole.Template
dotnet new install ./

After installation, you will see a success message indicating the installation was completed.

Success: FluentConsole.Templates::1.1.0 installed the following templates:
----------------------  -------  ----  --------------
FluentConsole.Template  flu-cli  [C#]  Common/Console

Creating a NuGet Package

Create the FluentConsole.Templates.csproj file under src/Templates

The key code is as follows

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <PackageId>FluentConsole.Templates</PackageId>
    <PackageVersion>1.1</PackageVersion>
    <Title>FluentDotnetConsole Templates</Title>
    <Authors>DealiAxy</Authors>
    <Company>CodeLab</Company>
    <Description>提供「现代化的控制台应用的开发体验」脚手架,能像 Web 应用那样很优雅地整合各种组件,包括依赖注入、配置、日志等功能。</Description>
    <PackageTags>dotnet-new;templates;console;cli</PackageTags>
    <PackageProjectUrl>https://github.com/Deali-Axy/fluent-dotnet-console</PackageProjectUrl>

    <PackageType>Template</PackageType>
    <TargetFramework>netstandard2.0</TargetFramework>
    <IncludeContentInPack>true</IncludeContentInPack>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <ContentTargetFolders>content</ContentTargetFolders>
    <NoWarn>$(NoWarn);NU5128</NoWarn>
    <NoDefaultExcludes>true</NoDefaultExcludes>
    <PackageReadmeFile>README.md</PackageReadmeFile>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="content\**\*" Exclude="content\**\bin\**;content\**\obj\**"/>
    <Compile Remove="**\*"/>
  </ItemGroup>
</Project>

Then run dotnet pack to create the NuGet package.

After that, you can upload it to the NuGet platform.

Publishing NuGet Packages via GitHub

GitHub offers a Packages management feature that allows you to create NuGet packages directly from a GitHub project and publish them to the NuGet platform.

Note: I'll look into this and continue with more details in the next article.

References

_EOF_

Developing Modern .NetCore Console Applications: (2) Creating a C# Project Template

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

SharpIco:用纯C#打造零依赖的.ico图标生成器,支持.NET9与AOT编译

SharpIco 是一款纯 C# 实现的跨平台图标生成工具,支持 AOT 编译,无需外部依赖。它能将 PNG 图片分解为多尺寸(16x16至512x512)合成 ICO 图标,并支持图标结构分析。工具提供命令行界面,可通过 dotnet tool 一键安装,并已发布至 NuGet 和 Scoop。项目旨在简化图标生成与验证,追求轻量、纯粹、易集成的使用体验。

2025-05-26 · C# · 约 1 分钟

并行化优化KD树算法:使用C#实现高效的最近邻搜索

本文介绍了如何使用并行计算技术优化 KD 树算法,并使用 C# 编程语言实现了高效的最近邻搜索。首先,我们简要介绍了 KD 树的原理和构建过程,然后详细讨论了如何利用并行计算库在多个 CPU 核心上并行构建 KD 树,从而加速搜索过程。通过实验验证,我们证明了并行化优化能够显著提高 KD 树的构建速度和搜索效率,为大规模数据集下的最近邻搜索问题提供了一种高效的解决方案。

2024-03-10 · C# · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…