Skip to content

DependencyContext.Load not loading compile library assemblies #8805

Closed

Description

@Kurira

Steps to reproduce

dotnet new console

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.Cli.Utils" Version="2.0.0" />
  </ItemGroup>

</Project>
using Microsoft.Extensions.DependencyModel;
using System;
using System.Linq;

namespace Core
{
    class Program
    {
        static void Main()
        {
            var context = DependencyContext.Load(typeof(Program).Assembly);

            Console.WriteLine("Libraries: " + context.CompileLibraries.Count);
            Console.WriteLine("Assemblies: " + context.CompileLibraries.Count(library => 
                library.Assemblies.Any()));
        }
    }
}

Expected behavior

context.CompileLibraries having Assemblies property filled with assembly paths.
This used to work in previous versions.

Actual behavior

Assemblies always empty.

Environment data

.NET Command Line Tools (2.0.0)

Product Information:
 Version:            2.0.0
 Commit SHA-1 hash:  cdcd1928c9

Runtime Environment:
 OS Name:     Windows
 OS Version:  6.1.7601
 OS Platform: Windows
 RID:         win7-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.0.0\

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.0
  Build    : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d

Does my project needs to have some kind of configuration for this to work?
Or it has to be loaded in a different way to get compiled assembly paths.

Activity

  1. livarcocc commented on Nov 11, 2017

    @livarcocc
    Contributor

    @eerhardt any idea when/why this might have changed? @Kurira, it would be helpful if you could indicate when this worked before. I believe the DependencyContext is constructed using the deps.json file for your app, as such I am not sure how it would find absolute paths to assemblies. I haven't dealt much with that code, so it is completely possible it is looking at actual assemblies, but I will let @eerhardt say for sure.

  2. Kurira commented on Nov 11, 2017

    @Kurira
    Author

    @livarcocc it used to work on v1.x~, and even now I see mvc core doing something similar to this and it works there. I'm not sure if any prep work needs to be done here, for this to work.

  3. eerhardt commented on Nov 11, 2017

    @eerhardt
    Member

    You need to set a property in your .csproj for the compile assemblies to work.

    Under a PropertyGroup element in your csproj, add a PreserveCompilationContext element and set its value to “true”.

    There was a similar property in the old project.json file.

  4. Kurira commented on Nov 11, 2017

    @Kurira
    Author

    Yes, that helped, thank you @eerhardt! I somehow thought I tried that already.
    Will do some testing with custom tools and will close it if everything is fine.

  5. livarcocc commented on Nov 11, 2017

    @livarcocc
    Contributor

    Let us know how it goes. I frequently forget about PreserveCompilationContext.

  6. Kurira commented on Nov 18, 2017

    @Kurira
    Author

    Thanks, seems like it's working even without PreserveCompilationContext if you are using that code in a tool.

  7. transferred this issue fromdotnet/clion Jan 31, 2020
  8. added this to the Discussion milestone on Jan 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions