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

Blazor WebAssembly集成Fluxor时出现重复键异常,求解决方案

解决Fluxor中"An item with the same key has already been added. Key: MyApp.Frontend.Stores.CounterState"异常

针对你遇到的问题,可按以下步骤排查解决:

1. 排查重复的State定义

  • 确认项目中是否存在多个全限定名为MyApp.Frontend.Stores.CounterState的类:比如同一命名空间下的重复文件、未删除的旧代码文件、partial class错误拆分导致的重复定义。
  • 检查项目文件(.csproj),确认没有重复的<Compile Include="Stores/CounterState.cs" />这类条目,避免同一文件被多次编译。
  • 手动删除项目的bin和obj目录,清理编译残留后重新构建。

2. 检查Fluxor注册逻辑

  • 确认Program.cs中是否多次调用AddFluxor,或者同时存在自动扫描(ScanAssemblies)和手动注册CounterState的代码:
    // 错误示例:自动扫描+手动注册导致重复
    builder.Services.AddFluxor(options => options.ScanAssemblies(typeof(Program).Assembly));
    builder.Services.AddScoped<CounterState>(); // 需移除手动注册
    

3. 验证CounterStore的实现正确性

  • 确保CounterStore继承Feature<CounterState>时,没有重复定义相同的Feature逻辑,且GetName()返回值唯一:
    public class CounterStore : Feature<CounterState>
    {
        // 确保返回的名称与State类名一致,避免和其他Feature冲突
        public override string GetName() => nameof(CounterState);
        protected override CounterState GetInitialState() => new();
    }
    

4. 清理IDE编译缓存

  • 针对Rider环境,执行File -> Invalidate Caches...,选择Invalidate and Restart,清除IDE缓存后重新构建项目。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 16:50:20