Skip to content

Commit

Permalink
Reduce test projects and remove usage of Newtonsoft.Json in StartupHo…
Browse files Browse the repository at this point in the history
…ok tests (#84652)
  • Loading branch information
elinor-fung authored Apr 12, 2023
1 parent 82372ed commit 3be7ac1
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@
<RuntimeFrameworkVersion>$(MNAVersion)</RuntimeFrameworkVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" />
</ItemGroup>

</Project>
14 changes: 11 additions & 3 deletions src/installer/tests/Assets/TestProjects/PortableApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;

namespace PortableApp
{
Expand All @@ -14,9 +16,15 @@ public static void Main(string[] args)
Console.WriteLine(string.Join(Environment.NewLine, args));
Console.WriteLine(RuntimeInformation.FrameworkDescription);

// A small operation involving NewtonSoft.Json to ensure the assembly is loaded properly
var t = typeof(Newtonsoft.Json.JsonReader);
System.Diagnostics.Trace.WriteLine(t);
if (args.Length == 0)
return;

if (args[0] == "load_shared_library")
{
var asm = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName("SharedLibrary"));
FieldInfo field = asm.GetType("SharedLibrary.SharedType").GetField("Value");
Console.WriteLine($"SharedLibrary.SharedType.Value={field.GetValue(null)}");
}
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,42 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;

internal class StartupHook
{
public static void Initialize()
{
AssemblyLoadContext.Default.Resolving += SharedHostPolicy.SharedAssemblyResolver.Resolve;
Console.WriteLine($"Hello from startup hook in {(typeof(StartupHook).Assembly.GetName().Name)}!");

bool addResolver = Environment.GetEnvironmentVariable("TEST_STARTUPHOOK_ADD_RESOLVER") == true.ToString();
if (addResolver)
{
AssemblyLoadContext.Default.Resolving += OnResolving;
}

bool useDependency = Environment.GetEnvironmentVariable("TEST_STARTUPHOOK_USE_DEPENDENCY") == true.ToString();
if (useDependency)
{
UseDependency();
}
}
}

namespace SharedHostPolicy
{
public class SharedAssemblyResolver
[MethodImpl(MethodImplOptions.NoInlining)]
private static void UseDependency()
{
public static Assembly Resolve(AssemblyLoadContext context, AssemblyName assemblyName)
{
if (assemblyName.Name == "SharedLibrary")
{
string startupHookDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string sharedLibrary = Path.GetFullPath(Path.Combine(startupHookDirectory, "SharedLibrary.dll"));
return AssemblyLoadContext.Default.LoadFromAssemblyPath(sharedLibrary);
}
Console.WriteLine($"SharedLibrary.Value: {SharedLibrary.SharedType.Value}");
}

private static Assembly OnResolving(AssemblyLoadContext context, AssemblyName assemblyName)
{
if (assemblyName.Name != "SharedLibrary")
return null;
}

Console.WriteLine($"Resolving {assemblyName.Name} in startup hook");
string startupHookDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string sharedLibrary = Path.GetFullPath(Path.Combine(startupHookDirectory, "SharedLibrary.dll"));
return AssemblyLoadContext.Default.LoadFromAssemblyPath(sharedLibrary);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 3be7ac1

Please sign in to comment.