3

I wanted to checking my database table for periodically.So how can i create a webservice and how can i configure it.

sejal patel
  • 262
  • 2
  • 4
  • 12

3 Answers3

2

basically what you need is, something which is always running and hence can make periodic calls.

There are a number of ways to do it

  1. (Since ASP.NET hence) You can make a Windows Service, and host this service on your server, since server is always running, this Windows Service will make request to your webservice, update database or watever you want

  2. You can use SQL Jobs to do it. You can call a webservice from a job, through a SSIS (Sql Server Integration Service) Package. These packages are very very robust in nature, they can do almost any db activity that you want them to do, including webservice request.

  3. And finally, you can use third party tools such as Quartz.Net

References:

  1. this is how you can call a webservice through a windows service.
  2. this is how you can call a webservice through a ssis package.
  3. this is how you can integrate a SSIS package in a SQL Job
  4. this is how you can create a windows service
  5. this is how you can create a SSIS package
  6. this is how you can get answer/tutorial of almost anything

Example:

simplest of all of these would be a Windows Service. Making a windows service and hosting it on the machine(server) is very easy, use one of the given links (specially the last link). Usually, in Windows Service, you do some activity in OnStart event. you can place a timer inside this OnStart and upon TimerTick(), you can request your webservice.

something like this:

class Program : ServiceBase
    {
        System.Timers.Timer timer;

        static void Main(string[] args)
        {
            ServiceBase.Run(new Program());
        }
        public Program()
        {
            this.ServiceName = "My Service";
        }
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            InitializeTimer();

        }

        protected override void OnStop()
        {
            base.OnStop();

            //TODO: clean up any variables and stop any threads
        }

        protected void InitializeTimer()
        {
            try
            {
                if (timer == null)
                {
                    timer = new System.Timers.Timer();
                    timer.Enabled = true;
                    timer.AutoReset = true;
                    timer.Interval = 60000 * 1;
                    timer.Enabled = true;
                    timer.Elapsed += timer_Elapsed;
                }

            }
            catch (Exception ex)
            {
                Utility.WriteLog("Exception InitialiseTimer : " + ex.Message.ToString());
            }
            finally
            {
            }
        }

        protected void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
        {
                        
            TimerTick();
            timer.Interval = 60000 * Convert.ToDouble(ConfigurationManager.AppSettings["TimerInerval"]);
        }

        private void TimerTick()
        {
            try
            {
                DownloadFromFTPandValidate objDownLoadandValidate = new DownloadFromFTPandValidate();
                objDownLoadandValidate.ProcessMain();
            }
            catch (Exception ex)
            {
                Utility.WriteLog("Exception InitialiseTimer : " + ex.Message.ToString());
            }
        }
    }

Here, class DownloadFromFTPandValidate wraps the code to db activity. It shd give you an idea.

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
  • i want to checked just if particular user is registerd or not but this thing is should checking for some period of time.. This thing how should i do? – sejal patel Jan 31 '13 at 07:12
  • since they are always running, and they have to always run in order for a periodic execution. always running doesn't mean always executing. you can always configure your service to execute a particular set of code after fixed interval, same goes for jobs – Manish Mishra Jan 31 '13 at 07:13
  • ssis packages are meant to do this. to run at a certain interval i.e. every night at 11pm or on a certain period i.e. after every 2hours. In one of my projects, I've 15+ SSIS packages, doing all shitfull works i.e. google api, weather service, db update, email parsing and wat not – Manish Mishra Jan 31 '13 at 07:15
  • I also want to run or call a one method which checking a every day 1 time for user already exist or not.. – sejal patel Jan 31 '13 at 07:32
  • watever sejal. any plain activity that you can do manually(through a button click) through asp.net, you can do from them. see the example i've posted, its c#. you can write ur logic to check user exists or not. go on, dig more abt them – Manish Mishra Jan 31 '13 at 07:34
  • Thanks for such a kind help. – sejal patel Jan 31 '13 at 08:33
  • how can i run this windows service? can u tell me? – sejal patel Feb 01 '13 at 05:12
  • goto Visual Studio, click on New, and choose windows on the left and then, look for Windows Service template in the right window. choose tht. its simple to configure it. you can deploy a windows service, in a most simple way, by using a setup project. http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.100).aspx and http://support.microsoft.com/kb/317421 . – Manish Mishra Feb 01 '13 at 06:48
  • u mean i have to create a exe? i can not directly run like as a website one of page we run simply? – sejal patel Feb 01 '13 at 09:02
  • :).yes. tht's the whole point of creating windows service or ssis. you want it to get scheduled. you cannot do it through a website. code has to be at some location, where it can be scheduled, one such location is windows service. we are again at the beginning of this entire discussion. – Manish Mishra Feb 01 '13 at 09:09
  • yes becoz i cn not understand how to do this so.. i asking u a lots of que.. :( – sejal patel Feb 01 '13 at 09:12
  • now i create a one webservice in which one method and i called that method into window service.. now i want to check that its working or not.. i directly run but its not run and give me error – sejal patel Feb 01 '13 at 09:16
  • this is a new prlbm altogether. try finding tutorials or questions on running web services inside a windows service. you should begin with the fact "It is possible", and let's close this discussion. – Manish Mishra Feb 01 '13 at 09:18
0

You will need a job scheduler for periodical task. I recommend you a good one. Check out this link: http://quartznet.sourceforge.net/

phnkha
  • 7,782
  • 2
  • 24
  • 31
0

Why not using a trigger on your table which runs a stored procedure once data was modified, then use the xp_cmdshell to access the commandline form your stored procedure so you can run for example a batch file or whatever.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130