4

I have tried other solutions posted to stackoverflow and have found none that work, here is my problem.

So I want to send an email using hangfire through my MVC application, this works on my local machine but when I upload it to a remote server I get the following error on hangfire:

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

This is the code I use to send the email:

foreach (var EmailEntry in EmailEntries)
        {
            var email = new EmailTemplateModel
            {
                ViewName = "EmailTemplateModel",
                FromAddress = "donotreply@emailservice.com",
                EmailAddress = EmailEntry,
                Subject = "Task Report",
                Date = Dates,
                Task = DatesAndTasks,
            };
            email.Send();
        }

When I use the 'ViewName' method it returns '~/Views/Emails' on my local machine.

Inside of the Send() method:

// Summary:
        //     Convenience method that sends this email via a default EmailService.
        public void Send();

Application structure in IIS:

Server > Sites > Default > MyApplication

Issue raised by JodyL's solution below:

StructureMapDependencyScope.get_CurrentNestedContainer() 

enter image description here

Solution:

Edited the following code in 'StructureMapDependencyScope' class:

Before:

public IContainer CurrentNestedContainer {
            get {
                return (IContainer)HttpContext.Items[NestedContainerKey];
            }
            set {
                HttpContext.Items[NestedContainerKey] = value;
            }
        }

After:

public IContainer CurrentNestedContainer {
            get {
                IContainer container = null;
                if (HttpContext != null)
                    container = (IContainer)HttpContext.Items[NestedContainerKey];
                return container;
            }
            set {
                HttpContext.Items[NestedContainerKey] = value;
            }
        } 

Before:

private HttpContextBase HttpContext {
            get {
                var ctx = Container.TryGetInstance<HttpContextBase>();
                return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);
            }
        }

After:

private HttpContextBase HttpContext {
            get {
                var ctx = Container.TryGetInstance<HttpContextBase>();
                if (ctx == null && System.Web.HttpContext.Current == null)
                    return null;

                return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);
            }
        } 
GeorgeB
  • 808
  • 1
  • 8
  • 26
  • https://www.hangfire.io/pricing/ – Lex Li Apr 20 '18 at 11:57
  • Could be related to `var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));` from your previous question, but you have no provided enough details in this current question. provider a [mcve] – Nkosi Apr 20 '18 at 12:18
  • I'm using IIS 7 on the remote server and the error occurs when I try to send the email, if there is anything else you need me to add, please ask :) – GeorgeB Apr 20 '18 at 12:24
  • Hello! If the problem happens inside the method `Send()` could you please share the code of that method with us? Adding to that, what does the structure of the application in IIS look like in your case? – Oleg Safarov Apr 21 '18 at 07:35
  • I hope the additions to the question are sufficient – GeorgeB Apr 23 '18 at 07:24
  • We barely have any information. Certianly not enough information to solve anything. We need a complete example, something we can run and debug. The most obvious cause of this is that you are trying to use a virtual path that starts with a `/` instead of a `~` somewhere, which isn't allowed. – caesay Apr 24 '18 at 18:31
  • I could provide a small version of the project for people to run but the test would not be fair. It would also require a local IIS for the solution to run. – GeorgeB Apr 26 '18 at 13:02
  • How about it [Using Postal and Hangfire in Subsite](https://stackoverflow.com/questions/35389505/using-postal-and-hangfire-in-subsite) ? – Serg csharp .Net Developer Apr 27 '18 at 07:55

3 Answers3

1

This problem is probably caused because HTTPContext is not available to Postal when using Hangfire. If HTTPContext.Current is null, Postal uses http://localhost as the URL, which doesn't map to your application location. In order to get around this problem, you can use FileSystemRazorViewEngine when sending the email and pass it the path of your email template.

See the answer to this question for details on how to achieve this. Using Postal and Hangfire in Subsite

JodyL
  • 184
  • 1
  • I will post the error with the solution in my answer. – GeorgeB Apr 27 '18 at 07:58
  • 1
    I see from your response that you are using StructureMap as your DI container. The error you are seeing is again most likely related to the fact that when Hangfire executes the task, HTTPContext is null. The CurrentNestedContainer property in StructureMapDependencyScope attempts to retrieve an item from the Items property of HTTPContext. You could modify the CurrentNestedContainer property to check for null before attempting to retrieve the item. – JodyL Apr 27 '18 at 09:15
  • This worked! I will post the code I have used in my question. – GeorgeB Apr 27 '18 at 09:34
0

Most probably the problem is related to IIS rather than Hangfire, etc. So, could you please try to using with dot (.) as shown below:

Instead of using

'~/Views/Emails'

try this

'./Views/Emails'

Similarly you might also encounter some problems when displaying images and in such case you should make the following changes:

~/../Content/images/image.png

to

./../Content/images/image.png
Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63
0

Please try to use MapPath as follows:

var path = HostingEnvironment.MapPath(@"~/Views/Emails");
Niladri
  • 5,832
  • 2
  • 23
  • 41