之前一直在找高效监听文件变化,来实现自动编译。最开始想不停扫描监听目录来比对修改时间。但这样性能太低,而且不及时。寻思着文件系统应该有文件变化事件,可能不同系统不统一。DotNetCore
提供了FileSystemWatcher
来监控文件变化,看实现代码就是和系统做对接的,不会有不停扫描导致性能低下问题。
类似下面这样监控文件变化实现自动编译
using System;
using System.IO;
namespace FileSystemWatcherTest
{
class Program
{
static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
//监听controllers文件夹的改变
watcher.Path = AppContext.BaseDirectory + "Test";
//所有子目录的改变
watcher.IncludeSubdirectories = true;
//设置文件的文件名、目录名及文件的大小改动会触发Changed事件
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
//监听.cs文件的改变
watcher.Filter = "*.cs";
// 文件创建和改变事件,触发自动编译
watcher.Changed += Watcher_Changed;
watcher.Created += Watcher_Created;
watcher.EnableRaisingEvents = true;
Console.ReadLine();
}
private static void Watcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine(e.FullPath);
}
private static void Watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine(e.FullPath);
}
}
}
ASP.NET Core 发布之后通过命令控制监听地址和环境变量
添加Command
支持
新建一个ASP.NET Core 项目,打开Program.cs
添加下面的代码:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build())
.UseStartup<Startup>()
.Build();
}
主要是这句代码:UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build())
发布项目
通过命令 dotnet publish -c Release
发布项目
指定监听地址和环境变量
我们先启动运行一下dotnet WebApplication1.dll
我们可以看到默认的监听地址为 http://localhost:5000
,默认的环境变量为Production
我们可以通过--server.urls 监听地址
来制定监听地址,可以通过--environment 环境变量
来指定环境变量
比如:dotnet WebApplication1.dll --server.urls http://*:8080 --environment Staging
注意
到 ASP.NET Core 2.2 默认就已经添加了这个包,通过 WebHost.CreateDefaultBuilder
创建的就默认包含
dotnet website.dll
指定运行参数
默认的监听地址为 http://localhost:5000
,默认的环境变量为Production
可以通过-server.urls
监听地址来制定监听地址,可以通过--environment
环境变量来指定环境变量
dotnet website.dll --server.urls http://*:88 --environment ZYQ
运行结果
Hosting environment: ZYQ
Content root path: D:\Source\Repos\website\out
Now listening on: http://[::]:88
Application started. Press Ctrl+C to shut down.
NETCORE 设置监听URL
记录一下已经实践过的4种监听url
的方法:
一、 直接写死url
地址在代码(不推荐使用这种)
webBuilder.UseUrls("http://192.168.1.1:7001;https://192.168.1.1:7002");
//或下面这种监听本地所有的IP的端口
//webBuilder.UseUrls("http://*:7001");
二、使用dotnet
命令直接将地址通过main方法的args参数传入
dotnet xxxx.dll --urls "http://127.0.0.1:7001;https://127.0.0.1:7002"
三、使用配置文件
新建一个hosting.json
文件,添加如下内容
{
"urls": "http://localhost:7001;http://localhost:7002"
}
使用ConfigureWebHostDefaults
加载配置文件,并使用配置文件中的urls
属性的value
作为监听地址
//加载配置文件
IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("hosting.json", optional: true).Build();
//设置监听配置文件
webBuilder.UseConfiguration(config);
//也可以,两者等价
//webBuilder.UseUrls(config["urls"]);
为什么说两者等价呢?
因为UseConfiguration()
做的事情和UseUrls()
的事情都是一样的。
四、使用环境变量
新建变量名:ASPNETCORE_URLS
,变量值:http://127.0.0.1:7001;https://127.0.0.1:7002
的环境变量
直接启动已经编译好的exe
文件或者使用dotnet xxx.dll
文件,就能监听到环境变量设置的URL
dotnet
命令行程序指定监听和端口启动
服务器上需要一个远程执行cmd
命令的工具,所以找到了以下的这个工具WindowsRemoteCommandRunner
,它可以同web
页面或者http
请求,执行你所输入的命令。
项目地址:https://github.com/kejsardamberg/WindowsRemoteCommandRunner
但是这个应用默认只能通过localhost:5000
来访问,像192.168.1.100
这类的局域网IP就不能访问,所以就需要修改应用的监听地址。
启动的时候通过,以下命令来访问。
dotnet WindowsRemoteCommandRunner.dll urls http://*:5000