Search code examples
c#.netwcfasp.net-mvc-4mvcmailer

using MvcMailer in WCF


We're creating an app that uses ASP.NET MVC4 for the web app and WCF for the web services that access a common data store.

The web app send emails using MvcMailer, I was hoping someone could point me to some resources that show how MvcMailer can be used from WCF as well (if the said resources exist).

I've gone through: https://github.com/smsohan/MvcMailer/issues/44

I'd rather not roll my own code, but I'm willing to go down that path if there's no better solution.

Thanks!


Solution

  • I was the guy who posted that issue on GitHub. There are no resources out there right now for doing that, so I had to figure it out myself. Here's the little guide I wrote to my co-workers. I should post it online somewhere.

    How to allow MvcMailer to function in your WCFService.

    1) Enable the HttpContext

    A) Add the following to your Web.Config

    <system.serviceModel>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />   
    </system.serviceModel>
    

    B) Add the following attribute to your service class.

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    //public class MyService
    //{
    //  ...
    //}
    

    2) Enable Razor

    A) Add the following to your Web.Config

    <system.web>
        <compilation debug="true" targetFramework="4.0">
            <assemblies>
                <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            </assemblies>
            <buildProviders>
                <add extension=".cshtml" type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
            </buildProviders>
        </compilation>
    </system.web>
    

    B) Add the following references to the WCF service project.

    System.Web.WebPages
    System.Web.WebPages.Razor
    

    C) Select the references, right click, and open Properties for them. Then set the following property:

    Copy Local = true
    

    Done! MvcMailer will now work in your WCF service!