dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'context')

Error message

Value cannot be null. (Parameter 'context')

What it means

AddUiFrameworkPackageAnalyzer.Initialize throws ArgumentNullException because its AnalysisContext parameter was null. Roslyn analyzers receive the AnalysisContext from the compiler host during analyzer registration; the library guards explicitly so a misconfigured host or direct unit-test invocation fails fast instead of crashing later during ConfigureGeneratedCodeAnalysis or RegisterSemanticModelAction.

Solutions

  1. Ensure the analyzer is executed through a Roslyn host (Visual Studio, MSBuild/csc /analyzer, or Microsoft.CodeAnalysis testing packages) that supplies a valid AnalysisContext.
  2. In custom test harnesses, construct a real AnalysisContext (e.g. via AnalyzerOptions and a compilation start context from the Roslyn testing framework) rather than passing null.
  3. If you are hosting analyzers yourself, verify the CodeAnalysis runtime version matches the analyzer's target Roslyn version so context creation is not skipped.

Example fix

// before
new AddUiFrameworkPackageAnalyzer().Initialize(null);
// after
// Run via a real Roslyn session:
var compilation = CSharpCompilation.Create("proj", syntaxTrees);
compilation.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(new AddUiFrameworkPackageAnalyzer()));
Defensive patterns

Strategy: validation

Validate before calling

if (context == null) throw new ArgumentNullException(nameof(context)); // or only call Initialize via a real Roslyn host
analyzer.Initialize(context);

Type guard

static bool HasContext(AnalysisContext c) => c is not null;

Try / catch

try
{
    analyzer.Initialize(context);
}
catch (ArgumentNullException ex) when (ex.ParamName == "context")
{
    // log: analyzer invoked without a Roslyn context; fix host
}

Prevention

When it happens

Trigger: Calling AddUiFrameworkPackageAnalyzer.Initialize(null) directly, or a Roslyn analysis engine / test harness that supplies a null AnalysisContext when instantiating the analyzer.

Common situations: Custom analyzer test runners that forget to pass a context, mocking frameworks returning null from AnalysisContext factories, or embedding Roslyn analyzers in a non-standard tool host.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/a31f254fcec6e1ef. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive.Analyzers/Analyzers/AddUiFrameworkPackageAnalyzer.cs:136

            PackagingCategory,
            DiagnosticSeverity.Warning,
            isEnabledByDefault: true,
            description: ReferenceToRxUwpRequiredDescription,
            helpLinkUri: "https://github.com/dotnet/reactive");

        /// <inheritdoc/>
        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(
        [
            ReferenceToRxWindowsFormsRequiredRule,
            ReferenceToRxWpfRequiredRule,
            ReferenceToRxWindowsRuntimeRequiredRule,
            ReferenceToRxUwpRequiredRule
        ]);

        /// <inheritdoc/>
        public override void Initialize(AnalysisContext context)
        {
            if (context is null) {  throw new ArgumentNullException(nameof(context)); }

            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();

            context.RegisterSemanticModelAction(AnalyzeSemanticModel);
        }

        private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
        {
            // Note: our goal is to do as little work as possible in cases where we won't produce a
            // diagnostic. We expect not to need to report anything the majority of the time, so we
            // want our impact to be minimal.
            //
            // We have registered for this callback, and not for syntax node ones, because we only
            // want to run when there are errors.
            var d = context.SemanticModel.GetDiagnostics();
            foreach (var diag in d)
            {

View on GitHub (pinned to 94b5d5ab91)