What is the proper way to download setup packages

2019-04-09 16:29发布

I wrote a WiX custom MBA that I have been using which embeds all of the setup packages (msis, cabs and exes) I need for my installation. However I would now like to make a lightweight web bootstrapper which will download the packages that need to be installed. I thought you would get that for free with the underlying WiX bootstrapper engine, but I guess I was wrong.

I tried subscribing to the ResolveSource event to get a package's download url and download it to the local source location, but it seems like at that point it's too late in the process as my installation fails with an error "Failed to resolve source for file: " (even though the download is successful).

Sample of what I tried:

private void OnResolveSource(object sender, ResolveSourceEventArgs e)
{  
    string localSource = e.LocalSource;
    string downloadSource = e.DownloadSource;

    if (!File.Exists(localSource) && !string.IsNullOrEmpty(downloadSource))
    {
        try
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.DownloadFile(e.DownloadSource, e.LocalSource);
            }
        }

        catch (ArgumentNullException ex)
        {
            e.Result = Result.Error;
        }

        catch (WebException ex)
        {
            e.Result = Result.Error;
        }
    }
}

标签: c# wix wix3.7
1条回答
时光不老,我们不散
2楼-- · 2019-04-09 17:12

Thanks to Rob Mensching answering this on the wix-users mailing list:

Make sure your packages of URLs provided (authored is easiest but you can programmatically set them all) then return IDDOWNLOAD from the ResolveSource call.

I edited my code as follows:

private void OnResolveSource(object sender, ResolveSourceEventArgs e)
{
    if (!File.Exists(e.LocalSource) && !string.IsNullOrEmpty(e.DownloadSource))
        e.Result = Result.Download;
}

Setting the result to Result.Download instructs the bootstrapper engine to download the package. No need to try to download the file myself.

查看更多
登录 后发表回答