Skip to content

chore: Fix error when running benchmark with file-based app#3198

Merged
timcassell merged 2 commits into
dotnet:masterfrom
filzrev:chore-fix-error-on-file-based-app
Jul 23, 2026
Merged

chore: Fix error when running benchmark with file-based app#3198
timcassell merged 2 commits into
dotnet:masterfrom
filzrev:chore-fix-error-on-file-based-app

Conversation

@filzrev

@filzrev filzrev commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

This PR intended following validation error when running benchmark with file-based apps.

// * Assembly run, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null is located in temp. If you are running benchmarks from xUnit you need to disable shadow copy. It's not supported by design.

What's changed in this PR

1. Portability/RuntimeInformation.cs
Add IsFileBasedApp API to detect benchmark is running on File-based apps or not.

2. ShadowCopyValidator.cs
Add logics to skip validatation when running file-based apps.

3. MosCpuDetector.cs
Add logics to avoid following errors when running file-based apps without PublishAot=false setting.
(Note: As far as I knows, Currently Benchmark Host is not support NativeAot, Because it use reflections)

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.
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at System.Management.MTAHelper.CreateInMTA(Type type)
   at System.Management.ManagementPath.CreateWbemPath(String path)
   at System.Management.ManagementPath..ctor(String path)
   at System.Management.ManagementPath..cctor()
   at System.Runtime.CompilerServices.InitHelpers.CallClassConstructor(Void* cctor, Void* instantiatingArg, Exception* pException)

TestCode

dotnet main.cs -c Release

#!/usr/bin/env dotnet

// Use latest preview version
#:package BenchmarkDotNet@*-preview*
#:property PublishAot=false

using System.Collections;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.InProcess.Emit;

var config = DefaultConfig.Instance
    .AddJob(Job.ShortRun.WithToolchain(InProcessEmitToolchain.Default));

if(args.Length == 0)
    args = ["--filter", "*"];
    
var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly)
                                 .Run(args, config)
                                 .ToArray();

if (summaries.Any(x => x.Reports.Any(r => !r.Success)))
    return 1;
return 0;

namespace Benchmarks
{
    public class SampleBenchmarks
    {
        [Benchmark]
        public bool Execute() 
        {
            return true;
        }
    }
}

@timcassell

timcassell commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

3. MosCpuDetector.cs
Add logics to avoid following errors when running file-based apps without PublishAot=false setting.
(Note: As far as I knows, Currently Benchmark Host is not support NativeAot, Because it use reflections)

We should rather catch the exception like we do in other parts of the code that use COM. Built-in COM can be disabled without AOT, and I believe it can be enabled with AOT.

Comment thread src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs Outdated
Comment thread src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs Outdated
@filzrev
filzrev force-pushed the chore-fix-error-on-file-based-app branch from a5d872c to 2cfc0c7 Compare July 23, 2026 04:30
@filzrev

filzrev commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

We should rather catch the exception like we do in other parts of the code that use COM. Built-in COM can be disabled without AOT, and I believe it can be enabled with AOT.

I've updated code based on following try-catch code.

// COM may be disabled, in which case this will throw (#2253).
// It could be NotSupportedException or COMException, we just catch all.
catch
{
return null;
}

@filzrev
filzrev force-pushed the chore-fix-error-on-file-based-app branch from 2cfc0c7 to 49bcf23 Compare July 23, 2026 04:42
@timcassell
timcassell merged commit c754529 into dotnet:master Jul 23, 2026
11 checks passed
@timcassell timcassell added this to the v0.16.0 milestone Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants