DI Registration service type .net core 3.0

不打扰是莪最后的温柔 提交于 2020-01-25 08:34:07

问题


I have one n-tier layered app and in Infrastructure module where I'm trying to develop sending email for confirmation an user, I'm getting an error.

No service for type 'IMS.Infrastructure.Helpers.CustomEmailConfirmationTokenProvider`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.

From code what I had did is next:

public class CustomEmailConfirmationTokenProvider<TUser> : DataProtectorTokenProvider<TUser> where TUser : class
{
    public CustomEmailConfirmationTokenProvider(IDataProtectionProvider dataProtectionProvider, IOptions<DataProtectionTokenProviderOptions> options, ILogger<DataProtectorTokenProvider<TUser>> logger) : base(dataProtectionProvider, options)
    {
    }
}

and for creating services:

services.AddDbContext<ApplicationIdentityDbContext>(options =>
                options.UseSqlServer(configuration.GetConnectionString("Default")));

            services.AddIdentityCore<ApplicationUser>(options =>
                {
                    options.Password.RequireDigit = false;
                    options.Password.RequireLowercase = false;
                    options.Password.RequireNonAlphanumeric = false;
                    options.Password.RequireUppercase = false;
                    options.Password.RequiredLength = 4;

                    options.SignIn.RequireConfirmedEmail = true;
                    options.Tokens.ProviderMap.Add("CustomEmailConfirmation",
                        new TokenProviderDescriptor(
                            typeof(CustomEmailConfirmationTokenProvider<IdentityUser>)));

                    options.Tokens.EmailConfirmationTokenProvider = "CustomEmailConfirmation";

                })
                .AddEntityFrameworkStores<ApplicationIdentityDbContext>();



            services.AddTransient<CustomEmailConfirmationTokenProvider<ApplicationUser>>(o =>
            {
                var service = new CustomEmailConfirmationTokenProvider<ApplicationUser>(o.GetService<IDataProtectionProvider>(), o.GetService<IOptions<DataProtectionTokenProviderOptions>>(), o.GetService<ILogger<DataProtectorTokenProvider<ApplicationUser>>>());

                return service;
            });

I will need help to understand how service CustomEmailConfirmationTokenProvider is not registered, what I had did wrong ?

Kind Regards, Danijel


回答1:


From perspective of IoC container CustomEmailConfirmationTokenProvider<ApplicationUser> and CustomEmailConfirmationTokenProvider<IdentityUser> are two different and unrelated classes.

You should have both usage and registration to have same type of user.



来源:https://stackoverflow.com/questions/58972242/di-registration-service-type-net-core-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!