0

I have a asp.net mvc5 website, that gets some image files from a api and stores them in a image folder.

public async Task<bool> getPhoto64(UserTest user)
    {
        try
        {
            if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png"))))
                using (var client = new HttpClient())
                {
                    string baseUrl = ConfigurationManager.AppSettings["BaseUrl"];
                    client.BaseAddress = new Uri(baseUrl);
                    client.DefaultRequestHeaders.Accept.Clear();


                    HttpResponseMessage response = await client.GetAsync("user/image?userIid=" + user.IID);
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        Stream st = await response.Content.ReadAsStreamAsync();

                        byte[] bytes = new byte[(int)st.Length];
                        st.Read(bytes, 0, (int)st.Length);
                        if (bytes != null)
                        {

                            if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png"))))
                                File.WriteAllBytes(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png")), bytes);


                        }
                    }
                }
        }
        catch (Exception e)
        {
            logger.Error(e);
            return false;
        }
        return true;

    }

Right now im doing this when a user connects to the website, but i would like to know if there is a way for the website to automatically trigger this method , lets say once a day at 00:00.

Thought
  • 5,326
  • 7
  • 33
  • 69
  • 1
    I don't think this is about MVC, take a look [how to schedule a task in ASP.NET](http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx). – adricadar Oct 27 '15 at 08:45
  • 2
    Look at [similar questions](http://stackoverflow.com/questions/542804/best-way-to-run-scheduled-tasks) on SO, or Scott Hanselman had [a good article](http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx) about options for scheduled tasks in ASP.NET from last year. – Mr Moose Oct 27 '15 at 08:46
  • 1
    This is a job for a service application – Mladen Oršolić Oct 27 '15 at 08:47
  • 1
    Perform a Scheduling using window task scheduler can be managed using a console application that run once at midnight. – Suprabhat Biswal Oct 27 '15 at 08:47

1 Answers1

0

you can use "Windows Task Scheduler" for this purpose.

Ravi
  • 316
  • 7
  • 17