Watching for File Changes in ASP .NET Core

By Shahed C on November 26, 2018

This is the eighth of a new series of posts on ASP .NET Core. In this post, we’ll learn how to use the .NET CLI file watcher tool during development and debugging.

ASPNETCoreLogo-300x267

Before You Begin

Before you begin, take a look at the sample code project on GitHub:

Web AspNetCoreWatcher on GitHub: https://github.com/shahedc/AspNetCoreWatcher

Specifically, look at the history of CalcController to see how the Sum() method has been updated to accept doubles and not just ints, when summing up two numbers.

Debugging while Running

Initially, we have a Sum() method that looks like this:

 [Route("[action]")]
 public ActionResult<string> Sum(
    [FromQuery(Name = "num1")] int num1,
    [FromQuery(Name = "num2")] int num2)
 {
    var sum = (num1 + num2).ToString();
    return $"The sum of numbers {num1} and {num2} is {sum}.";
 }

We can run the web app from the command line using dotnet run:

> dotnet run

This allows us to access the Web API via a web browser to add 2 numbers, e.g. 3 and 4 for num1 and num2.

https://localhost:5001/api/calc/Sum?num1=3&num2=4

SumInts

However, we get an error if we try to pass in numbers with decimal points, e.g. 3 and 4.4 for num1 and num2.

https://localhost:5001/api/calc/Sum?num1=3&num2=4.4

SumDoublesError

To fix the error, we can change the input parameters to expect doubles instead of ints.

 [Route("[action]")]
 public ActionResult<string> Sum(
 [FromQuery(Name = "num1")] double num1,
 [FromQuery(Name = "num2")] double num2)
 {
 var sum = (num1 + num2).ToString();
 return $"The sum of numbers {num1} and {num2} is {sum}.";
 }

If you try to refresh the browser, you should still get the same old error message. You would have to restart the app just to see the new changes. Don’t fretthere’s an easy way to get around this!

Watching for File Changes

Revert your changes, and run the application again. But this time, use the ASP .NET Core file watcher using the following CLI command.

dotnet watch run

This should kick off the app, while watching for file changes.

Try to access the same URL to ensure that you’re still seeing the same error message.

https://localhost:5001/api/calc/Sum?num1=3&num2=4.4

Then, edit your code to use doubles instead of ints and save your file. Let the app restart itself and access the URL in your browser again. Your app should now accept any double values as proper input, indicating that your changes have taken effect.

SumDoubleSuccess

Testing, Customization and Opting Out

What else can you do with the watcher?

  • Automated Testing: Run dotnet watch test to rerun tests when file changes are detected.
  • Customization: Update your .csproj project file to include or exclude files using wildcard patterns, separated by semicolons. You can also opt of out specific files, by mentioning them in your .csproj file.

As explained in the official docs, here is an example of the syntax:

  • Watch element with Include or Exclude attributes
  • Other elements with Watch attribute set to false (defaults to true when not set)
<ItemGroup>
 <!-- extends watching group to include *.ext1 files, exclude subfolders/files -->
 <Watch Include="**\*.ext1" Exclude="subfolder1\**\*;**\*.ext1.ext2;obj\**\*;subfolder2\**\*" />

 <!-- exclude class file Class1.cs from dotnet-watch -->
 <Compile Include="Class1.cs" Watch="false" />
 <!-- exclude embedded resource ResourceFile.resx from dotnet-watch -->
 <EmbeddedResource Include="ResourceFile.resx" Watch="false" />
 <!-- exclude changes in this referenced project -->
 <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" Watch="false" />
</ItemGroup>

References

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.