Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core locking files

I have a ASP.NET Core app. I run the application by running the command

dotnet run 

I'm seeing the following error in one out of five situations when I build this ASP.NET Core app.

C:...\error CS2012: Cannot open 'C:...\bin\Debug\netcoreapp1.0\AAA.Web.dll' for writing -- 'The process cannot access the file 'C:...\bin\Debug\netcoreapp1.0\AAA.Web.dll' because it is being used by another process.'

In addition to the above issue, I also see no updates that I make in the CSHTML file. I have to stop the dotnet run command, build the app again and then run the dotnet run command.

How can I fix these issues?

enter image description here

like image 664
wonderful world Avatar asked Jan 24 '17 13:01

wonderful world


People also ask

How to lock a file while writing in c#?

Lock a file in C# using File. Lock() method . The lock method let you lock a file so another process cannot access the file. This works even if it has read/write access to the file.


1 Answers

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> 
like image 67
csauve Avatar answered Sep 21 '22 12:09

csauve