.NET Core locking files

Solution 1:

This may also help when running your aspnetcore app in IIS.

Add the following to your csproj:

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="echo &quot;App Offline&quot; /a &gt; &quot;$(ProjectDir)app_offline.htm&quot;" />
  </Target>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="del &quot;$(ProjectDir)app_offline.htm&quot;" />
  </Target>

Solution 2:

If it is on local and application is running on IIS, then in windows task bar before your date time, there should be a IIS Express Option. select IIS Express, select exit. Try running application again.

exit IIS Express

Solution 3:

In my case, I got the same issue due to dotnet watch run command even after stopping the watch mode.
Visual studio reported this error message : The file is locked by: ".NET core Host (pid)"
killing the process with the pid fixed the issue.