How can i create a webservice which is checking table for some periodically in database in asp.net? - asp.net

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

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
(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
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.
And finally, you can use third party tools such as Quartz.Net
References:
this is how you can call a webservice through a windows service.
this is how you can call a webservice through a ssis package.
this is how you can integrate a SSIS package in a SQL Job
this is how you can create a windows service
this is how you can create a SSIS package
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.

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

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.

Related

Asterisk SendURL() using grandstream UCM6204

We need to invoke URL contains the caller number each time there is an inbound call, we are using Grandstream UCM6204 PBX.
There is a function called SendURL() that can be used through Asterisk, I connected to Asterisk using PuTTy but I'm not sure what to do next?
After doing some research, I found that I couldn't have direct access to Asterisk through UCM devices, which will not allow us to modify the dial plan.
Is the only approach by creating a third party application to communicate with AMI and do the job? Problem with this scenario that this application should be online and working 24/7.
I wrote C# code to accomplish this, but it is not working as expected.
public FormMain()
{
InitializeComponent();
AstCon = new ManagerConnection(ConfigurationManager.AppSettings["astHost"],
int.Parse(ConfigurationManager.AppSettings["astPort"]),
ConfigurationManager.AppSettings["astUser"], ConfigurationManager.AppSettings["astPass"]);
AstCon.NewState += astCon_NewState;
AstCon.ConnectionState += astCon_ConnectionState;
try
{
AstCon.Login();
}
catch (Exception ex)
{
MessageBox.Show(
$"Error connecting to {ConfigurationManager.AppSettings["astHost"]}. Error: {ex.Message}");
// Terminate Application
Application.Exit();
}
}
private void astCon_NewState(object sender, NewStateEvent e)
{
if (e.ChannelStateDesc.ToLower() == "ring" && e.Connectedlinenum == "6401")
{
if (ConfigurationManager.AppSettings["ScreenPopPath"] != null)
{
var runString = ConfigurationManager.AppSettings["ScreenPopPath"];
runString = runString.Replace("{callerid-number}", e.CallerId);
Process.Start(runString);
}
}
}
void astCon_ConnectionState(object sender, ConnectionStateEvent e)
{
connectionStateLbl.Text = AstCon.IsConnected() ? $"Connected to: {AstCon.Username}#{AstCon.Hostname}" : $"Disconnected, reconnecting to {AstCon.Hostname}...";
}
}
You can use CURL function. But for invoke it need understand PBX dialplan and hooks
You can use external AMI app.
You can read CEL events in database.

Quartz.Net ASP.NET Status update

I'm using quartz in an asp.net site and I'm using it to fire off a longer background task. I want to be able to use a timer or jquery to call the job and get back status and progress info similar to how I was doing it before. Here is code that I was using:
protected void Timer1_Tick(object sender, EventArgs e)
{
if (MyTaskManager.Instance.IsTaskRunning)
{
if (MyTaskManager.Instance.TotalItems > 0) ProgressLabel.Text = string.Format(ProgressLabel.Text, MyTaskManager.Instance.TotalItems, MyTaskManager.Instance.ItemsProcessed);
else ProgressLabel.Text = string.Format("Records Processed: {0}", MyTaskManager.Instance.ItemsProcessed);
}
else
{
Timer1.Enabled = false;
}
}
Has anyone done this that could point me in the right direction?
It might not be what you're looking for but I've used SignalR with Quartz.Net and it works great.
I've published a simple application in my repository.
You have to create a custom Hub which you will use to interact with your ASP.NET page.
Your (quartz.net) job will interact your ASP.NET page through your Hub the same way.
Once you have installed ASP.NET SignalR:
Install-Package Microsoft.AspNet.SignalR
You can create a startup class to configure SignalR:
[assembly: OwinStartup(typeof(AspNetQuartSignalR.Startup))]
namespace AspNetQuartSignalR
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
You have to reference a couple of scripts in your page:
jquery-2.1.1.js
jquery.signalR-2.1.1.js
and the automatically generated /signalr/hubs
Now you can create your own Hub:
public class QuartzHub : Hub
{
...
}
with methods which will allow you to interact with the scripts in your ASP.NET page.
Let's say your Hub has a method CheckQuartzStatus which gives you the status of all the quartz.net triggers configured:
public void CheckQuartzStatus()
{
string message = string.Empty;
var allTriggerKeys = Global.Scheduler.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup());
foreach (var triggerKey in allTriggerKeys)
{
ITrigger trigger = Global.Scheduler.GetTrigger(triggerKey);
message += string.Format("{0} = {1}", trigger.Key, Global.Scheduler.GetTriggerState(trigger.Key)) + Environment.NewLine;
}
Clients.All.onCheckQuartzStatus(message);
}
Your jQuery script can interact with this method in a very simple way:
quartz.server.checkQuartzStatus();
As you can see your Hub method at some point fires an action: onCheckQuartzStatus.
That is a call to an event defined in your javascript defined in the page :
quartz.client.onCheckQuartzStatus = function (message) {
alert(message);
};
You can see how the interaction works looking at the script in the Default.aspx page.
You can read a lot more here.
You're going to have to build all of this functionality yourself. Quartz.Net jobs run in the threadpool and you don't have a way of referencing them from the scheduler. You could do this by having the job write its progress somewhere and then have your timer check that spot for progress.

Configuring Quartz.Net to stop a job from executing, if it is taking longer than specified time span

I am working on making a scheduler, just like Windows Scheduler using Quartz.Net.
In Windows Scheduler, there is an option to stop a task from running if it takes more than the specified time. I have to implement the same in my scheduler.
But I am not able to find any extension method/setting to configure Trigger or Job accordingly.
I request some inputs or suggestions for it.
You can write small code to set a custom timout running on another thread. Implement IInterruptableJob interface and make a call to its Interrupt() method from that thread when the job should be interrupted. You can modify the following sample code as per your need. Please make necessary checks/config inputs wherever required.
public class MyCustomJob : IInterruptableJob
{
private Thread runner;
public void Execute(IJobExecutionContext context)
{
int timeOutInMinutes = 20; //Read this from some config or db.
TimeSpan timeout = TimeSpan.FromMinutes(timeOutInMinutes);
//Run your job here.
//As your job needs to be interrupted, let us create a new task for that.
var task = new Task(() =>
{
Thread.Sleep(timeout);
Interrupt();
});
task.Start();
runner = new Thread(PerformScheduledWork);
runner.Start();
}
private void PerformScheduledWork()
{
//Do what you wish to do in the schedled task.
}
public void Interrupt()
{
try
{
runner.Abort();
}
catch (Exception)
{
//log it!
}
finally
{
//do what you wish to do as a clean up task.
}
}
}

Asp.net c#, Rollback or Commit after multiple process

I want to use Rollback() or commit() functions after multiple process.
There is no error, but it does not commit() to update DB.
Here is my example code,
public void startTransaction(){
using(Ads_A_Connection = new AdsConnection(Ads_A_connection_string))
using(Ads_B_Connection = new AdsConnection(Ads_B_connection_string))
{
Ads_A_Connection.Open();
Ads_B_Connection.Open();
AdsTransaction aTxn = Ads_A_Connection.BeginTransaction();
AdsTransaction bTxn = Ads_B_Connection.BeginTransaction();
try{
string aResult = this.process1(Ads_A_Connection);
this.process2(Ads_B_Connection, aResult);
this.process3(Ads_A_Connection. Ads_B_Connection);
aTxn.Commit();
bTxn.Commit();
// there is no error, but it couldn't commit.
}catch(Exception e){
aTxn.Rollback();
bTxn.Rollback();
}
}
}
public string process1(conn){
// Insert data
return result;
}
public void process2(conn. aResult){
// update
}
public void process3(aConn, bConn){
// delete
// update
}
I guess, its because out of using scope. because I tried to put all the code into
startTransaction() method, then it works. but it look too dirty.
How can I use rollback() or commit() after multiple (METHOD) process?
anybody know, please advice me.
Thanks!
[EDIT]
I just add TransactionScope before connection,
using (TransactionScope scope = new TransactionScope())
{
using(Ads_A_Connection = new AdsConnection(Ads_A_connection_string))
using(Ads_B_Connection = new AdsConnection(Ads_B_connection_string))
{
.
.
but it makes an error, it say "Error 5047: The transaction command was not in valid sequence."
I need a little more hint please :)
To extend what Etch mentioned, their are several issues with manually managing transactions on your connections:
You need to pass the SQL connection around your methods
Need to manually remember to commit or rollback when you are finished
If you have more than one connection to manage under a transaction, you should really use DTC or XA to enroll the transactions into a Distributed / 2 phase transaction.
TransactionScopes are supported with the Advantage Database Server, although you will need to enable the MSDTC service and possibly also enable XA compliance.
Note that I'm assuming that the advantage .NET client has some sort of connection pooling mechanism - this makes the cost of obtaining connections very lightweight.
Ultimately, this means that your code can be refactored to something like the following, which is easier to maintain:
private void Method1()
{
using(Ads_A_Connection = new AdsConnection(Ads_A_connection_string))
{
Ads_A_Connection.Open();
string aResult = this.process1(Ads_A_Connection);
} // Can logically 'close' the connection here, although it is actually now held by the transaction manager
}
private void Method2()
{
using(Ads_B_Connection = new AdsConnection(Ads_B_connection_string))
{
Ads_B_Connection.Open();
this.process2(Ads_B_Connection, aResult);
} // Can logically 'close' the connection here, although it is actually now held by the transaction manager
}
public void MyServiceWhichNeedToBeTransactional(){
using(TransactionScope ts = new TransactionScope()) { // NB : Watch isolation here. Recommend change to READ_COMMITTED
try{
Method1();
Method2();
ts.Complete();
}
catch(Exception e){
// Do Logging etc. No need to rollback, as this is done by default if Complete() not called
}
}
}
TransactionScope is your friend!
TransactionScope

Polling an access database inside a non-dedicated windows server

We have a website that runs on ASP.NET. I would like to run a service that checks the database table every XX hours and do an action ( send mail ) if some conditions are not met.
We cannot use
Windows Service as the hosted server is not a dedicated server. (We just have the control panel)
SQL Service for sending mail as we are using Access 2003 (mdb) as our database. It resides at App_Data
IIS is also not available
Its very critical that we need some kind of polling in the server.
We are kind of stuck now. What are the alternatives we have?
its mentioned here
Easy Background Tasks in ASP.NET
here are some snippets from that link
private static CacheItemRemovedCallback OnCacheRemove = null;
protected void Application_Start(object sender, EventArgs e)
{
AddTask("DoStuff", 60);
}
private void AddTask(string name, int seconds)
{
OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
HttpRuntime.Cache.Insert(name, seconds, null,
DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, OnCacheRemove);
}
public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
// do stuff here if it matches our taskname, like WebRequest
// re-add our task so it recurs
AddTask(k, Convert.ToInt32(v));
}
Works well in my testing; badges are awarded every 60 seconds like clockwork for all >users - Jeff Atwood
There's a trick you can use to simulate a windows service using just your ASP.NET web service.
The gist of it goes as follows:
Place an item in your cache with an expiry period that is equal to how often you would like to poll your database.
Add a callback that is fired when the item is removed from the cache. In the callback method, add your polling code to do the thing you want to do (call access database, send mails, etc.)
so: in your global asax, something like this:
private const string DummyCacheItemKey = "pollingCacheKey";
protected void Application_Start(Object sender, EventArgs e)
{
RegisterCacheEntry();
}
private bool RegisterCacheEntry()
{
if( null != HttpContext.Current.Cache[ DummyCacheItemKey ] ) return false;
HttpContext.Current.Cache.Add( DummyCacheItemKey, "Test", null,
DateTime.MaxValue, TimeSpan.FromMinutes(1),
CacheItemPriority.Normal,
new CacheItemRemovedCallback( CacheItemRemovedCallback ) );
return true;
}
public void CacheItemRemovedCallback( string key,
object value, CacheItemRemovedReason reason)
{
Debug.WriteLine("Cache item callback: " + DateTime.Now.ToString() );
// Do the service works
DoWork();
ReregisterCacheItem();
}
Whilst it's not ideal, it fits your constraints.
Full details of the technique can be found here: http://www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc

Resources