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?
目录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 withSerilogfor log file output - 🔧 Configuration - Configuration framework based on
Microsoft.Extensions.Configuration, extensible with components likedotenv.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 许可协议。转载请注明出处!
相关文章
C#集成ViewFaceCore人脸检测识别库
人脸检测与识别现在已经很成熟了,C# 上有 ViewFaceCore 这个很方便的库,但这种涉及到 native 调用的库,一般会有一些坑,本文记录一下开发和部署的过程。
2023-09-20 · C# · 约 1 分钟
纯 C#实现+AOT 打造的智能PDF目录提取工具 PdfTocExtractor
用C#写个PDF目录提取工具?挺有意思,还整什么语义识别,效果真有那么好吗?AOT编译挺香,但别搞得太复杂就行。
2025-08-20 · C# · 约 1 分钟
C#实现解析Markdown文档并处理图片链接
前言 又是好久没写博客了 其实也不是没写,是最近在「做一个博客」,从2月21日开始,大概一个多星期的时间,疯狂刷进度,边写代码边写了一整系列的博客开发笔记,目前为止已经写了16篇了,然后上3月之后工作有点忙~~加上有点沉迷原神~~,然后进度就暂时慢下来了 事实证明做什么事情一旦停下来就会产生惰性,于是停了这么久,我终于下定决心继续更新! 这次开发的博客用的是C#语言,搭配.netcore技术栈;前
2022-03-19 · C# · 约 1 分钟
在.NetCore(C#)中使用ODP-NET-Core+Dapper操作Oracle数据库
前言 虽然一直在说“去IOE化”,但是在国企和政府,Oracle的历史包袱实在太重了,甚至很多业务逻辑都是写在Oracle的各种存储过程里面实现的…… 我们的系统主要的技术栈是Django / Spring / AspNetCore,Java的不必说对Oracle支持肯定没问题,关键在于Django对Oracle版本有要求,兼容性不是特别好,Oracle版本没办法随意升级的,所以我想到用.Net
2021-12-18 · C# · 约 1 分钟
讨论
共 0 条加载评论中…