diff --git a/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs b/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs index 9cbe145650..6c2a39b361 100644 --- a/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs +++ b/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs @@ -24,8 +24,9 @@ internal class MosCpuDetector : ICpuDetector double maxFrequency = 0; double nominalFrequency = 0; - using (var mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor")) + try { + using var mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); foreach (var moProcessor in mosProcessor.Get().Cast()) { string? name = moProcessor[WmiCpuInfoKeyNames.Name]?.ToString(); @@ -45,6 +46,12 @@ internal class MosCpuDetector : ICpuDetector } } } + catch + { + // System.TypeInitializationException: The type initializer for 'System.Management.ManagementPath' threw an exception. + // --->System.NotSupportedException: Built-in COM has been disabled via a feature switch.See https://aka.ms/dotnet-illink/com for more information. + return null; + } string? processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null; Frequency? maxFrequencyActual = maxFrequency > 0 && processorsCount > 0 @@ -64,4 +71,4 @@ internal class MosCpuDetector : ICpuDetector MaxFrequencyHz = maxFrequencyActual?.Hertz.RoundToLong() }; } -} \ No newline at end of file +} diff --git a/src/BenchmarkDotNet/Portability/RuntimeInformation.cs b/src/BenchmarkDotNet/Portability/RuntimeInformation.cs index 2e026d8f07..dd4e1f6022 100644 --- a/src/BenchmarkDotNet/Portability/RuntimeInformation.cs +++ b/src/BenchmarkDotNet/Portability/RuntimeInformation.cs @@ -76,6 +76,11 @@ public static bool IsNativeAOT && IsAot && !IsWasm && !IsMono; // Wasm and MonoAOTLLVM are also AOT + // File-based apps contains specific RuntimeHostConfigurationOptions. + // https://github.com/dotnet/dotnet/blob/v10.0.302/src/sdk/documentation/general/dotnet-run-file.md + public static bool IsFileBasedApp => AppContext.GetData("EntryPointFilePath") != null + && AppContext.GetData("EntryPointFileDirectoryPath") != null; + public static readonly bool IsRunningInContainer = string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), "true"); internal static string GetArchitecture() => GetCurrentPlatform().ToString(); diff --git a/src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs b/src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs index c5a0a3b373..97b09e6948 100644 --- a/src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs +++ b/src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs @@ -1,3 +1,4 @@ +using BenchmarkDotNet.Portability; using System.Reflection; namespace BenchmarkDotNet.Validators @@ -11,7 +12,12 @@ private ShadowCopyValidator() { } public bool TreatsWarningsAsErrors => false; public IAsyncEnumerable ValidateAsync(ValidationParameters validationParameters) - => validationParameters + { + // Skip validation when running on file-based app. because it's executed on temp directory. + if (RuntimeInformation.IsFileBasedApp) + return AsyncEnumerable.Empty(); + + return validationParameters .Benchmarks .Select(benchmark => benchmark.Descriptor.Type.GetTypeInfo().Assembly) .Distinct() @@ -22,5 +28,6 @@ public IAsyncEnumerable ValidateAsync(ValidationParameters vali $"Assembly {assembly} is located in temp. If you are running benchmarks from xUnit you need to disable shadow copy. It's not supported by design.") ) .ToAsyncEnumerable(); + } } } \ No newline at end of file