RickStrahl / Westwind.AspnetCore.LiveReload

ASP.NET Core Live Reload Middleware that monitors file changes in your project and automatically reloads the browser's active page
Other
469 stars 42 forks source link

Web Project as a Class Library #14

Closed rd-nrg closed 4 years ago

rd-nrg commented 4 years ago

This perfectly works for a standalone web application but not if web application startup is referenced in a console application as a class library.

  1. Add a console application to a solution
  2. Add a web application as a class library to the same solution and reference it in the console application above by adding:
    public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
  3. Run "dotnet watch run", load http://localhost:5000, and change a cshtml file. The change is detected by LiveReload but it throws exceptions when reloading the active page.
RickStrahl commented 4 years ago

If you want standalone I recommend using the Dotnet Tool (3.0 SDK).

dotnet tool install LiveReloadServer

That'll be a self contained executable you can run from anywhere.

As a console app you'll have a dependency on the ASP.NET Core runtimes - so if you're building a console app you'll need to add the <framework include="AspNet.Core.All" /> reference.

Take a look at the LiveReloadServer project in this repository - it can run either as a console app or as a dotnet tool - depending on the packaging options.

rd-nrg commented 4 years ago

Thanks for your prompt reply. The LiveReloadServer is not an option as the cshtml files depend on external class libraries and data within the same solution. Would you please tell me how to add <framework include="AspNet.Core.All" /> to a .NET Core 3.0 console application?

RickStrahl commented 4 years ago

You need to add <framework include="AspNet.Core.App" /> not .All in 3.0.

I can attest that that this works. I've build a several tools that rely on the LiveReload middleware and it works great in that integrated scenario. To see how this works look at the dotnet tool implementation and it also compiles to a standalone server.

<Project Sdk="Microsoft.NET.Sdk">

  ...
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Westwind.AspNetCore.LiveReload" Version="0.1.5.4" />   
  </ItemGroup>

</Project>