【问题标题】:After clone of ASP.NET Core Web API project on other computer receiving MediatR error在收到 MediatR 错误的其他计算机上克隆 ASP.NET Core Web API 项目后
【发布时间】:2020-08-29 05:28:58
【问题描述】:

在我的计算机应用程序工作正常,但其他计算机在尝试处理任何请求后收到下一个错误:

为 MediatR.IRequestHandler`2[Application.User.Register+Command,Application.User.User] 类型的请求构造处理程序时出错。向容器注册您的处理程序。有关示例,请参阅 GitHub 中的示例 连接字符串

我在 Startup.cs 中使用以下行注册 MediatR:

services.AddMediatR(typeof(List.Handler).Assembly);

我的创业班:

 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DataContext>(opt =>
            {
                opt.UseSqlite(Configuration.GetConnectionString("DefaultConnection"));
            });
            services.AddCors(opt =>
            {
                opt.AddPolicy("CorsPolicy", policy =>
                {
                    policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:3000");
                });
            });

            services.AddMvc(opt => 
            {
                var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
                opt.Filters.Add(new AuthorizeFilter(policy));
            })
                .AddFluentValidation(cfg => cfg.RegisterValidatorsFromAssemblyContaining<Create>())
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            var builder = services.AddIdentityCore<AppUser>();
            var identityBuilder = new IdentityBuilder(builder.UserType, builder.Services);
            identityBuilder.AddEntityFrameworkStores<DataContext>();
            identityBuilder.AddSignInManager<SignInManager<AppUser>>();

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["TokenKey"]));
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(opt =>
                {
                    opt.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuerSigningKey = true,
                        IssuerSigningKey = key,
                        ValidateAudience = false,
                        ValidateIssuer = false
                    };
                });

            services.AddScoped<IJwtGenerator, JwtGenerator>();
            services.AddScoped<IUserAccessor, UserAccessor>();
            services.AddMediatR(typeof(List.Handler).Assembly);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMiddleware<ErrorHandlingMiddleware>();
            if (env.IsDevelopment())
            {
                //app.UseDeveloperExceptionPage();
            }
            else
            {
              //  app.UseHsts();
            }

            //  app.UseHttpsRedirection();
            app.UseAuthentication();
            app.UseCors("CorsPolicy");
            app.UseMvc();
        }
    }

问题是我无法在我的电脑上重现同样的问题。

【问题讨论】:

  • 您是否清理并重建了解决方案?
  • 是的,没有帮助
  • 确保对于User.Register Handler 类,所有依赖项都注册在服务集合中,但由于某种原因缺少其中一个
  • 双重检查,在我开发的电脑上工作正常,但笔记本电脑上的配置相同,接收错误
  • 你能分享你的启动文件吗,User.Register Handler 有哪些依赖项?

标签: c# asp.net-core asp.net-web-api mediatr


【解决方案1】:

问题在另一个层面:

 var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["TokenKey"]));

读取配置时出现问题。修复后,一切都按设计工作。

【讨论】:

    猜你喜欢
    • 2019-09-30
    • 2017-08-31
    • 1970-01-01
    • 2019-11-04
    • 2023-03-25
    • 1970-01-01
    • 2016-04-14
    • 2018-12-29
    • 1970-01-01
    相关资源
    最近更新 更多