You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Blazor单例服务在页面间传递值时出现错误

Hey there, let's walk through the common issues that might be causing your error and how to fix them—since you didn't share the exact console error, I'll cover the most frequent scenarios with Blazor scoped services for cross-page data sharing:

1. Null Reference Exceptions (Most Common)

If your console is throwing a NullReferenceException, it’s almost always one of two things:

  • tradeValue is null: Before assigning values to AppData, add a null check to ensure you’re working with valid data:
    if (tradeValue != null)
    {
        AppData.tradeID = tradeValue.tradeId;
        AppData.utpID = tradeValue.utpid;
    }
    else
    {
        Console.WriteLine("Warning: tradeValue is null, could not populate AppData");
    }
    
  • AppData failed to inject: Double-check that the namespace in your Startup.cs registration matches exactly what you’re using in the Razor page. You can simplify this by adding the namespace to your _Imports.razor file:
    @* Add this to _Imports.razor *@
    @using Trade.FrontOffice.Client.ServicesRouting
    
    Then your page injection can be shortened to:
    @inject AppData AppData
    
    This reduces the chance of typos in long namespace strings.

2. Scoped Service Lifecycle Mismatch

Scoped services behave differently in Blazor Server vs. WebAssembly, and this can lead to unexpected data loss or errors:

  • Blazor Server: Scoped services are tied to a single SignalR connection. If you navigate using a traditional <a href="/page"> tag or trigger a full page refresh, a new connection is created, and your AppData instance is reset. Use NavigationManager instead for in-app navigation:
    @inject NavigationManager NavManager
    
    <button @onclick="() => NavManager.NavigateTo("/your-target-page")">Go to Target Page</button>
    
  • Blazor WebAssembly: Scoped services are per browser tab. Data will persist across navigation within the same tab, but opening a new tab will create a fresh AppData instance.

3. Need Cross-Tab/Connection Data? Use Singleton Instead

If your requirement is to share data across multiple tabs, connections, or user sessions (note: be careful with multi-user scenarios!), switch to a singleton service:

// Update Startup.cs registration
services.AddSingleton<Trade.FrontOffice.Client.ServicesRouting.AppData>();

⚠️ Important: Singleton services are globally shared. If this is a multi-user app, add a user identifier (like a unique session ID or user ID) to your AppData class to prevent data leakage between users.

4. Verify Service Registration Correctness

Double-check that your ConfigureServices method in Startup.cs has the correct registration. If you’ve imported the namespace, you can simplify it:

using Trade.FrontOffice.Client.ServicesRouting;

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<AppData>();
}

This eliminates namespace typos that can cause the service to not be found (leading to an InvalidOperationException at runtime).

If none of these fix your issue, share the exact error message from your browser console—this will help pinpoint the problem much faster!

内容的提问来源于stack exchange,提问作者Shrey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 03:02:35