Reputation: 53
I'm very new at programming and coding and I've been asked to install a service multiple times and for them to have different config files. Here's the thing:
On the config files I have connection settings that need to be different for both services. The service just retrieves information from another controller, but they need to recieve information from two different controllers and that's why they have asked to have two different instances of the same service with different config files.
I'm stucked with this. I know that both of the services have to have different names. But how can I install them with Inno setup and have them read two different config files?
The service was made with #C, .NET Framework 4.5.2
I have the simple installer for 1 instance:
[Files]
Source: "C:\...\Service.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\...\log4net.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:...\Service.exe.config"; DestDir: "{app}"; Flags: ignoreversion
[Run]
Filename: {sys}\sc.exe; \
Parameters: "create Service start= auto binPath= ""{app}\Service.exe"""; \
Flags: runhidden
[UninstallRun]
Filename: {sys}\sc.exe; Parameters: "stop Service"; Flags: runhidden
Filename: {sys}\sc.exe; Parameters: "delete Service"; Flags: runhidden
It's a very simple service. I know how to add Components
with checkboxes to make the user choose how many instances they want. But I don't know how to add a second instance and how make the second instance read the second config file.
Is there something that I need to change in my service code? Is this at all possible?
I'm sorry for my poor english and if I coudn't explain myself better. I know pretty much nothing about coding and services. Thanks for your help!
Upvotes: 4
Views: 2502
Reputation: 202612
The name of the service is the first argument after create
command.
So create two entries in the [Run]
section, one for each service:
[Run]
Filename: {sys}\sc.exe; \
Parameters: "create Service1 start= auto binPath= ""{app}\Service.exe""" ; \
Flags: runhidden
Filename: {sys}\sc.exe; \
Parameters: "create Service2 start= auto binPath= ""{app}\Service.exe""" ; \
Flags: runhidden
And similarly for the [UninstallRun]
.
Regarding the loading of configuration file. You didn't tell us anything about, how your service code determines where to load the configuration file from in the first place.
Assuming the path is somehow hardcoded (or somehow programmatically resolved), you can change the code to incorporate the service name into the path.
To determine name of the service instance for which your C# code is currently running, see How can a Windows Service determine its ServiceName?
Upvotes: 1