DI注册服务类型.net core 3.0 [英] DI Registration service type .net core 3.0

查看:134
本文介绍了DI注册服务类型.net core 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个n层分层的应用程序,并且在基础结构模块中,我试图开发发送电子邮件以确认用户的程序,但出现错误。

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.


没有
类型的服务'IMS.Infrastructure.Helpers.CustomEmailConfirmationTokenProvider`1 [Microsoft.AspNetCore.Identity.IdentityUser]'
已注册。

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)
    {
    }
}

并用于创建服务:

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;
            });

我将需要帮助来了解如何未注册CustomEmailConfirmationTokenProvider服务,我做错了什么?

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

亲切的问候,
Danijel

Kind Regards, Danijel

推荐答案

容器 CustomEmailConfirmationTokenProvider< ApplicationUser> CustomEmailConfirmationTokenProvider< IdentityUser> 是两个不同且不相关的类。

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.

这篇关于DI注册服务类型.net core 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆