我想使用 ASP.NET Core 2.1 创建一个 Web 服务,用于检查应用程序启动
因此,例如,我有一个场景来检查文件夹结构,如果在应用程序启动时不创建文件夹结构。
创建文件夹结构的方法在 FileService.cs 中,应用程序在任何 http 请求之前启动时,必须通过 DI 启动该方法。
appsettings.json 包含键和值,其中包含创建文件夹结构的结构。
"FolderStructure": {
"Download": {
"English": {
"*": "*"
},
"Hindi": {
"*": "*"
}
},
"Upload": {
"*": "*"
}
}
并在接口和服务下面使用
界面
namespace MyApp.Services
{
public interface IFileService
{
void CreateDirectoryStructure(string path = "");
void CreateFolder(string name, string path = "");
void CreateFile(string name, string path = "");
bool CheckFileExists(string path);
bool CheckFolderExists(string path);
}
}
服务
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Configuration.Binder;
using System.IO;
using Microsoft.Extensions.Logging;
namespace MyApp.Services
{
public class FileService : IFileService
{
private readonly IFileProvider _fileProvider;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IConfiguration _config;
private readonly ILogger<FileService> _logger;
string defaultfolderPath = ConfigurationManager.AppSetting["DefaultDrivePath"];
public FileService(IFileProvider fileProvider, IHostingEnvironment hostingEnvironment, IConfiguration config, ILogger<FileService> logger)
{
_fileProvider = fileProvider;
_hostingEnvironment = hostingEnvironment;
_config = config;
_logger = logger;
}
public void CreateDirectoryStructure(string drivePath = "")
{
if (drivePath.Equals(""))
{
drivePath = ConfigurationManager.AppSetting["DefaultDrivePath"];
_logger.LogInformation($"Default folder path will be picked {drivePath}");
}
foreach (var item in _config.GetSection("FolderStructure").GetChildren())
{
CreateFolder(item.Key, drivePath);
foreach (var i in _config.GetSection(item.Path).GetChildren())
{
if (i.Key != "*")
CreateFolder(i.Key, $"{drivePath}/{item.Key}");
}
}
}
public void CreateFolder(string name, string path = "")
{
string fullPath = string.IsNullOrEmpty(path) ? $"{defaultfolderPath}/{name}" : $"{path}/{name}";
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
_logger.LogInformation($"Directory created at {fullPath} on {DateTime.Now}");
}
}
public void CreateFile(string name, string path = "")
{
string fullPath = string.IsNullOrEmpty(path) ? $"{defaultfolderPath}/{name}" : $"{path}/{name}";
if (!File.Exists(fullPath))
{
File.Create(fullPath);
_logger.LogInformation($"File created at {fullPath} on {DateTime.Now}");
}
}
public bool CheckFolderExists(string path)
{
string fullPath = string.IsNullOrEmpty(path) ? defaultfolderPath : path;
return Directory.Exists(fullPath);
}
public bool CheckFileExists(string path)
{
string fullPath = string.IsNullOrEmpty(path) ? defaultfolderPath : path;
return File.Exists(fullPath);
}
}
}
现在的挑战是在应用程序启动后立即调用文件夹服务方法,但我们需要通过 DI 初始化文件服务
services.AddSingleton<IFileService, FileService>();
并且在配置方法中你可以调用所需的服务。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IFileService FileService)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
//dont change the below order as middleware exception need to be registered before UseMvc method register
app.ConfigureCustomMiddleware();
// app.UseHttpsRedirection();
app.UseMvc();
FileService.CreateDirectoryStructure();
}