【问题标题】:`gulp.watch` blocking execution of `dotnet run``gulp.watch` 阻止执行 `dotnet run`
【发布时间】:2018-12-24 02:37:52
【问题描述】:

我正在执行 gulp.watch 作为 .NET Core 项目中预编译脚本的一部分。在执行dotnet run 时,gulp.watch 调用似乎阻塞了线程,因此应用程序根本无法启动。

如何告诉gulp.watch 将句柄返回给线程?

我创建了一个最小的工作示例来重现问题,使用dotnet new 并通过npm 安装gulp

这是我的gulpfile.js

var gulp = require('gulp');

gulp.task('copy', () => {
    gulp.src("watch_src/foo.txt")
      .pipe(gulp.dest("watch_dst/"));
});

gulp.task('watch', () => {
    var watcher = gulp.watch("watch_src/foo.txt", ['copy']);
    watcher.on('change', function(){
        console.log('foo.txt changed!');
    });
});

gulp.task('default', ['watch']);

我的Program.cs 文件如下所示:

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

我的project.json 文件如下所示:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
    }
  },
  "scripts": {
    "precompile": "gulp"
  }
}

执行dotnet run 将执行预编译脚本,并且对foo.txt 的更改由控制台上的事件处理程序反映。但是应该打印Hello World!Main 方法没有被执行:

c:\Temp\src\gulptest>dotnet run
Compiling gulptest for .NETCoreApp,Version=v1.0
[17:03:48] Using gulpfile c:\Temp\src\gulptest\gulpfile.js
[17:03:48] Starting 'watch'...
[17:03:48] Finished 'watch' after 11 ms
[17:03:48] Starting 'default'...
[17:03:48] Finished 'default' after 43 ╬╝s
foo.txt changed!
[17:04:15] Starting 'copy'...
[17:04:15] Finished 'copy' after 34 ms

【问题讨论】:

  • 我也尝试过使用gulp-watch 插件,但它的行为相同。
  • 你终于找到答案了吗?我很想知道。谢谢
  • 没有。不幸的是,似乎没有其他人有这个问题,或者他们以另一种方式解决了这个问题。我也尝试联系 gulp 社区here,但没有成功。
  • 该死的,我希望你能给我答案。我真的很想要这个。我刚刚发布了tweet。如果你有账号,请RT。希望社区能看到它......

标签: c# npm gulp .net-core


【解决方案1】:

我正在使用 vs 代码,我也遇到过你的问题

project.json 中,您有以下部分:

"scripts": {
    "precompile": "gulp"
  }

这会卡住dotnet run

如果您想启动 dotnet rungulp watch

,请按照以下步骤操作

第一步:在task.json文件中定义一个新任务:

 {
            "type": "shell",
            "command": "gulp watch",
            "problemMatcher": [],
            "label": "gulp-watch"
 }

Step2:在launch.json中添加新的启动配置:

  {

            "name": "gulp-watch",
            "preLaunchTask": "gulp-watch",
            "request": "launch",
            "type": "node"
  },

Step3:在launch.json中添加复合配置:

 "compounds": [{
        "name": ".Net+gulp",
        "configurations": [
            "gulp-watch",
            ".NET Core Launch (web)"
        ]
    }],

更多详情请参考此链接https://code.visualstudio.com/docs/editor/debugging

【讨论】:

    【解决方案2】:

    ASP.NET core 中的 main 方法应该是这样的 -

                var host = new WebHostBuilder()
                    .UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseIISIntegration()
                    .UseStartup<Startup>()
                    .Build();
    
                host.Run();
    

    您在上面显示的是纯控制台应用程序,不需要 gulp。

    【讨论】:

      猜你喜欢
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2022-01-23
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多