title: dotnet core 3.x PublishSingleFile: Temp files get deleted
body: <p>We are providing an application as a self-contained dotnet core 3.1 single executable file. The exe is running as a service on various Windows Servers:</p>
  
  <ul>
  <li>Server 2012 64 bit</li>
  <li>Server 2016 64 bit</li>
  <li>Server 2019 64 bit</li>
  </ul>
  
  <p>After some time of running (a week and above), the service fails to restart because of the missing <em>AppName.deps.json</em> file inside the temporary directory:</p>
  
  <pre><code>System.IO.FileNotFoundException: Could not find file 'C:\Windows\TEMP\.net\AppName\4rpjodow.e1g\AppName.deps.json'.
  File name: 'C:\Windows\TEMP\.net\AppName\4rpjodow.e1g\AppName.deps.json'
     at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
     at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
     at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
     at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
     at System.IO.File.OpenRead(String path)
     at Microsoft.Extensions.DependencyModel.FileWrapper.OpenRead(String path)
     at Microsoft.Extensions.DependencyModel.DependencyContextLoader.LoadContext(IDependencyContextReader reader, String location)
     at Microsoft.Extensions.DependencyModel.DependencyContextLoader.Load(Assembly assembly)
     at Microsoft.Extensions.DependencyModel.DependencyContext.Load(Assembly assembly)
     at Microsoft.Extensions.DependencyModel.DependencyContext.LoadDefault()
     at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
     at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
     at System.Lazy`1.CreateValue()
     at System.Lazy`1.get_Value()
     at Microsoft.Extensions.DependencyModel.DependencyContext.get_Default()
     at Serilog.Settings.Configuration.Assemblies.AssemblyFinder.Auto()
     at Serilog.ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration settingConfiguration, IConfiguration configuration, String sectionName, DependencyContext dependencyContext)
     at Serilog.ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration settingConfiguration, IConfiguration configuration, DependencyContext dependencyContext)
     at AppName.Program.&lt;&gt;c__DisplayClass2_0.&lt;CreateWebHostBuilder&gt;b__2(WebHostBuilderContext hostingContext, ILoggingBuilder logging)
     at Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.&lt;&gt;c__DisplayClass9_1.&lt;ConfigureLogging&gt;b__1(ILoggingBuilder builder)
     at Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection services, Action`1 configure)
     at Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.&lt;&gt;c__DisplayClass9_0.&lt;ConfigureLogging&gt;b__0(WebHostBuilderContext context, IServiceCollection collection)
     at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException&amp; hostingStartupErrors)
     at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
     at AppName.Program.Main(String[] args)
  </code></pre>
  
  <p>By checking the directory we could see that there are missing some more .json and also .dll files. I guess that the file system cleanup removes all files that are not in use while the service is running.</p>
  
  <p>Deleting the whole temp directory resolves the problem because the exe will be extracted again on startup. The problem seems to be the fact, that only some files get deleted and the content is not beeing extracted again.</p>
  
  <p>Our preferred solution would be to extract the files to a directory outside the temp dir. Until now we did not found an option that would provide this feature. Also any other ideas to avoid deleting the temp files are welcome. We would like to keep on working with a single executable if possible because this makes shipping the application to our customers much more comfortable.</p>
  
  <p><strong>EDIT</strong>: This is the current PropertyGroup for publish from .csproj:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;PropertyGroup&gt;
      &lt;OutputType&gt;Exe&lt;/OutputType&gt;
      &lt;TargetFramework&gt;netcoreapp3.1&lt;/TargetFramework&gt;
      &lt;RuntimeIdentifier&gt;win-x64&lt;/RuntimeIdentifier&gt;
      &lt;SelfContained&gt;true&lt;/SelfContained&gt;
      &lt;Version&gt;1.0-develop&lt;/Version&gt;
      &lt;ApplicationVersion&gt;1.0-develop&lt;/ApplicationVersion&gt;
      &lt;Description&gt;App-Description&lt;/Description&gt;
      &lt;Copyright&gt;Copyright © 2019 Company&lt;/Copyright&gt;
      &lt;LangVersion&gt;8&lt;/LangVersion&gt;
      &lt;PublishTrimmed&gt;true&lt;/PublishTrimmed&gt;
      &lt;PublishSingleFile&gt;true&lt;/PublishSingleFile&gt;
      &lt;ApplicationIcon&gt;Icon.ico&lt;/ApplicationIcon&gt;
      &lt;AssemblyName&gt;AppName&lt;/AssemblyName&gt;
      &lt;RootNamespace&gt;AppName&lt;/RootNamespace&gt;
      &lt;PreserveCompilationContext&gt;false&lt;/PreserveCompilationContext&gt;
      &lt;DebugSymbols&gt;false&lt;/DebugSymbols&gt;
  &lt;/PropertyGroup&gt;
  </code></pre>