简体   繁体   English

为什么当前的ASP.NET MVC应用程序根虚拟路径在另一个线程/任务中不可用?

[英]Why current ASP.NET MVC application root virtual path is not available in a another thread/Task?

One of my recent projects involved sending reminder e-mails based on some reminder configuration. 我最近的项目之一涉及根据一些提醒配置发送提醒电子邮件。 E-mail sending is implemented in an asynchronous job using Quartz.net. 电子邮件发送是使用Quartz.net在异步作业中实现的。 and it requires to include a permalink to a entity in the application. 并且它需要在应用程序中包括一个到实体的永久链接。

However, in order to get this permalink, I have to be able to compute the full URL, based on the identifier stored in the queue used for the job. 但是,为了获得此永久链接,我必须能够基于存储在作业队列中的标识符来计算完整的URL。 The answer is not straightforward, since HttpContext is not available in the thread's context. 答案并不简单,因为在线程上下文中HttpContext不可用。

One solution is to store application root path in the queue and use it from there. 一种解决方案是将应用程序根路径存储在队列中,然后从那里使用它。 Another way is to use a function like the following: 另一种方法是使用如下函数:

public static String GetCurrentBasePath(String prefix = "http://")
{
   return String.Format("{0}{1}{2}", 
      prefix,                                           // protocol
      System.Net.Dns.GetHostEntry("").HostName,         // host
      System.Web.HttpRuntime.AppDomainAppVirtualPath    // virtual application path
   );
}

However, this has (severe) limitations, since the protocol must be provided and also returns the hostname , not the domain name , thus rendering it useless when having multiple Web applications bound to the same host. 但是,这样做有(严重)局限性,因为必须提供协议,并且还返回hostname ,而不是domain name ,因此,当将多个Web应用程序绑定到同一主机时,它就变得无用。

The question: is Web application base path available in another thread/task? 问题:Web应用程序基本路径在另一个线程/任务中可用吗? . I think the other thread/task context is somehow connected to the ApplicationPool , rather than the WebApp and since multiple WebApp s can use the same ApplicationPool , there is not a direct connection between thread context and the WebApp . 我认为其他线程/任务上下文以某种方式连接到ApplicationPool而不是WebApp并且由于多个WebApp可以使用同一ApplicationPool ,因此线程上下文和WebApp之间没有直接连接。

I'm not sure how you're invoking this. 我不确定您如何调用此功能。 But from my experience, parallelism (as you've mentioned) loses the HttpContext. 但是根据我的经验,并行性(如您所提到的)失去了HttpContext。 That said, there is nothing to stop you from using a config variable with the string value(s) that you need. 就是说,没有什么可以阻止您使用具有所需字符串值的config变量。 Of course, depending how dynamic this value is this might not be the best course of action. 当然,取决于此值的动态程度,这可能不是最佳的做法。 But remember that the computation you're making above is expensive, so remember to store the value locally. 但是请记住,您在上面进行的计算非常昂贵,因此请记住将值存储在本地。

I believe the best solution here (assuming you know the context values, which I don't see why you couldn't/wouldn't) would be to set some variables and avoid the computation altogether. 我相信这里最好的解决方案(假设您知道上下文的值,我不知道为什么不能/不会)将设置一些变量并完全避免计算。

static readonly string hostName = "your-host";
static readonly string virtualPath = "your-virtual-path";
public static String GetCurrentBasePath(String prefix = "http://")
{
   return String.Format("{0}{1}{2}", 
      prefix,                                           // protocol
      hostName,         // host
      virtualPath    // virtual application path
   );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM