How to inject configuration settings into an MSIX package

1.9k views Asked by At

I've got a mature wcf application currently packaged with wix that generates an msi. When the msi is installed on a user's pc (using the wix /msi gui or using msiexec automated with powershell remoting) they pass in parameters such as the URL of the application's back end web service that are saved to the app's app.config file.

I'm looking to replace the wix installer process with MSIX. The main benefit to me of this switch will be that the users can install the app themselves from a web server URL, rather than having to faff with downloading and running an MSI.

To achieve a simple click and install process and be able to stage my MSIX in different customer environments I need some way to set my app's back end URL per environment when I stage the app. Given the MSIX is a self container package where all it's files are hashed and signed to ensure it's not tampered with, is there a way for me to ship a different URL when I stage the app without having to repackage the app whenever I stage it?

For context the app is a product that we customise for a lot of customers so internally we have a lot of environments we are constantly deploying to with automated tools so I want to avoid having to repackage dynamically when I'm setting up a new environment.

I basically want to ship config with, but not inside the msix.

2

There are 2 answers

0
Bogdan Mitrache On

MSIX changes the way we configured apps at install time. Using an MSIX package you can no longer capture user inputs (app configurations) during the installation, nor can you execute any custom code. This means that you no longer have the option to customize anything at install time.

As you said, the files delivered within the MSIX cannot be tampered with. The only way to achieve this behavior is to retrieve and apply additional settings when the app is launched for the first time.

  1. This can be done both manually, i.e. you design custom dialogs that your users will see and fill in only the first time they launch the app.

  2. Or you can implement an automatic customization support that relies on the URL of your AppInstaller, i.e. each of your users must receive a different AppInstaller link. When your package installs on the system, it will cache that link and you can interrogate it using predefined APIs, thus implement a custom behavior in your application based on the link you read.

In this example from the MSIX techcommunity forums I included a sample which shows how you can save the AppInstaller URL in the registry, using a PowerShell script.

Now, this sample relies on the Package Support Framework integration from Advanced Installer. Using this method you get more flexibility as you can customize the PS script included in the MSIX package without changing the code of your app. You can even extend the PS script to update your config file based on the URL it reads.

However, you can skip entirely using the Package Support Framework and simply add the code that saves the URL inside your app. Then configure your app to check this URL every time it is launched, using the sample code below, and update your config file based on the URL it reads.

Obviously, the default version of your config file would need to contain a unique placeholder, this way you can skip checking the AppInstaller URL if the placeholder is missing (i.e. your app replaced it with corresponding configurations, based on the URL it detected)

[Windows.ApplicationModel.Package, indows.ApplicationModel,ContentType=WindowsRuntime]
$path = [Windows.ApplicationModel.Package]::Current.GetAppInstallerInfo().Uri.AbsolutePath

Very Important! Make sure you save the config file in the AppData folder and not in the installation folder (as you could do when using an MSI). If you try to write in any file from the installation folder your app will fail.

AppData is handled differently for MSIX package apps, you can read more here:

2
zett42 On

Have you looked at modification packages?

These are somewhat similar to MSI transform files, but can be installed later than the MSIX file (like an addon or DLC package). They basically allow you to add to or overwrite files in the virtual file system of the original package. Your app could read a configuration file that stores the URL, which in turn could be modified by installing a modification package, without having to touch the original MSIX package.