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 · 2 comments
Open

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

haseebIC opened this issue Jun 30, 2021 · 2 comments

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.

@peakyblinder04
Copy link

It looks like the issue might be with the way you're registering the IEmailService in the Unity container. In your first example, you don’t seem to be providing the ViewEngines.Engines dependency that might be required by EmailService. Try adjusting your registration to something like this:

csharp
Copy code
container.RegisterType<IEmailService, EmailService>(
new InjectionConstructor(ViewEngines.Engines)
);
This way, you're explicitly passing the ViewEngines.Engines into the constructor of EmailService, which should fix the view resolution issue https://bodyly.nl/voedingsmiddelen-die-testosteron-verlagen-bij-mannen/.

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

3 participants