Return from a Windowsservices via an ASP.NET application

Asked

Viewed 88 times

2

I have a Windows Service which at a certain time scans the machine and performs a backup, to a folder on a server on the network.

The Service is running on the machines perfectly and running the backups without errors.

But I want to create an application WEB, to receive information from this JPA Windows Service as "Backup Successfully Performed" among others.

How do I do it ? Through a ASP.NET WEB API ? Some other way ?

  • Do you need to show the status of what’s happening in the service? You need to save the history of what was executed to log?

  • @Intruso Look, I hadn’t thought about the status of the service. But it’s a good one. Logs yes, I think about saving. What I need initially is to know how to do this. So what I can research. Initially showing the way, would already be an excellent help =)

1 answer

0


Use the database to save the status of executions, it will serve not only to show the user what is happening, but also the execution history.

Create a Table (something like Processoexecucaostatus) where you can have a Description field and another Status (I always have an ID field that is a primary key with auto-increment, makes the control very objective and easy to know which record I am working on).

The description field can be text to describe the routine that was executed and the status field controls whether it was executed or not and whether it was successful.

In the service, change the method you run daily (in our Example, let’s call Rotinadiariabackup) to at each stage of the Method perform the status update in the database.

Example:

void RotinaDiariaBackup()
{
  try
    {
      rep.InsereStatusProcessoIniciado();

      this.ExecutaBackup(); //este metodo é a sua rotina (aquilo que o serviço ja executa de tempos em tempos.

      rep.InsereStatusProcessoExecutadoSucesso();
     }
  catch(Exception)
  {
     rep.InsereStatusProcessoExecucaoFalha();
  }
}

Logically, there are some flaws in this method, from many points of view so keep in mind the design of the solution, not this code itself.

You should better consider error handling, consider that the methods you enter into the database may also fail (one should have an alternate error log for tracking - a txt or windows log).

But, in general, let’s explain the logic:

rep = repository for access to the database that stores the process data, to simplify, I did not create a process objectExecucao, but it is highly recommended as you may need to represent more than the status and description.

Therefore, the methods would receive an instance of Processoexecucao (that does not yet exist) to carry out the actions.

insertion.(); = basically this snippet inserts in the database a row in the table Processoexecucaostatus with the description you want as default and status (say status = 0 is running in progress).

rep.Inserts status Executed process(); = Update the row you have now entered by updating the status of the table to success (let’s say Status = 1).

rep.Inserts status Run failed(); = If one of the previous methods fails, an Exception will be launched and you update the status record to 2 (status with error).

You see, it should be noted that other methods may fail that are not your routine (the successful update may fail), so you should think about how to control this flow (transaction?)

After the modifications in the service, implement a screen that shows the records of the table Processoexecucaostatus and you will have the status of what was executed and also of what is being executed, because the screen only shows the records.

This control table approach is quite common, however, there are some problems:

  • the status of the table is not the status of the service, so if the service fails for any reason other than routine, the screen does not show this.

  • The screen shows a photo at the time of what was inserted, there is no communication between the processes, so it does not show the status of the service at that instant, shows the last record change that was possible to do, if you intend to use the screen to monitor the service, not that way, will have to implement another form of access to the service itself (which is usually a little more complex).

  • Performance of the database, currently your service has no connection to the database, performing fewer requests and as a consequence, the database (which may be a simple machine) does not suffer from it, you will be going to the bank periodically (both to read to the screen, and to perform their routines).

  • Please don’t make one Select * from Processxecucaostatus on the screen, put a conditional or a top, with time the table will grow. This solves itself that you correctly implement the repository and access the screen of the same classes treating the service as if it were a GUI, but, not everyone thinks so, many do not have layers or treat as another mini-system.

Finally, if you need to include new routines in this systematic, just put a Typorotin column in the table and create a new Method for the new routine, following the same structure and the screen will already reflect the new status, it is easy to maintain and expand, something like this:

void RotinaDiariaEmails()
{
  try
    {
      int tipoRotina = 1; // 1 = envio diario de emails  - 0 = backup

      rep.InsereStatusProcessoIniciado(tipoRotina);

      this.ExecutaEnvioEMails(); //este metodo é a sua rotina 

      rep.InsereStatusProcessoExecutadoSucesso(tipoRotina);
     }
  catch(Exception)
  {
     rep.InsereStatusProcessoExecucaoFalha(tipoRotina);
  }
}

In the service, you would call the methods in sequence or have another queue structure that executes the methods:

void StartSomeTimes()
{
    RotinaDiariaEmails();
    RotinaDiariaBackup();
}

You probably have a timer on your service that runs from time to time, you would call Startsometimes in the body of the method that is run by the timer.

  • Show!!! Excellent answer...

  • when you say, "if you intend to use the screen to monitor the service, it is not that way, you will have to implement another form of access to the service itself (which is usually a little more complex)." It would be an application with the use of Socket ? Something related? Could you give me a statement so I can do some research

  • Can be done by a socket tcp yes, no problem. The server would listen to the service and the page would trigger a ping method to pick up the status of the service, at each page post, the status would be updated.

  • What information do you want to know about the service via socket?

  • Actually, I just want to know what another way to implement access to the service is. Then I used the socket as an example, if it would be another way.

  • 1

    Old man, I can tell you something about sockets, but what you want is practically another answer :) take a look at this Tuto http://www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-using (English) for the tcp part

Show 1 more comment

Browser other questions tagged

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