简体   繁体   中英

Locating included assemblies in trimmed self-contained deployment

I'm learning C# and it's related stack and trying to build trimmed self-contained executable. The problem is in that some assemblies are not directly referenced and are implied to be used via Managed Extensibility Framework, thus trimmer doesn't count them as used. I'm including them in scd using <TrimmerRootAssembly Include="x"/> ), but they are not "visible" to code, even though they are referenced in the project. Here is my csproj:

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

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.0</TargetFramework>
        <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
        <PublishReadyToRun>true</PublishReadyToRun>
        <PublishTrimmed>true</PublishTrimmed>
        <PublishSingleFile>true</PublishSingleFile>
    </PropertyGroup>

    <ItemGroup>
      <TrimmerRootAssembly Include="Ayte.Tool.Toolkit.Handler.*" />
      <ProjectReference Include="..\Ayte.Tool.Toolkit.Core\Ayte.Tool.Toolkit.Core.csproj" />
      <ProjectReference Include="..\Ayte.Tool.Toolkit.Handler.DotNet\Ayte.Tool.Toolkit.Handler.DotNet.csproj" />
      <ProjectReference Include="..\Ayte.Tool.Toolkit.Handler.JVM\Ayte.Tool.Toolkit.Handler.JVM.csproj" />
      <ProjectReference Include="..\Ayte.Tool.Toolkit.Handler.Toolkit\Ayte.Tool.Toolkit.Handler.Toolkit.csproj" />
    </ItemGroup>

</Project>

Ayte.Tool.Toolkit.Handler.Toolkit is successfully included in SCD and is accessible by my application:

public static void Main(string[] args)
{            
    Console.WriteLine(Assembly.Load("Ayte.Tool.Toolkit.Handler.Toolkit"));
}

// Ayte.Tool.Toolkit.Handler.Toolkit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

But without direct usage of assembly classes / explicit by-name load it doesn't appear neither in AppDomain.CurrentDomain.GetAssemblies() or Assembly.GetExecutingAssembly().GetReferencedAssemblies() :

Current domain assemblies:
System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
Ayte.Tool.Toolkit.CLI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
System.Console, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Executing assembly references:
System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Console, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Runtime.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e

How can i get list of all assemblies that are contained within SCD to eagerly load them? Any direct reference by name, class load or anything else breaks the whole MEF concept, so i would like not to do it in any by-hand method.

One of the solutions would be install Microsoft.Extensions.DependencyModel and use DependencyContext.Default . The correct way is:

var assemblies = DependencyContext.Default.GetRuntimeAssemblyNames(runtimeIdentifier);

I'm not sure how to properly resolve runtimeIdentifier , but using non-existing identifier (eg String.Empty ) will fall back to default set of assembly names, which was sufficient for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM