DotNetCore监听文件变化

之前一直在找高效监听文件变化,来实现自动编译。最开始想不停扫描监听目录来比对修改时间。但这样性能太低,而且不及时。寻思着文件系统应该有文件变化事件,可能不同系统不统一。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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值