hermanho/postal.aspnetcore

Configuring services?

logany-hi opened this issue · 1 comments

I'd really appreciate an assist in getting this to actually start up. Unfortunately I've not found any documentation about configuring services or how to actually use this library in the first place.

I have, however, spent the last 3 hours on http://aboutcode.net/postal looking at the older MVC documentation which didn't get me anywhere either.

So here's what I've done:

I imported the nuget package reference:

<PackageReference Include="Postal.AspNetCore" Version="2.7.1" />

Added services to my startup as seemed logical:

services.AddScoped<IEmailService, Postal.EmailService>();
services.AddScoped<IEmailParser, Postal.EmailParser>();
services.AddScoped<IEmailViewRender, Postal.EmailViewRender>();

I'm still getting errors about initializing services:

System.AggregateException
  HResult=0x80131500
  Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Postal.IEmailService Lifetime: Scoped ImplementationType: Postal.EmailService': Unable to resolve service for type 'Postal.AspNetCore.ITemplateService' while attempting to activate 'Postal.EmailViewRender'.) (Error while validating the service descriptor 'ServiceType: Postal.IEmailParser Lifetime: Scoped ImplementationType: Postal.EmailParser': Unable to resolve service for type 'Postal.AspNetCore.ITemplateService' while attempting to activate 'Postal.EmailViewRender'.) (Error while validating the service descriptor 'ServiceType: Postal.IEmailViewRender Lifetime: Scoped ImplementationType: Postal.EmailViewRender': Unable to resolve service for type 'Postal.AspNetCore.ITemplateService' while attempting to activate 'Postal.EmailViewRender'.)
  Source=Microsoft.Extensions.DependencyInjection
  StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
   at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at BookingsPortal.Program.Main(String[] args) in C:\dev\Redacted\BookingsPortal\Program.cs:line 41

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Postal.IEmailService Lifetime: Scoped ImplementationType: Postal.EmailService': Unable to resolve service for type 'Postal.AspNetCore.ITemplateService' while attempting to activate 'Postal.EmailViewRender'.

Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'Postal.AspNetCore.ITemplateService' while attempting to activate 'Postal.EmailViewRender'.

Some documentation that isn't 9000 years old would be REALLY helpful at this point...

My implementation involves below is probably wrong so a working example I could use to correct would also be appreciated.

public class ConfirmationEmailTemplate : Email
{
    public string To { get; set; }
    public string MemberName { get; set; }
    public string BookingDate { get; set; }
    public string BookingTOD { get; set; }  // Time of Day
    public string QuestionnaireLink { get; set; }
    public string RegionAddress { get; set; }
}

And my controller:

public async Task<IActionResult> Index(int bookingId)
{
    var booking = await context.Bookings
        .Include(x => x.Member)
        .SingleAsync(x => x.Id == bookingId);

    // Removed most of the work which builds the values for the email properties.

    var email = new ConfirmationEmailTemplate
    {
        BookingDate = booking.Date.ToString("dd MMM yyyy"),
        BookingTOD = booking.Date.ToString("HH:mm"),
        MemberName = memberName,
        QuestionnaireLink = url,
        RegionAddress = sb.ToString(),
        To = memberEmail,
        Attachments = _attachments,
        ViewName = "ConfirmationEmailTemplate"
    };

    // Injected Postal.IEmailService for functionality to build the MailMessage
    var _email = await emailService.CreateMailMessageAsync(email);
    await emailService.SendAsync(_email);
    //await email.SendAsync(emailService);

    return new OkResult();
}

Found the samples, working on it.

Apologies.