smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
584 stars 178 forks source link

Using MvcMailer from a WCF service #44

Open ghost opened 12 years ago

ghost commented 12 years ago

Hello, I wanted to use MvcMailer from a WCF service, and found only a small modification to MvcMailer helped this along dramatically. I wanted to share it with you, as I do not have an easy way to submit my changes.

WCF services have access to an HttpContext when aspNetCompatibilityEnabled="true" in the Web.config and the following attribute is added to the service class:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

After that, the only issue with MvcMailer is that it wanted to have a routing table for the ControllerContext constructor. Rather than try to fake this, we can use a different ControllerConext constructor which only requires an HttpRequest and the ControllerBase.

So if you modify the MailerBase.CreateControllerContext() method as such, you can then use MvcMailer in an WCF service.

private ControllerContext CreateControllerContext()
{
    ControllerContext = new ControllerContext(CurrentHttpContext.Request.RequestContext, this);
    return ControllerContext;
}

There doesn't seem to be any drawbacks to using this alternative constructor, since MvcMailer doesn't use the routing table anyway.

I'd like to see this worked into a future release since it solves some issues with using MvcMailer and WCF (which really is a great way to separate your email from your project!).

Thanks

smsohan commented 12 years ago

Thanks Spencer. I will consider this.


Sent from my iPhone S M Sohan

On 2012-04-16, at 6:41 AM, Spencer Drager reply@reply.github.com wrote:

Hello, I wanted to use MvcMailer from a WCF service, and found only a small modification to MvcMailer helped this along dramatically. I wanted to share it with you, as I do not have an easy way to submit my changes.

WCF services have access to an HttpContext when aspNetCompatibilityEnabled="true" in the Web.config and the following attribute is added to the service class:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

After that, the only issue with MvcMailer is that it wanted to have a routing table for the ControllerContext constructor. Rather than try to fake this, we can use a different ControllerConext constructor which only requires an HttpRequest and the ControllerBase.

So if you modify the MailerBase.CreateControllerContext() method as such, you can then use MvcMailer in an WCF service.

       private ControllerContext CreateControllerContext()
       {
           ControllerContext = new ControllerContext(CurrentHttpContext.Request.RequestContext, this);
           return ControllerContext;
       }

There doesn't seem to be any drawbacks to using this alternative constructor, since MvcMailer doesn't use the routing table anyway.

I'd like to see this worked into a future release since it solves some issues with using MvcMailer and WCF (which really is a great way to separate your email from your project!).

Thanks


Reply to this email directly or view it on GitHub: https://github.com/smsohan/MvcMailer/issues/44

Thejraj commented 11 years ago

I tried to make this implementation. But i stuck in the beginning itself, has i couldn't find the source code for MVC Mailer (Can you share the link where i can find this) and will you pls elaborate this in steps...

smsohan commented 11 years ago

The source code is on github.


Sent from my iPhone Sohan SM

On 2012-12-20, at 4:14 AM, Thejraj notifications@github.com wrote:

I tried to make this implementation. But i stuck in the beginning itself, has i couldn't find the source code for MVC Mailer (Can you share the link where i can find this) and will you pls elaborate this in steps...

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/44#issuecomment-11569808.

jmogera commented 11 years ago

Not sure what to with the CreateControllerContext() and where. Do you have example of it? I downloaded the source code but not sure where to look for the WCF implementation. Please advice!

Here is the test project that I have created: http://sdrv.ms/101uaPX

Somasekar-N commented 9 years ago

Hi, I can't user ViewBag, getting error as "The name 'ViewBag' does not exist in the current context", is there any fix for this?

shishirkushwaha commented 9 years ago

@smdrager Thanks, its just works like a blaze. Although I had to add ControllerContext = new ControllerContext(CurrentHttpContext.Request.RequestContext, this); in constructor of MyMailer implementation.

You forgot to mention the thing about ControllerContext on your Stackoverflow answer. Again, so many thanks to you. :+1:

ghost commented 9 years ago

@shishirkushwaha Glad I was of assistance. I didn't mention the part in this Github issue since it was linked to in the question. I wished by now the MvcMailer authors had implemented this change since there is no downside.

Happy email sending!