0

How to run multiple instances of a windows service in same server in C#.net? I have one windows service, I want to create run 4 instances of this service in one server, I have one idea but not sure how to proceed. Can I specify 4 instances name in an app-config file and run each instance one after the other? please help me out, A sample code will be very helpful, thanks in advance.

divya
  • 21
  • 5
  • 1
    You can install the same service under a different name. (Like "MyService1, MyService2, ...") But I never had different configs for different instances, so I am not sure about this part. – Fildor May 05 '20 at 12:47
  • I don't want different configs, in the same single config file I want to mention all 4 instances. – divya May 05 '20 at 12:55
  • Not sure how you would make an instance aware which one it is... does that make sense? – Fildor May 05 '20 at 12:56
  • lets say I have one service that does encryption of say 1000 records and writes logs into a log file with information like start n end time for encryption, failures if any , etc, now I will divide into 4 instances each instance processing 250 records at a time, ultimately I can have only one log file which shows name of instance followed by other information I mentioned above, so is this possible to have a single config file achieve what is required? – divya May 05 '20 at 14:17
  • So, it's only about a log file's name? Is there a logging framework involved? If you have one, you should be able to configure it to use unique filenames, so you don't have to worry about that. – Fildor May 05 '20 at 14:21
  • BTW: How a Windows Service can determine its own ServiceName: https://stackoverflow.com/q/1841790/982149 – Fildor May 05 '20 at 14:22
  • can all instances write logs to same log file, can I achieve that? am using Log4net? – divya May 05 '20 at 14:31
  • I am sure, it is possible, yet is not without caveats. 1. Shared access can and will be detrimental to performance. 2. Logentries from different services may be written at the same time, so you need to make sure, you can filter. – Fildor May 05 '20 at 14:33
  • With 2. I mean you cannot expect "Svc1 start, line1, line2, line3, Svc1 end, Svc2 start ..." but it will perhaps look like "Svc1 start, Svc2 start, line1, line2, line1, line3, line2, ..." – Fildor May 05 '20 at 14:35
  • Following [this question](https://stackoverflow.com/q/2586431/982149) and its answers, I wouldn't recommend a shared file. – Fildor May 05 '20 at 14:38
  • yes threading is really a best fit, but due to some project constraints, we are not allowed to use threading. – divya May 05 '20 at 17:55
  • is there any other solution to my problem, lets keep logging aside, lets say I can have a separate log file for each instance too, coming to processing the records, now I have a single instance and in config file I am specifying how many records to pick up(for Ex: range 0 to 1000) , like wise I want my second instance to pick up next set of records from 1001 to 2000, third instance from 2001 to 3000 and so on, so is it right to have a separate config files for each instance? – divya May 05 '20 at 18:06

0 Answers0