Service to pick up a change in the bank

Asked

Viewed 219 times

0

Here’s what I need. As I do for a service to auto startar whenever the flag of the bank changes from 0 leg 1. I have a field in a table in the BD(Sql Server), that every time the seller request a discount, the flag(Flagrelease byte) changes from 0 to 1 and at that time my service should send a message to the App. Well this, I do. I just don’t know how the service will automatically do this.

  • Guy the bank start a project I won’t know how to tell you, but if the service is already running you can create a Trigger or something for when someone does an UPDATE.

  • Would it have to be through notification, like: FCM or GCM https://firebase.google.com/docs/cloud-messaging/? hl=en I am also interested in joining this service to my Webservice

  • @Gabrielcoletta, it’s not the start bank. What I want if it is possible to make a type of timer for the service, that every 10s, for example, it makes a select in the table and see if changed. If yes, then send the notification to the App.

2 answers

0

You can create a Windows Service with a SqlDependency

protected override void OnStart(string[] args)
{
    SqlDependency.Stop(this.ConnectionString);
    SqlDependency.Start(this.ConnectionString);
    Task.Run(Monitorar);
}

protected override void OnStop()
{   
    SqlDependency.Stop(this.ConnectionString);
}

private async Task Monitorar()
{
    await SqlDependencyOnChange(null, null);
}

private async Task SqlDependencyOnChange(object sender, SqlNotificationEventArgs e)
{
    var flag = false;
    using (var conexao = new SqlConnection(this.ConnectionString))
    {
        await conexao.OpenAsync();
        using (var comando = new SqlCommand("SELECT Flag FROM Tabela", conexao))
        {
            var depedency = new SqlDependency(comando);
            depedency.OnChange += new OnChangeEventHandler(async (_sender, _e) => {
                await SqlDependencyOnChange(_sender, _e);
            });

            var result = comando.ExecuteScalar();
            if (result != null || result != DBNull.Value)
                flag = (bool)result;
        }
    }

    if (flag)
    {
        await Processar();
    }
}

private async Task Processar()
{
    //Envie a notificação para o seu App.
}

Remembering that the BROKER should be active in your Sql Server.:

ALTER DATABASE [Database] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE

0

What you want is to make a Job. Job is a task that will be performed every x value range and you can implement (not recommend) or use a third solution.

I know some like the Hangfire, Quartz, Rabbitmq or up to the Azure has solutions to register a service. In my case I will explain the Hangfire because it is the only one I know.

You need to create a recurring Job, where the server will call every x time your method:

RecurringJob.AddOrUpdate(() => MeuMetodoAValidarOBanco(), Cron.Daily);

Note that he does not use Datetime, usually for Job the concept of Cron is used. Cron is a string expression that you can work better with time intervals. Hangfire itself gives some simpler options, but there is this website where you can generate your expressions.

Because it’s a service, you can either upload it as a Windows service or even upload it to your API. The only problem with the latter is that IIS clears the pool when no one accesses the API. Follow the link for a related question here on Stack.

  • The customer preferred to (better) make the change in his system and send by parameter to a service that when receiving the parameter, the service then triggers the notification. I found it better this way. A service/processing less.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.