Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Extensions.DependencyModel with Net5.0-Preview7 PublishSingleFile #41265

Closed
scamille opened this issue Aug 24, 2020 · 7 comments
Closed

Comments

@scamille
Copy link

scamille commented Aug 24, 2020

Description

Accessing Microsoft.Extensions.DependencyModel.DependencyContext.Default in a application targetting Net5.0 and published as a SingleFile causes an exception, because the applications .deps.json file can not be found.

Configuration

Windows 10 1909 x64, Visual Studio 2019 16.7.2, Net5.0 preview7

Regression?

Worked with netcoreapp3.1 target framework.

Other information

To replicate:

  • Create a new net core console application
  • Set project target framework to 5.0
  • Add <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.6" /> to the project.
  • Adjust Program.cs to:
    class Program
    {
        static void Main(string[] args)
        {
            var _ = Microsoft.Extensions.DependencyModel.DependencyContext.Default;
            Console.WriteLine("Hello World!");
        }
    }
  • Add publish profile with RunetimeIdentifier and PublishSingleFile, eg.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
	<PropertyGroup>
		<Configuration>Release</Configuration>
		<Platform>Any CPU</Platform>
		<PublishDir>bin\Release\net5.0\publish\</PublishDir>
		<RuntimeIdentifier>win-x64</RuntimeIdentifier>
		<PublishProtocol>FileSystem</PublishProtocol>
		<PublishSingleFile>True</PublishSingleFile>
	</PropertyGroup>
</Project>

Running the published program leads to the following stack-trace

Unhandled exception. System.IO.FileNotFoundException: Could not find file '[...]\ConsoleApp1\ConsoleApp1\bin\Release\net5.0\publish\ConsoleApp1.deps.json'.
File name: '[...]\ConsoleApp1\ConsoleApp1\bin\Release\net5.0\publish\ConsoleApp1.deps.json'
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream.OpenHandle(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, Int32 bufferSize, Boolean useAsync)
   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 ConsoleApp1.Program.Main(String[] args)

Running the program from the debugger or the normal bin/Release folder works fine.

I stumbled upon this problem because a third party library I use (Serilog.Settings.Configuration) seems to stumble upon this problem when loading available assemblies.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-DependencyModel untriaged New issue has not been triaged by the area owner labels Aug 24, 2020
@ghost
Copy link

ghost commented Aug 24, 2020

Tagging subscribers to this area: @eerhardt
See info in area-owners.md if you want to be subscribed.

@ghost
Copy link

ghost commented Aug 24, 2020

Tagging subscribers to this area: @swaroop-sridhar, @agocke
See info in area-owners.md if you want to be subscribed.

@eerhardt
Copy link
Member

@vitek-karas @swaroop-sridhar @agocke - what is the expectation of how to read the .deps.json file when PublishSingleFile=true?

The current DependencyModel code is reading the file from the APP_CONTEXT_DEPS_FILES app context variable. But that appears to be pointing to a file that doesn't exist.

@eerhardt eerhardt added this to the 5.0.0 milestone Aug 24, 2020
@eerhardt eerhardt removed the untriaged New issue has not been triaged by the area owner label Aug 24, 2020
@vitek-karas
Copy link
Member

I'll let @swaroop-sridhar correct me, but I think we simply don't support reading the .deps.json in sigle-file ... yet.
We should fix the APP_CONTEXT_DEPS_FILES as it should not point to non-existing file - filed #41267 to track that work (hopefully for .NET 5).

Just curious - what do you need the .deps.json content for?

@scamille
Copy link
Author

The error occured when using https://github.com/serilog/serilog-settings-configuration , the particular exception happens in https://github.com/serilog/serilog-settings-configuration/blob/dev/src/Serilog.Settings.Configuration/Settings/Configuration/Assemblies/AssemblyFinder.cs

I am not in any way associated with this project, but their usage of Microsoft.Extensions.DependencyModel.DependencyContext.Default static variable looks legitimate.

@vitek-karas
Copy link
Member

DependencyContext.Default should now always return null in single-file apps - after #41267 is fixed. So serilog should not crash anymore, but it won't find anything anyway since it's going to fallback to directory search which will only see the main executable. So the functionality is somewhat different.

We don't have room to fix the DependencyModel in .NET 5.0 anymore. We'll look into this for .NET 6.

@eerhardt
Copy link
Member

This work has now been completed in 6.0. The intentional behavior is:

  • DependencyContext.Default is attributed with
    [RequiresAssemblyFiles("DependencyContext for an assembly from a application published as single-file is not supported. The method will return null. Make sure the calling code can handle this case.")]
  • When you set PublishSingleFile=true, and use DependencyContext.Default in your application, you get a warning with the above message.
  • It no longer throws in a single file application, but instead returns null, as indicated by the message.

Closing this issue as this is now working as designed in 6.0.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants