locked
why Quartz.net can not read this line of code? based on the example from: www.mikesdotnetting.com RRS feed

  • Question

  • User-1651604128 posted

    Hi, I am following this tutorial to implement Quartz.net in my asp.net mvc web app,

    http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net

    The following code can be executed by a button action, but after I made the schedule , it has run time error here in debug.

    using (var sr = new StreamReader(Server.MapPath(@"\\App_Data\\Templates\test1.doc"))

    with this error message of "Object reference not set to an instance of an object."

    I found that "Server" returns null, does anybody know why that line of code is different when I execute manually and schedulelly?

    Thursday, February 26, 2015 11:31 PM

Answers

  • User-1651604128 posted

    You should try 

    using (var sr = new StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/Templates/test.doc")))

    Finally, I found that this code is working.

    System.Web.Hosting.HostingEnvironment

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 27, 2015 3:41 PM

All replies

  • User-821857111 posted

    You should try 

    using (var sr = new StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/Templates/test.doc")))


    Friday, February 27, 2015 2:19 AM
  • User-1651604128 posted

    You should try 

    using (var sr = new StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/Templates/test.doc")))

    Thanks Mike, I forgot mention that I was running the codes in my local machine, not on server. I will try your solution and let you know the result.

    So if I want the c# class run manually or schedule by Quartz.net, I have to prepare two sets of codes for some statements and identity if the c# is running in manually or through Quartz.net?

    This issue can easy fix, by my another question is: I saw most people mentioned the issue "Application pool" for Quartz.net, for my case, my asp.net mvc web app is running as a tool, there are no user to visit it, so after the application is started, the quartz.net will be triggered immediately through your example codes, and then it will be running every 5 mins, basically, it will send email notice through certain condition (query results) from a DB2 database. so  as you mentioned in your website, after the website is running 20 mins, since the server pool is idle, (nobody visit it), then the quartz.net will not working any more? how to resolve this issue? I may not use it if this issue can not be resolved. Somebody mentioned to add some condition in IIS, no sure if there is any solution? any idea?

    Friday, February 27, 2015 7:27 AM
  • User-760709272 posted

    So if I want the c# class run manually or schedule by Quartz.net, I have to prepare two sets of codes for some statements and identity if the c# is running in manually or through Quartz.net?

    No, just use System.Web.HttpContext.Current.Server - it works everywhere.

    This issue can easy fix, by my another question is: I saw most people mentioned the issue "Application pool" for Quartz.net, for my case, my asp.net mvc web app is running as a tool, there are no user to visit it, so after the application is started, the quartz.net will be triggered immediately through your example codes, and then it will be running every 5 mins, basically, it will send email notice through certain condition (query results) from a DB2 database. so  as you mentioned in your website, after the website is running 20 mins, since the server pool is idle, (nobody visit it), then the quartz.net will not working any more? how to resolve this issue? I may not use it if this issue can not be resolved. Somebody mentioned to add some condition in IIS, no sure if there is any solution? any idea?

    http://forums.asp.net/post/5825786.aspx

    Friday, February 27, 2015 7:34 AM
  • User-1651604128 posted

    You should try 

    using (var sr = new StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/Templates/test.doc")))

    Hi Mike, I have just tried it, the HttpContext.Current.Server did not compile so I used

    System.Web.HttpContext.Current.Server, but I got the same error, and in debug, the System.Web.HttpContext.Current.Server returns null.

    By the way, I am running my mvc in my local macine, not on production server.

    any idea how to fix it? thanks

    Friday, February 27, 2015 8:24 AM
  • User-1651604128 posted

    Peter Cong

    So if I want the c# class run manually or schedule by Quartz.net, I have to prepare two sets of codes for some statements and identity if the c# is running in manually or through Quartz.net?

    No, just use System.Web.HttpContext.Current.Server - it works everywhere.

    Peter Cong

    This issue can easy fix, by my another question is: I saw most people mentioned the issue "Application pool" for Quartz.net, for my case, my asp.net mvc web app is running as a tool, there are no user to visit it, so after the application is started, the quartz.net will be triggered immediately through your example codes, and then it will be running every 5 mins, basically, it will send email notice through certain condition (query results) from a DB2 database. so  as you mentioned in your website, after the website is running 20 mins, since the server pool is idle, (nobody visit it), then the quartz.net will not working any more? how to resolve this issue? I may not use it if this issue can not be resolved. Somebody mentioned to add some condition in IIS, no sure if there is any solution? any idea?

    http://forums.asp.net/post/5825786.aspx

    Hi AidyF, I have just tried it, , but I got the same error, and in debug, the System.Web.HttpContext.Current.Server returns null.

    By the way, I am running my mvc in my local macine, not on production server.

    any idea how to fix it? thanks

    Friday, February 27, 2015 8:26 AM
  • User-760709272 posted

    What do you mean by "running on your local machine"?  Is this code in a website in your local IIS?  Or is it a console or windows app?

    Friday, February 27, 2015 8:40 AM
  • User-1651604128 posted

    What do you mean by "running on your local machine"?  Is this code in a website in your local IIS?  Or is it a console or windows app?

    This mvc web app is in developing stage, of course ,it is developing in my local machine and testing in local machine, it is a mvc web application,

    it will be implemented to production web server if everything is working well in my local machine. I think every company is doing the same thing as mine.

    Friday, February 27, 2015 10:54 AM
  • User-1651604128 posted

    You should try 

    using (var sr = new StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/Templates/test.doc")))

    Finally, I found that this code is working.

    System.Web.Hosting.HostingEnvironment

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 27, 2015 3:41 PM