Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Postal with Unity IoC Container MVC 5? #182

Open
haseebIC opened this issue Jun 30, 2021 · 1 comment
Open

How to use Postal with Unity IoC Container MVC 5? #182

haseebIC opened this issue Jun 30, 2021 · 1 comment

Comments

@haseebIC
Copy link

I am having trouble with Postal setup. I have registered dependencies like below:

container.RegisterType<IEmailParser, EmailParser>(); container.RegisterType<IEmailViewRenderer, EmailViewRenderer>(); container.RegisterType<IEmailService, EmailService>();

On the controller side I have the following code:

private readonly IEmailService _emailService;

public EmailsController(IEmailService emailService)
{
this._emailService = emailService;
}

public ActionResult Test()
{
var email = new TestEmail
{

        };
        var message = _emailService.CreateMailMessage(email);
        return new EmailViewResult(email);

}

The dependencies are being injected properly by the unity container but the target view ("Test") is not being found by ViewEngine.

However, when I have the following code in the controller everything works fine.

private readonly IEmailService _emailService = new EmailService(ViewEngines.Engines);;

public ActionResult Test()
{
var email = new TestEmail
{

        };
        var message = _emailService.CreateMailMessage(email);
        return new EmailViewResult(email);

}

I believe I'm doing something wrong while registering the unity container. Please help me out.

Thanks

@robertmack812
Copy link

Try registering IEmailService with a factory method that injects ViewEngines.Engines explicitly, like this: container.RegisterType(new InjectionFactory(c => new EmailService(ViewEngines.Engines)));. This Solution should ensure that the ViewEngines are passed properly during injection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants