简体   繁体   中英

How to compile code on the fly in asp.net core?

I m create one application in asp.net core using visual studio 2015.(.net core version:1.0.1) when I do any change in cs file then save file and refresh browser my code changes is not reflected in the browser. So how can I get the reflection of code on the fly?

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

There is no such thing, really, as 'compile code on the fly'.

The only thing that microsoft offers is to recompile a whole project/assembly on change of a file. This can be done with dotnet watch run . This is a tool that watches the file-system for changes (done quite poorly so) and then shuts down the dotnet process and re-runs the dotnet start to startup the process again.

I have to say this is very sluggish and impossible to use in big projects.

My advice would be to implement your own watcher. and instead make it build to a different directory each time, so you can stop the current process only after a successful build of the project (and start this new successful build). You could do this with a simple node script or python script.

Simple script looks like this:

  • Watches a directory for changes on *.cs files
  • Watcher contains a callback that runs dotnet build -o /TempDir
  • After this process gave exit code 0 (means it was successful)
    • Kill the old process
    • run dotnet run /Tempdir/your.dll to start the new process

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM