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

Using MvcMailer from a WCF service #44

Open
ghost opened this issue Apr 16, 2012 · 7 comments
Open

Using MvcMailer from a WCF service #44

ghost opened this issue Apr 16, 2012 · 7 comments

Comments

@ghost
Copy link

ghost commented Apr 16, 2012

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
Copy link
Owner

smsohan commented Apr 16, 2012

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:
#44

@Thejraj
Copy link

Thejraj commented Dec 20, 2012

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
Copy link
Owner

smsohan commented Dec 20, 2012

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//issues/44#issuecomment-11569808.

@jmogera
Copy link

jmogera commented Apr 30, 2013

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
Copy link

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
Copy link

@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. 👍

@ghost
Copy link
Author

ghost commented Jul 3, 2015

@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!

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

5 participants