Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 32079aa

Browse files
MarcoRossignoliweshaggard
authored andcommitted
Add Assembly.LoadFrom() fallback in AssemblyCatalog
Fixes #27433
1 parent ce4ebdf commit 32079aa

File tree

8 files changed

+92
-2
lines changed

8 files changed

+92
-2
lines changed

src/System.ComponentModel.Composition/System.ComponentModel.Composition.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.ComponentModel.Compo
77
{2D694AC8-A12F-4622-9405-74E20EC40C3A} = {2D694AC8-A12F-4622-9405-74E20EC40C3A}
88
EndProjectSection
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.ComponentModel.Composition.Noop.Assembly", "tests\System.ComponentModel.Composition.Noop.Assembly\System.ComponentModel.Composition.Noop.Assembly.csproj", "{396D6EBF-60BD-4DAF-8783-FB403E070A56}"
11+
ProjectSection(ProjectDependencies) = postProject
12+
{2D694AC8-A12F-4622-9405-74E20EC40C3A} = {2D694AC8-A12F-4622-9405-74E20EC40C3A}
13+
EndProjectSection
14+
EndProject
1015
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.ComponentModel.Composition", "src\System.ComponentModel.Composition.csproj", "{2D694AC8-A12F-4622-9405-74E20EC40C3A}"
1116
ProjectSection(ProjectDependencies) = postProject
1217
{DD3B8052-CE03-4159-8311-1CE1382C51B0} = {DD3B8052-CE03-4159-8311-1CE1382C51B0}
@@ -30,6 +35,10 @@ Global
3035
{59F4682D-C41D-45A7-9798-16C75525BB1D}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
3136
{59F4682D-C41D-45A7-9798-16C75525BB1D}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
3237
{59F4682D-C41D-45A7-9798-16C75525BB1D}.Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
38+
{396D6EBF-60BD-4DAF-8783-FB403E070A56}.Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
39+
{396D6EBF-60BD-4DAF-8783-FB403E070A56}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
40+
{396D6EBF-60BD-4DAF-8783-FB403E070A56}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
41+
{396D6EBF-60BD-4DAF-8783-FB403E070A56}.Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
3342
{2D694AC8-A12F-4622-9405-74E20EC40C3A}.Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
3443
{2D694AC8-A12F-4622-9405-74E20EC40C3A}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
3544
{2D694AC8-A12F-4622-9405-74E20EC40C3A}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
@@ -44,6 +53,7 @@ Global
4453
EndGlobalSection
4554
GlobalSection(NestedProjects) = preSolution
4655
{59F4682D-C41D-45A7-9798-16C75525BB1D} = {1A2F9F4A-A032-433E-B914-ADD5992BB178}
56+
{396D6EBF-60BD-4DAF-8783-FB403E070A56} = {1A2F9F4A-A032-433E-B914-ADD5992BB178}
4757
{2D694AC8-A12F-4622-9405-74E20EC40C3A} = {E107E9C1-E893-4E87-987E-04EF0DCEAEFD}
4858
{DD3B8052-CE03-4159-8311-1CE1382C51B0} = {2E666815-2EDB-464B-9DF6-380BF4789AD4}
4959
EndGlobalSection

src/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/AssemblyCatalog.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Diagnostics.CodeAnalysis;
99
using System.Diagnostics.Contracts;
1010
using System.Globalization;
11+
using System.IO;
1112
using System.Reflection;
1213
using System.Threading;
1314
using Microsoft.Internal;
@@ -566,7 +567,15 @@ private static Assembly LoadAssembly(string codeBase)
566567
assemblyName.CodeBase = codeBase;
567568
}
568569

569-
return Assembly.Load(assemblyName);
570+
try
571+
{
572+
return Assembly.Load(assemblyName);
573+
}
574+
//fallback attempt issue https://github.com/dotnet/corefx/issues/27433
575+
catch (FileNotFoundException)
576+
{
577+
return Assembly.LoadFrom(codeBase);
578+
}
570579
}
571580
}
572581
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<BuildConfigurations>
5+
netstandard;
6+
netcoreapp;
7+
</BuildConfigurations>
8+
</PropertyGroup>
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
4+
<PropertyGroup>
5+
<ProjectGuid>{396D6EBF-60BD-4DAF-8783-FB403E070A56}</ProjectGuid>
6+
</PropertyGroup>
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />
8+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" />
9+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Debug|AnyCPU'" />
10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Release|AnyCPU'" />
11+
<ItemGroup>
12+
<Compile Include="TestClass.cs" />
13+
</ItemGroup>
14+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
15+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace System.ComponentModel.Composition.Noop.Assembly
6+
{
7+
[Export]
8+
public class TestClass
9+
{
10+
}
11+
}

src/System.ComponentModel.Composition/tests/System.ComponentModel.Composition.Tests.csproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,15 @@
187187
<Compile Include="TestAssembly.cs" />
188188
<Compile Include="TransparentTestCase.cs" />
189189
</ItemGroup>
190+
<ItemGroup>
191+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
192+
</ItemGroup>
193+
<ItemGroup>
194+
<ProjectReference Include="System.ComponentModel.Composition.Noop.Assembly/System.ComponentModel.Composition.Noop.Assembly.csproj">
195+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
196+
<OutputItemType>content</OutputItemType>
197+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
198+
</ProjectReference>
199+
</ItemGroup>
190200
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
191-
</Project>
201+
</Project>

src/System.ComponentModel.Composition/tests/System/ComponentModel/Composition/Hosting/AssemblyCatalogTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,5 +1081,18 @@ public void ToString_ShouldReturnICompositionElementDisplayName()
10811081
Assert.Equal(catalog.DisplayName, catalog.ToString());
10821082
}
10831083
}
1084+
1085+
[Fact]
1086+
public void NonStaticallyReferencedAssembly()
1087+
{
1088+
string testAssembly = "System.ComponentModel.Composition.Noop.Assembly.dll";
1089+
var directory = TemporaryFileCopier.GetNewTemporaryDirectory();
1090+
Directory.CreateDirectory(directory);
1091+
var finalPath = Path.Combine(directory, testAssembly);
1092+
var sourcePath = Path.Combine(Directory.GetCurrentDirectory(), testAssembly);
1093+
File.Copy(sourcePath, finalPath);
1094+
var assemblyCatalog = new AssemblyCatalog(finalPath);
1095+
Assert.NotEmpty(assemblyCatalog);
1096+
}
10841097
}
10851098
}

src/System.ComponentModel.Composition/tests/System/ComponentModel/Composition/Hosting/DirectoryCatalogTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,19 @@ public void LoadedFiles_ContainsMultipleDllsAndSomeNonDll_ShouldOnlyContainDlls(
343343
cat.LoadedFiles);
344344
}
345345

346+
[Fact]
347+
public void LoadedFiles_NonStaticallyReferencedAssembly()
348+
{
349+
string testAssembly = "System.ComponentModel.Composition.Noop.Assembly.dll";
350+
var directory = TemporaryFileCopier.GetNewTemporaryDirectory();
351+
Directory.CreateDirectory(directory);
352+
var finalPath = Path.Combine(directory, testAssembly);
353+
var sourcePath = Path.Combine(Directory.GetCurrentDirectory(), testAssembly);
354+
File.Copy(sourcePath, finalPath);
355+
var catalog = new DirectoryCatalog(directory, "*.dll");
356+
Assert.NotEmpty(catalog);
357+
}
358+
346359
[Fact]
347360
public void Constructor_InvalidAssembly_ShouldBeFine()
348361
{

0 commit comments

Comments
 (0)