Using Postal and Hangfire in Subsite

2019-05-23 09:23发布

I have been trying to use Postal on my MVC5 site. When I host my webpage a subsite ie, http://localhost/Subsite I am receiving the error

  • The virtual path '/' maps to another application, which is not allowed

I have debugged it down to when the ControllerContext is being created the HttpContext isn't getting set correctly. Since I'm running Postal from Hangfire the HttpContext.Current is always null. Postal creates the ContollerContext using the code below.

        ControllerContext CreateControllerContext()
    {
        // A dummy HttpContextBase that is enough to allow the view to be rendered.
        var httpContext = new HttpContextWrapper(
            new HttpContext(
                new HttpRequest("", UrlRoot(), ""),
                new HttpResponse(TextWriter.Null)
            )
        );
        var routeData = new RouteData();
        routeData.Values["controller"] = EmailViewDirectoryName;
        var requestContext = new RequestContext(httpContext, routeData);
        var stubController = new StubController();
        var controllerContext = new ControllerContext(requestContext, stubController);
        stubController.ControllerContext = controllerContext;
        return controllerContext;
    }

    string UrlRoot()
    {
        var httpContext = HttpContext.Current;
        if (httpContext == null)
        {
            return "http://localhost";
        }

        return httpContext.Request.Url.GetLeftPart(UriPartial.Authority) +
               httpContext.Request.ApplicationPath;
    }

How can I specify the UrlRoot so that instead of pulling the default of localhost to pull it based on my subsite?

2条回答
Lonely孤独者°
2楼-- · 2019-05-23 09:59

Below is another possible solution that I think is more elegant than above. It also resolves an issue that appears when accessing the MVC application while the background process is being executed.

public static void SendTypedEmailBackground()
{
    try
    {
        var engines = new ViewEngineCollection();
        var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));

        var eng = new FileSystemRazorViewEngine(viewsPath);
        engines.Add(eng);

        var email = new WebApplication1.Controllers.EmailController.TypedEmail();
        email.Date = DateTime.UtcNow.ToString();
        IEmailService service = new Postal.EmailService(engines);
        service.Send(email);

    }
    catch(Exception ex)
    {
        throw ex;
    }
}
查看更多
我想做一个坏孩纸
3楼-- · 2019-05-23 10:21

I followed the directions here http://docs.hangfire.io/en/latest/tutorials/send-email.html to send my email. The method in the tutorial is below

    public static void NotifyNewComment(int commentId)
{
// Prepare Postal classes to work outside of ASP.NET request
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));

var emailService = new EmailService(engines);

// Get comment and send a notification.
using (var db = new MailerDbContext())
{
    var comment = db.Comments.Find(commentId);

    var email = new NewCommentEmail
    {
        To = "yourmail@example.com",
        UserName = comment.UserName,
        Comment = comment.Text
    };

    emailService.Send(email);
}
}

I found the issue was that the FileSystemRazorViewEngine was not being used bty postal. To get the this to work I had to make sure that the FileSystemRazorViewEngine was the first engine in the available. I then removed it because I did not want it to be the default engine. Below is my updated method.

    public static void NotifyNewComment(int commentId)
{
// Prepare Postal classes to work outside of ASP.NET request
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var eng = new FileSystemRazorViewEngine(viewsPath));
ViewEngines.Engines.Insert(0, eng);

var emailService = new EmailService(engines);

// Get comment and send a notification.
using (var db = new MailerDbContext())
{
    var comment = db.Comments.Find(commentId);

    var email = new NewCommentEmail
    {
        To = "yourmail@example.com",
        UserName = comment.UserName,
        Comment = comment.Text
    };

    emailService.Send(email);
    ViewEngines.Engines.RemoveAt(0)
}
}
查看更多
登录 后发表回答