C# MVC Run controller code weekly - c#

I have a controller that runs and gets data from a database and sends an email through to the client.
This email setting gets set in a form that is saved to a table with the email recipients. I need to run this on a schedule, which the user (client) defines in a form. Here the client can select either weekly, biweekly or monthly.
From here and after this is saved in the database, it is easy enough to pull from the database, but I need a way to run this every time at the time specified by the client (weekly, biweekly, or monthly). I was thinking of using a webjob (Azure) to do this but I am not sure how to approach this.
So
Are there any issues with this approach?
What are some of the other options?
Any recommendations?

Have you taken a look at solutions that are designed for this? Why re-invent the wheel?
Take a look at Hangfire.io
You can then just do the following:
BackgroundJob.Enqueue(BatchClient.Start());
Or you can add a Recurring job:
RecurringJob.AddOrUpdate(() => BatchClient.Start(), Cron.Daily);
There are also other options also worth investigating e.g. Quartz.Net

I don know if this is the best approach out there, but we use this in our company projects (exactly for sending scheduled emails):
We use Cron Expression to define frequency of an action that needs to be perfomed;
We use Quartz.NET to get the next date when this action should be performed;
For the checking the dates and running actions we are using a WCF service, running in a Windows Service on its own thread.
It might be to complex for your need, but it is pretty flexible. You have to decide where and how you store cron expressions and next dates. For the Cron Expression we use the DB next dates we keep in memory.

Related

Is it possible to write a Windows service using .NET that will trigger a function if any changes have occured in my local Access DB

I have local Access database on my computer. The database is used by another application. I want to write a windows service using .net to send an sms when changes have occurred in the local Access database.
Not sure how complex the application is and how often data changes, but You could use FileSystemWatcher to detect the moment when Access Data file has changed, then use some simple logic to check if table of interest is updated, for example look at row count in simplest case.
there is no efficient way for the service to know if the database changed and know which are the changes, why not write a method in the other app to send the sms?
i'm assuming that the other application is developed by you, if that not the case, please give more context to your question so we can help you.
Another possible solution is to use Quartz.net to schedule a job that executes a job every n minutes and runs a query against the database to evaluate if records changed in specific table(s).
I have used TopShelf and Quartz.net to build similar solutions and it works reliably. Here is a good tutorial to get you started: https://prashantbrall.wordpress.com/tag/topshelf/

C# Best pattern for actions triggered by dates

I've written applications to handle this situation dozens of times over the years. I just want to know if there's a better way.
Here are some examples:
A table contains an expiration date. After that date, I need to make a notification of the expiration (email, messenger, etc).
Someone has not logged in for X days. Need to send a notification.
The way I currently handle it:
Create a table of sent notifications.
Create a view in the database of all expirations/late logins with no record of a notification already sent.
Write a Windows Service to scan the view every X minutes and send the notifications, write them out to the "Sent Notifications" table (which will make it disappear from the view).
Is there any better way of accomplishing this task? I only ask because sometimes some new technology passes me by.
Thanks in advance
I think you could find a possible solution via SSIS (Sql Server Integration Services). You can create a package to monitor the porcess and to send email to those people who need to be informed.

Update External Systems from Dynamics CRM 2011

a bit new to Dynamics CRM and trying to find the best way to update external systems in real time when specific entities are created or updated in Dynamics CRM 4.0 or 2011. What I've gathered thus far is that my best option is to
Write a web service for the downstream LOB system.
Write a custom workflow activity that calls the web service and register the plugin inside CRM.
Attach the custom workflow activity to a record created / updated trigger on the underlying entity that I want to receive updates for.
Am I on the right track or is there a better alternative?
What you have seems a pretty sound design and is probably the way I would approach it.
If you need synchronous behaviour (not sure how real time your real time is here) you might want to consider plugins (different from custom workflow activities) as these can be registered to run synchronously.
Other than that you could have your downstream system read the records from CRM on a frequent, scheduled basis - you can use the modifiedon field to see which records have changed since the last read.

Schedule a Microsoft Dynamics CRM 4.0 Workflow

I am in the process of puting together a custom workflow activity to be used in Microsoft Dynamics CRM 4.0
What I would like to ultimatly acheive is configure a workflow that runs on a schedueled basis i.e run every 2 hours Monday to Friday, rather than on a particular "CRM event" like create, delete status change ect.
Does anyone have any ideas?
Maybe schedule it outside of crm?
Edit 1:
What we are doing is processing rows in a staging table that gets generated from a front-end site. We are creating contact/account and opportunity records in CRM based on the data captured from the front-end.
The more I think about it the more I'm thinking that using workflow is possibly not the best solution?
What about a using a windows service?
Workflow was not best option for this situation due to the following:
Can't schedule it to run
The process can only be triggered by a CRM create, update or similar message
I went with a combination of the following:
A SQL CLR sproc that gets called on an UPDATE trigger on the staging table. The CLR sproc calls a webservice that generates CRM contacts/accounts. That way the front-end site can create records and set a "ready to process" flag once all data has been entered.
The requirement changed from a schedule solution to real time processing (well not ACTUALLY real time). The process needs to run as records are entered from the front end site.
Hope all that makes sense!
A windows workflow using the CRM webservices is one option, a better option would be to change your webform to access the CRM webservce adn enter the data directly.
if you really want to use workflows you can download a tool from http://www.patrickverbeeten.com/pages/TechnicalNet/MicrosoftCrm/PeriodicWorkflows.aspx?id=23 that you install on your CRM server and it allows you to use windows sheduled tasks to triger them.

Update C# client whenever database is updated

I am using MySQL with C# / Visual Studio 2008. I need to know any method so that if one user updates the database, all the logged in users over the network, are notified that a change has been occurred.
If i logged in the database through my application, at that time the serial no of the latest record in the database was 10, and then i someone with the privileges updates the record in the database and at the same time i am being notified that the database has been updated with a new record.
Common example can be of MSN messenger's alert for a friend when he gets online...
Though i really don't want that popup, but alert functionality is something like that...
One more thing to ask:
I want a functionality like if a record's status is not updated (for say about more than 3 hours), then i want all users to get alert about that issue also. This functionality is needed for all of the records, not specifically for new records.
Quite a long answer, but your best bet would be something like SqlCacheDependency.
Also you could use something called the "Reverse AJAX" technique, which is used by Google Mail notifications, and talked about here: How does incoming mail notification on Gmail works?
Besides those two, your own other options AFAIK is simple server polling.
Also, you haven't mentioned what your client is.
Is it a public web site on the internet? Is it an intranet app, it is a WPF app?
If it's a web site, you're best best is client-side callbacks using the Reverse AJAX technique i mentioned.
You probably need to design some kind of poll functionality in your client. For example, you can send a request each minute asking the database "Have you updated anything since [date the client latest got updates]?" If the database answers true, you let the client request the full set of updates. If not, you let it sleep another minute.
Using the SqlDependency Class is a better way to make all your data driven more faster and efficient. It just remove the need of constantly re-query your database checking every time when a changes is made in the data.

Categories