gpt4 book ai didi

asp.net - 如何在 ASP.NET Core 中即时编译代码?

转载 作者:行者123 更新时间:2023-12-02 10:48:15 26 4
gpt4 key购买 nike

我使用 Visual Studio 2015 在 asp.net core 中创建一个应用程序。(.net core 版本:1.0.1)当我在 cs 文件中进行任何更改,然后保存文件并刷新浏览器时,我的代码更改不会反射(reflect)在浏览器。那么如何动态获取代码的反射呢?

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%" ]
}
}

最佳答案

实际上,不存在“动态编译代码”这样的事情。

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

我不得不说这非常缓慢并且不可能在大型项目中使用。

我的建议是实现你自己的观察者。而是让它每次构建到不同的目录,这样您只能在成功构建项目后停止当前进程(并开始新的成功构建)。您可以使用简单的节点脚本或 python 脚本来完成此操作。

简单的脚本如下所示:

  • 监视目录中 *.cs 文件的更改
  • Watcher 包含运行 dotnet build -o/TempDir 的回调
  • 此过程给出退出代码 0 后(表示成功)
    • 终止旧进程
    • 运行 dotnet run/Tempdir/your.dll 启动新进程

关于asp.net - 如何在 ASP.NET Core 中即时编译代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43467766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com