简体   繁体   English

MVC3 C#中的物理应用程序路径用于计划任务

[英]Physical Application Path in MVC3 C# for Scheduled task

I'm working on e-commerce site which is based on ASP.NET MVC3 C#. 我正在开发基于ASP.NET MVC3 C#的电子商务网站。 I've implemented schedule task using System.Threading which is run in every min. 我使用System.Threading实现了计划任务,该计划每隔一分钟运行一次。 This schedule task calls simple function and it returns product and insert into in another table for some requirement. 此计划任务调用简单函数,它返回产品并插入另一个表中以满足某些要求。

My Problem is: 我的问题是:
There is one field say ProductImageURL and all images are stored in content/thumbs. 有一个字段说ProductImageURL,所有图像都存储在内容/拇指中。 When I'm trying to get this path using HttpContext.Request.PhysicalApplicationPath,it throw an error like this 当我试图使用HttpContext.Request.PhysicalApplicationPath获取此路径时,它会抛出这样的错误

The method or operation is not implemented. at System.Web.HttpRequestBase.get_PhysicalApplicationPath()

This error is occurred because there is a function of scheduler task is requesting product image via http but originating request is non http... just simple function call. 发生此错误是因为调度程序任务的功能是通过http请求产品图像但是原始请求是非http ...只是简单的函数调用。 If we manual type physical path then the function seems to work correctly. 如果我们手动键入物理路径,那么该函数似乎正常工作。

So How can I solve it? 那么我该如何解决呢? or Is there any other way to address this kind of issue? 或者还有其他方法可以解决这类问题吗?

This is not working because you are dispatching another thread to process the task. 这不起作用,因为您正在调度另一个线程来处理该任务。 At that point, you no longer have access to the HttpContext....because you are on another thread. 此时,您无法再访问HttpContext ....因为您在另一个线程上。 The new thread has no idea about HttpContext or where it came from. 新线程不知道HttpContext或它来自何处。

You are probably going to need to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 您可能需要使用Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); or some equivalent to get the path to your application. 或一些等价物来获取您的应用程序的路径。

Another option would be to pass in the current directory using HttpContext.Request.PhysicalApplicationPath as a string parameter to your function. 另一种选择是使用HttpContext.Request.PhysicalApplicationPath作为函数的字符串参数传入当前目录。 Really depends on how your code is setup to spawn the background thread and call the function. 实际上取决于您的代码如何设置以生成后台线程并调用该函数。

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

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