Skip to content

Commit

Permalink
Ensure AppContext.TargetFrameworkName is set in the module initializer
Browse files Browse the repository at this point in the history
Since Costura.Template targets .NET Standard 2.0 we test `AppContext.TargetFrameworkName == null` which translates to `AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName == null` on .NET Framework and `Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName == null` on .NET Core.

Fixes Fody#633
  • Loading branch information
0xced committed Jan 17, 2021
1 parent 49e2006 commit aacf957
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/Costura.Template/ILTemplateWithTempAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using System.Threading;

internal static class ILTemplateWithTempAssembly
Expand All @@ -26,6 +27,20 @@ public static void Attach()
return;
}

var currentDomain = AppDomain.CurrentDomain;

// Make sure the target framework is set in order not to interfere with AppContext switches initialization
// See https://github.com/Fody/Costura/issues/633 for full explanation
if (AppContext.TargetFrameworkName == null)
{
var targetFrameworkAttribute = (TargetFrameworkAttribute)Assembly.GetCallingAssembly()?.GetCustomAttribute(typeof(TargetFrameworkAttribute));
var targetFrameworkName = targetFrameworkAttribute?.FrameworkName;
if (targetFrameworkName != null)
{
currentDomain.SetData(nameof(AppContext.TargetFrameworkName), targetFrameworkName);
}
}

//Create a unique Temp directory for the application path.
var md5Hash = "To be replaced at compile time";
var prefixPath = Path.Combine(Path.GetTempPath(), "Costura");
Expand All @@ -38,7 +53,6 @@ public static void Attach()
libList.AddRange(preloadList);
Common.PreloadUnmanagedLibraries(md5Hash, tempBasePath, libList, checksums);

var currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += ResolveAssembly;
}

Expand Down
16 changes: 15 additions & 1 deletion src/Costura.Template/ILTemplateWithUnmanagedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using System.Threading;

internal static class ILTemplateWithUnmanagedHandler
Expand All @@ -28,6 +29,20 @@ public static void Attach()
return;
}

var currentDomain = AppDomain.CurrentDomain;

// Make sure the target framework is set in order not to interfere with AppContext switches initialization
// See https://github.com/Fody/Costura/issues/633 for full explanation
if (AppContext.TargetFrameworkName == null)
{
var targetFrameworkAttribute = (TargetFrameworkAttribute)Assembly.GetCallingAssembly()?.GetCustomAttribute(typeof(TargetFrameworkAttribute));
var targetFrameworkName = targetFrameworkAttribute?.FrameworkName;
if (targetFrameworkName != null)
{
currentDomain.SetData(nameof(AppContext.TargetFrameworkName), targetFrameworkName);
}
}

//Create a unique Temp directory for the application path.
var md5Hash = "To be replaced at compile time";
var prefixPath = Path.Combine(Path.GetTempPath(), "Costura");
Expand All @@ -37,7 +52,6 @@ public static void Attach()
var unmanagedAssemblies = IntPtr.Size == 8 ? preload64List : preload32List;
Common.PreloadUnmanagedLibraries(md5Hash, tempBasePath, unmanagedAssemblies, checksums);

var currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += ResolveAssembly;
}

Expand Down

0 comments on commit aacf957

Please sign in to comment.