AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UI.Services.IEmailSender'

Open paul000 opened this issue 8 years ago • 1 comments

I am following the Account confirmation and password recovery section verbatim but I'm still stuck on getting account confirmation and email recovery working. After scaffolding Identity it seems I having problems with, per a Stackoverflow respone "the same user data model". After running the app and clicking on the Register button I get the following response -------

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UI.Services.IEmailSender' while attempting to activate 'IdentityEmail.Areas.Identity.Pages.Account.RegisterModel'. Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired) lambda_method(Closure , IServiceProvider , object[] ) Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelActivatorProvider+<>c__DisplayClass1_0.<CreateActivator>b__0(PageContext context) Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelFactoryProvider+<>c__DisplayClass3_0.<CreateModelFactory>b__0(PageContext pageContext) Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.CreateInstance() Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I ran the app again in debug mode and the first lines I get after I hit the Register button -------------

Exception thrown: 'System.InvalidOperationException' in Microsoft.Extensions.DependencyInjection.Abstractions.dll Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll

My StartUp.cs file ------

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context =&gt; true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.Configure&lt;MvcOptions&gt;(options =&gt;
        {
            options.Filters.Add(new RequireHttpsAttribute());
        });

        services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity&lt;IdentityUser, IdentityRole&gt;(configure =&gt;
        {
            configure.SignIn.RequireConfirmedEmail = true;
        }
            )
            .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
            .AddDefaultTokenProviders();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddRazorPagesOptions(options =&gt;
            {
                options.AllowAreas = true;
                options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
                options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
            });

        services.ConfigureApplicationCookie(options =&gt;
        {
            options.LoginPath = $"/Identity/Account/Login";
            options.LogoutPath = $"/Identity/Account/Logout";
            options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
        });

        services.AddSingleton&lt;IEmailSender, EmailSender&gt;();

        services.Configure&lt;AuthMessageSenderOptions&gt;(Configuration);
    }
    ---------------

    Since IEmailSender is part of the first error that was generated here is the IEmailSender file

    using System.Threading.Tasks;
    using Microsoft.Extensions.Options;
    using SendGrid;
    using SendGrid.Helpers.Mail;

namespace IdentityEmail.Services { public interface IEmailSender { Task SendEmailAsync(string email, string subject, string message); } }

Is there somewhere else to look?

Paul


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

paul000 avatar Jul 10 '18 15:07 paul000

Dup of #7146 I hope to get this updated soon.

Rick-Anderson avatar Jul 10 '18 16:07 Rick-Anderson