如何在asp.net核心中动态编译代码?

时间:2017-04-18 08:55:28

标签: asp.net .net asp.net-core asp.net-core-mvc asp.net-core-1.0

我使用visual studio 2015在asp.net核心创建一个应用程序。(.net核心版本:1.0.1)当我对cs文件进行任何更改然后保存文件并刷新浏览器我的代码更改未反映在浏览器。那么如何动态地反映代码呢?

project.json

{
  "compilationOptions": {
  "emitEntryPoint": true
},
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    //"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview2-final",
    //"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final",
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": [
        "portable-net45+win8"
      ]
    },

    //Database Provider for EF Core
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
    //EF Core Package Manager Console Tools
    //"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    //EF Core Funtionality for MSSQL Server
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1",
    "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.DotNet.Watcher.Tools": {
      // "version": "1.0.0-preview2-final",
        "version": "1.0.0-*",
      "imports": "portable-net451+win8"
    },

    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": [
        "portable-net45+win8"
      ]
    }


  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "debugType": "portable"
  },
  "runtimes": {
    "win10-x64": {},
    "osx.10.10-x64": {},
    "ubuntu.14.04-x64": {}
  },
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

1 个答案:

答案 0 :(得分:3)

实际上没有“动态编译代码”这样的东西。

微软提供的唯一功能就是在更改文件时重新编译整个项目/程序集。这可以使用dotnet watch run完成。这是一个监视文件系统进行更改的工具(完成得非常糟糕),然后关闭dotnet进程并重新运行dotnet start以再次启动该进程。

我不得不说这是非常迟钝的,不可能在大型项目中使用。

我的建议是实施自己的观察者。而是每次都使它构建到不同的目录,因此只有在成功构建项目之后才能停止当前进程(并启动这个新的成功构建)。您可以使用简单的节点脚本或python脚本执行此操作。

简单脚本如下所示:

  • 在* .cs文件
  • 上查看目录以进行更改
  • Watcher包含一个运行dotnet build -o /TempDir
  • 的回调
  • 此过程后退出代码为0(表示成功)
    • 杀死旧流程
    • 运行dotnet run /Tempdir/your.dll以启动新流程