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 分钟
开发现代化的.NetCore控制台程序:(1)项目介绍以及依赖注入/配置/日志等使用细节
开发.NET Core控制台工具,依赖注入和日志整合得比Go优雅多了。哼,配置文件丝滑,但AOT?反射问题,放弃。
2023-11-10 · C# · 约 1 分钟
并行化优化KD树算法:使用C#实现高效的最近邻搜索
本文介绍了如何使用并行计算技术优化 KD 树算法,并使用 C# 编程语言实现了高效的最近邻搜索。首先,我们简要介绍了 KD 树的原理和构建过程,然后详细讨论了如何利用并行计算库在多个 CPU 核心上并行构建 KD 树,从而加速搜索过程。通过实验验证,我们证明了并行化优化能够显著提高 KD 树的构建速度和搜索效率,为大规模数据集下的最近邻搜索问题提供了一种高效的解决方案。
2024-03-10 · C# · 约 1 分钟
试一下用DotNetCore来做机器学习,ML-Net框架跑起来
ML.Net简介 官网:https://dotnet.microsoft.com/apps/machinelearning-ai/ml-dotnet 官方介绍:The ML.NET command line interface (CLI), provides tools for building machine learning models with ML.NET. ML.NET 是面向.NET
2021-12-18 · C# · 约 1 分钟
讨论
共 0 条加载评论中…