2

The solution I am working on requires multiple processes that are built in multiple Windows services all built using C# running on .NET.

  • Service 1 : Extract data in files in a folder
  • Service 2 : Analyse data and put results in image file
  • Service 3 : Process images into another application
  • Service 4 : Generate report

Each service runs every hour. Everything works fine.

One of the requests now is to do all services as fast as possible. Therefore I want these services to run consecutively.

How to call one service from the other or have a pipeline that runs all services one after the other.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
asmgx
  • 7,328
  • 15
  • 82
  • 143
  • @MitchWheat I did not get it, what is Filewatcher? – asmgx Apr 27 '21 at 01:17
  • sorry meant FileSystemWatcher. https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-5.0 – Mitch Wheat Apr 27 '21 at 01:35
  • @MitchWheat This is excellent solution.. I followed that and it is working.. just one thing.. is there a way i can make the OnChanged event to occur only once even if many files were copied? – asmgx Apr 27 '21 at 02:45
  • https://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice – Mitch Wheat Apr 27 '21 at 02:54
  • with the help of your comment and this https://stackoverflow.com/questions/33990605/watchfolder-event-in-c-sharp-triggers-multiple-times-on-a-copy-file – asmgx Apr 27 '21 at 05:14

2 Answers2

2

You can use a message queue. I recommend RabbitMQ. It is simple. By the your business flow each service listens its pipeline/queue. For example Service1 extracts data from files then sends a message to Service2’s queue. Service2 gets the message and runs its task, after completes its task sends a message to Service3... You can build a flow ilke this with RabbitMQ.

1

You could use FileSystemWatcher and use folders as simple queues between services. Also note, FileSystemWatcher might not be 100% Reliable: FileSystemWatcher vs polling to watch for file changes

Might need to set the NotifyFilter to get the behaviour you want.

You could also add a mutex to the service, that detects if it is already running perhaps, and consumes folder/file changes in one go.

There are other heavier weight, fully featured message queues, but these might be overkill for your needs.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    May be worth noting that FileSystemWatcher on its own isn't always good enough to detect all changes, see answers here: https://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes – Ian Mercer Apr 27 '21 at 06:50