Skip to content

Commit

Permalink
Use AssemblyLoadContext APIs on .NET Core (#5913)
Browse files Browse the repository at this point in the history
* Switch to netcoreapp2.1 target

* Remove targets file

* Clean up test project files

* Update to Particular.Packaging 0.11.0

* Use AssemblyLoadContext API on .NET Core
  • Loading branch information
bording authored Feb 11, 2021
1 parent 484f074 commit 6454809
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net472;netcoreapp2.1</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\NServiceBus.snk</AssemblyOriginatorKeyFile>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
Expand All @@ -11,9 +11,13 @@
<ProjectReference Include="..\NServiceBus.Core\NServiceBus.Core.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<Reference Include="System.Transactions" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="[3.12.0, 4.0.0)" />
<PackageReference Include="Particular.Packaging" Version="0.9.0" PrivateAssets="All" />
<PackageReference Include="Particular.Packaging" Version="0.11.0" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
<NoWarn>$(NoWarn);CA2007</NoWarn>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

Expand All @@ -18,17 +17,18 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" PrivateAssets="All" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Particular.Approvals" Version="0.2.0" PrivateAssets="All" />
<PackageReference Include="Particular.Packaging" Version="0.9.0" PrivateAssets="All" />
<PackageReference Include="Particular.Packaging" Version="0.11.0" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup>
<PackageId>NServiceBus.AcceptanceTests.Sources</PackageId>
<Description>Acceptance tests for NServiceBus core functionality</Description>
<IncludeBuildOutput>false</IncludeBuildOutput>
<BlockInvalidTargetFrameworks>false</BlockInvalidTargetFrameworks>
</PropertyGroup>

<!-- Workaround for https://github.com/dotnet/sdk/issues/1479 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" PrivateAssets="All" />
<PackageReference Include="NUnit" Version="[3.12.0, 4.0.0)" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Particular.Packaging" Version="0.9.0" PrivateAssets="All" />
<PackageReference Include="Particular.Packaging" Version="0.11.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="1.2.2" />
</ItemGroup>

Expand Down
9 changes: 8 additions & 1 deletion src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace NServiceBus.Hosting.Helpers
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
#if NETCOREAPP
using System.Runtime.Loader;
#endif
using System.Text;
using Logging;

Expand Down Expand Up @@ -153,8 +156,12 @@ bool TryLoadScannableAssembly(string assemblyPath, AssemblyScannerResults result

try
{
#if NETCOREAPP
var context = AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly());
assembly = context.LoadFromAssemblyPath(assemblyPath);
#else
assembly = Assembly.LoadFrom(assemblyPath);

#endif
return true;
}
catch (Exception ex) when (ex is BadImageFormatException || ex is FileLoadException)
Expand Down
4 changes: 2 additions & 2 deletions src/NServiceBus.Core/Hosting/Helpers/AssemblyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#if NETFRAMEWORK
using System.Reflection;
#endif
#if NETSTANDARD
#if NETCOREAPP
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
Expand Down Expand Up @@ -39,7 +39,7 @@ public void ValidateAssemblyFile(string assemblyPath, out bool shouldLoad, out s
}
#endif

#if NETSTANDARD
#if NETCOREAPP
public void ValidateAssemblyFile(string assemblyPath, out bool shouldLoad, out string reason)
{
using (var stream = File.OpenRead(assemblyPath))
Expand Down
33 changes: 2 additions & 31 deletions src/NServiceBus.Core/Licensing/Browser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if NETSTANDARD
namespace NServiceBus
namespace NServiceBus
{
using System.Diagnostics;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -39,32 +38,4 @@ public static bool TryOpen(string url)
return true;
}
}
}
#else
namespace NServiceBus
{
using System.Diagnostics;

static class Browser
{
public static bool TryOpen(string url)
{
using (var process = new Process())
{
process.StartInfo.FileName = url;

try
{
process.Start();
}
catch
{
return false;
}
}

return true;
}
}
}
#endif
}
6 changes: 0 additions & 6 deletions src/NServiceBus.Core/Licensing/LicenseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ namespace NServiceBus
{
using System;
using System.Diagnostics;
#if NETSTANDARD
using System.Runtime.InteropServices;
#endif
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -178,7 +176,6 @@ string CreateDeveloperLicenseUrl()
return $"https://particular.net/license/nservicebus?v={version}&t={isRenewal}&p={platform}";
}

#if NETSTANDARD
string GetPlatformCode()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand All @@ -198,9 +195,6 @@ string GetPlatformCode()

return "unknown";
}
#else
string GetPlatformCode() => "windows";
#endif

internal ActiveLicenseFindResult result;

Expand Down
20 changes: 7 additions & 13 deletions src/NServiceBus.Core/NServiceBus.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net472;netcoreapp2.1</TargetFrameworks>
<RootNamespace>NServiceBus</RootNamespace>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\NServiceBus.snk</AssemblyOriginatorKeyFile>
Expand All @@ -17,26 +17,22 @@
<Reference Include="System.Security" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
<PackageReference Include="System.Security.Cryptography.Xml" Version="4.7.0" />
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="System.Security.Cryptography.Xml" Version="5.0.0" />
</ItemGroup>

<!-- Public dependencies -->
<ItemGroup>
<ItemGroup Label="Public dependencies">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ItemGroup Label="Private dependencies">
<PackageReference Include="Fody" Version="6.3.0" PrivateAssets="All" />
<PackageReference Include="Janitor.Fody" Version="1.8.0" PrivateAssets="All" />
<PackageReference Include="LightInject.Source" Version="5.0.3" PrivateAssets="All" />
<PackageReference Include="SimpleJson" Version="0.38.0" PrivateAssets="All" />
<PackageReference Include="Obsolete.Fody" Version="5.2.1" PrivateAssets="All" />
<PackageReference Include="Particular.Licensing.Sources" Version="3.4.0" PrivateAssets="All" />
<PackageReference Include="Particular.Packaging" Version="0.9.0" PrivateAssets="All" />
<PackageReference Include="Particular.Packaging" Version="0.11.0" PrivateAssets="All" />
<PackageReference Include="SimpleJson" Version="0.38.0" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup>
Expand All @@ -45,8 +41,6 @@
</PropertyGroup>

<ItemGroup>
<None Remove="NServiceBus.targets" />
<Content Include="NServiceBus.targets" PackagePath="build" />
<None Include="..\..\packaging\nuget\tools\init.ps1" Pack="true" PackagePath="tools" Visible="false" />
<None Include="..\NServiceBus.Core.Analyzer\bin\$(Configuration)\**\NServiceBus.Core.Analyzer.dll" Pack="true" PackagePath="analyzers/dotnet/cs/NServiceBus.Core.Analyzer.dll" Link="NServiceBus.Core.Analyzer.dll" Visible="false" />
<None Include="..\NServiceBus.Core.Analyzer\tools\*.ps1" Pack="true" PackagePath="tools" Visible="false" />
Expand Down
7 changes: 0 additions & 7 deletions src/NServiceBus.Core/NServiceBus.targets

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ LightInject.Microsoft.DependencyInjection version 3.2.0
namespace LightInject.Microsoft.DependencyInjection
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand Down
Loading

0 comments on commit 6454809

Please sign in to comment.