Configure web service and method to run on a periodic basis - c#

I would like to automate a web method inside a web service that should run on a periodic basis. I also need to pass some arguments to it at runtime.
Some details:
My development machine has Visual Studio 2012 Professional and IIS
Express installed.
The production server has IIS 7.
SQL Server Express 2008 as RDBMS.
The arguments should be taken from a table in
the local DB and are subject to change.
Two of the fields in the table (execution_periodic_basis and execution_day) are used to indicate when the web method is supposed to run, i.e., the day can be MONDAY through SUNDAY for WEEKLY runs, or 1 through 28 for MONTHLY runs.
What would be the best approach to accomplish this? If you, based on your experience, have any suggestions on a better way to perform this process, they will be greatly appreciated.
Any tips or links will be more than welcome.

I'd recommend using IIS7.5 AutoStart http://msdn.microsoft.com/en-us/library/ee677260(v=azure.10).aspx site which you then use to Poll the SQL Server database. This AutoStart app (the web equivalent of a Windows Service) can then call your designated URL when the schedule matches its log and the current time.

Sounds like you'll need to either set timers (so look ahead for the next checkup, and set them), or once every x amount of time, see if it needs to be done.
I'd probably use a background thread with a timer and sleep in between.
Having said that, I'm not sure this question will get much love, as it's not the best "fit wise".

Use a Windows Service. Web Services are not designed for this task!

Related

A way for a program to constantly run

I am writing a website that will be using an SQL database. However I am needing the database to be updated with real-time information. I have no problems doing this as I have written a C# program to do this. However as I want this data to be updated 24/7. It's not appropriate to run it on my own machine. Any suggestions would be greatly appreciated!
There are many options:
If you have a dedicated server:
Create your application as a console application and put it in startup
Create your application as a console application and using Windows Task Scheduling run it with your required frequency.
Create it as a windows service that will always run
If you don't have a dedicated server:
Use libraries like Quartz to perform task scheduling in your web application
Assuming that by saying SQL you mean Microsoft SQL Server...
As already told the optimal solution is to code a Windows service. In case you are working with express edition of visual studio this can be tricky without the related template which is available only on paid edition or with 2017 community , so a possibility is to make an executable program and activate it using task scheduler and the repeat every option.
To be honest I would prefer to keep everything inside SQL , using SQL server agent you can execute jobs defined among SSIS , basically a rebranded and extended DTS which can easily operate on DB, have a better logging than the primitive OS task scheduler. No need to go on was or cloud, it is just a SQL server function that need to be enabled by the DBA, if not already used.

Deploy a backend application on a Windows Server

I'm making an application in C# with VS 2012 that checks a database every 15 seconds and perform some actions when it finds data. Right now I've created a Console Application so I can debug it easely but during relese this application needs to run in a IIS server.
How can I do that? I've read this question but it looks like some sort of workaround because to run it I need to perform these steps. Right now I'm reading the docs about Windows Service Application, Is this the right way?
EDIT Sorry but I've never used Windows server before, so as people pointed out IIS is only a web server, the thing I need to do is run my application in a Windows Server environment
IIS is a web-server and accordingly it should be used for hosting web applications.
Develop a windows service which does the job of checking the database in intervals and invoke a web service (which you can host in IIS)
If your application is performing some data query and manipulation on the server then I would recommend the approach to host it in a windows service.
Some advantages to this are:
The service will start and run independently of a user logging into the server.
You can configure the service to recover should it experience an exception (ideally not!).
The service will start automatically (if configured) when the server restarts.
You can configure which user group (or user) the service should run under so you have a more granual approach to security.
As it's running as a seperate process, you can monitor its memory and processor utilisation.
Debugging is slightly more cumbersome but not difficult, one approach I've used is to install the service locally, start it and then attach to it via the debugger. This question describes another approach I've also used.

Run an application from the IIS application pool

I have a console application which basically sends emails once per day.
The Windows server administrator disallows this technique and doesn't want to allow extra software on the computer (launched by a scheduled task or a service).
I've been asked to evaluate the possibility of redeveloping a part of the application and integrate it into the IIS application pool but I don't think IIS can do this. Is it possible ? If so, how ?
The only approach I've looked at so far is to redevelop it as a web application and launch a web page everyday with a scheduled task, but I'd like to avoid that.
Let's analyze your options:
Use task scheduler in your server to launch console app
Use task schedule**r in your server to **web service hosted in IIS
Have an IIS application running 100% of time in an infinite loop that that checks time every minute and if it happens to be the correct time send the emails
Have a windows service.
Use task scheduler in a different server to invoke
Analyzing each one of them:
KO: Your administrator does not want console apps and process is not isolated.
KO: You have process isolated but still you are installing a console app.
OK: Not very good for performance but your fulfills your admin conditions.
KO: Your admin does not want windows services.
??: Probably your admin will not want to use an extra server
Proposed solution: As you can see only options 3 and 5 might pass the filter. Ask him
Correct solution I did similar things in the past and went for option 2. You have to convince your admin that solution 3 is a bad idea and probably 5 is already out of the question. When he had to choose the lesser of the evils option 2 is the best :-)
NOTE: You don't mention it but in case you have a SQL Server or similar you can store there an scheduled task too...
I had similar questions when I was moving from Apache servers (where it's dead easy to send a nightly email) to Windows (where you have options).
Clients have sometimes pushed me towards SQL Mail. It's not terrible. If your web app has a SQL backend, and it's okay to schedule things there, it's capable of sending emails when properly configured.
I don't think this is possible. With an IIS application you'd need something to trigger loading the application (call the web page). This itself would require a scheduled task.
You need to pound some sense into your administrator. Sorry.

Best approach to execute stored procedures from within CRM using a Custom Button

I am looking for some advice. We have multiple systems that sync from Microsoft Dynamics 4 (CRM) on a nightly basis. However I am often asked to manually execute the SP during the day as the sync is sometimes required immediately. This SP is on a different server to CRM.
I am wanting to add a custom button to CRM that allows the user to sync CRM to other systems themselves if they need it immediately. I do not want this to be a Workflow that occurs every time a record is updated, it will be the a user decision.
I have done some Googling and I'm at a crossroads as to how best to approach this.. 1) Executing a service from the button's javascript or 2) Using a Workflow to execute an SSIS and calling this from the button.
If I use Option 1, what would be the best approach to creating the web service? Baring in mind that the Sync procedure can take a fair few minutes to run depending on internet speed between sites, I have to be careful of timeouts. If I use a .Net 3.5 asmx web service, is there any way to immediately respond, and run the SP in the background? I know I can call this from Javascript. I believe the alternative is to use a WCF Workflow Service (which is completely new to me) which will allow for background processing - but can this be called from Javascript?
If I use Option 2, how do I execute a local SSIS from CRM? I've read it's possible, but I was unable to find an example.
Sorry for the wall of text. Any guidance or recommendations on approach would be greatly appreciated. Many Thanks.
TL;DR: I am looking for some advice on how to execute stored procedures on a different database in Dynamics 4 using a Custom Button.
You would have to create a web service. It can be a WCF service or classic ASMX service. Deploying it on the CRM server will be slightly complex but it can be done (deploy the application within ISV folder and then modify your application's web.config file so that it restores all the custom stuff CRM sets in its own configuration). If you have a separate web server (or at least a separate web site on the CRM server) it will be much easier.
The service has to be marked as OneWay - so that it returns to the caller as soon as it is invoked. Unfortunately it also means that you won't be able to return error messages - those will have to be written in a log.
Now create a button in the CRM form and call the service using JavaScript. You should be able to configure your service so that it can be invoked by passing any arguments in the URL parameters.

Is there a way to have code run on a time schedule in an ASP.NET web app?

I have an ASP.NET web app providing access to a database on the server. What I need is a way to run code in the background on a given schedule that auto-updates the server database from another source.
I know how to do this in a windows app by adding a timer, linking up a function to the timer tick event and starting the timer. I do not know how to do this in a web app.
Is there a start-up event for a web app or somewhere where I can start this background process regardless of whatever any users are doing on the site?
You should not do this in an ASP.NET website - this is a major no-no. You are correct in thinking to use a timer on a background .exe. You should look into creating either a Windows Task (a console .exe executed by the server task timer), or a Windows Service. I would suggest the Windows Service as that is standard practice.
If you have access to the computer hosting your site I would write a little app that was run from the Task Scheduler.
The web server is not meant to handle long-running background tasks. It's the wrong tool.
If you dont have access to the hosting computer then I would suggest building some kind of interface whereby another computer rebuilt the database and uploaded it. I'm using the terms "interface" and "upload" in the loosest, broadest sense - apply your own definition.
I was searching for a solution myself couple of months ago, and even though I haven't found enough time to try it so far, I guess I can share the link with you. Maybe you'll find it helpful.
If yes, please, let me know here.
http://quartznet.sourceforge.net/
How to use Quartz.net with ASP.NET
you can use Windows Service or use Timer Control (In the Ajax Category)
Or
As other answers have stated, doing this full function - updating a database and scheduling it as an ASP.NET app is using the wrong tool for the job.
ASP.NET can be used to update a database - that's perfectly valid. Where it breaks down is in the timer. ASP.NET apps aren't meant to be long-running, which is necessary for the timer to work.
If you can do it, I'd strongly suggest using the approach others have suggested - a Windows Service or a Scheduled Task.
However, if you have no access to the actual server, other than to post ASP.NET code - you can't install a service and you can't set up a Windows app to run on a scheduled basis, here's an out-of-the box idea.
Set up a web service or ASPX page that does the update, and then call that page from a scheduled task on a machine you DO control.
So if this was at http://www.someserver.net/updatedb.aspx, there's no reason you can't set a scheduled task on your own PC to call that URL.
I'd consider this a last-ditch solution to be used only if you can't do one of the other options.
The global.asax.cs file has a method that is fired when your application starts: Application_Start. You can hook up your timer method in that event. Just beware, depending on how IIS configured, your app pool may shutdown. For example, if no one hits the site in 20 minutes for example. Just make sure if you HAVE to have this run every X minutes that you have IIS configured to ALWAYS be running and start your app. This is harder than it sounds. In the end, you may want to go with a regular windows scheduled task.

Categories