简体   繁体   中英

Physical Application Path in MVC3 C# for Scheduled task

I'm working on e-commerce site which is based on ASP.NET MVC3 C#. I've implemented schedule task using System.Threading which is run in every min. 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. When I'm trying to get this path using HttpContext.Request.PhysicalApplicationPath,it throw an error like this

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. 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. The new thread has no idea about HttpContext or where it came from.

You are probably going to need to use 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. Really depends on how your code is setup to spawn the background thread and call the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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