使用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:
tradeValueis null: Before assigning values toAppData, 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"); }AppDatafailed to inject: Double-check that the namespace in yourStartup.csregistration matches exactly what you’re using in the Razor page. You can simplify this by adding the namespace to your_Imports.razorfile:
Then your page injection can be shortened to:@* Add this to _Imports.razor *@ @using Trade.FrontOffice.Client.ServicesRouting
This reduces the chance of typos in long namespace strings.@inject AppData AppData
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 yourAppDatainstance is reset. UseNavigationManagerinstead 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
AppDatainstance.
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

