http_error_418
http_error_418

Reputation: 70

Provide config settings at install time

I have created a Windows service which is installed via an MSI. The service requires a host/IP, port, and path to communicate with which I have added as separate properties in VS2017 Settings.settings. I now want to have the installer take user input and write the settings to my config file. To begin with, I tried the following:

    public override void Install(IDictionary stateSaver)
    {
        string server;
        string port;
        string path;

        base.Install(stateSaver);

        server = this.Context.Parameters["SERVER"];
        port = this.Context.Parameters["PORT"];
        path = this.Context.Parameters["PATH"];

        Properties.Settings.Default.server = server;
        Properties.Settings.Default.port = port;
        Properties.Settings.Default.path = path;

        Properties.Settings.Default.Save();
    }

And an extract from my app.config file...

<userSettings>
    <myService.Properties.Settings>
        <setting name="server" serializeAs="String">
            <value />
        </setting>
        <setting name="port" serializeAs="String">
            <value />
        </setting>
        <setting name="path" serializeAs="String">
            <value />
        </setting>
    </myService.Properties.Settings>
</userSettings>

This doesn't save the settings, so I resorted to Google. The closest thing I have found is this question, however I don't fully understand what it's suggesting (I'm pretty new to C#). Am I using a full ServiceModel section group? I'm sure I could figure out how to edit the XML directly but that would be a bodge and not the proper way, I'd rather do it right.

Upvotes: 1

Views: 71

Answers (1)

PhilDW
PhilDW

Reputation: 20780

The general issue is that installer classes are instantiated using reflection, from a C++ shim Dll that's called by the msiexec service doing the install. In this environment the automatic stuff that goes on when assemblies and executables are loaded "normally" doesn't happen. In other words you have to do the Xml work yourself, specifying exactly the settings file path (because there is also no useful default working directory in this environment either).

Upvotes: 1

Related Questions