如何为 ASP.NET Core / Angular Web 应用创建安装程序?

4
我们创建了一个"on-prem"网络应用程序,并且我被指派创建该应用程序的安装程序,使用户可以编程选择SQLite或SQL Server实现。我对如何做此项任务一无所知,并且没有找到任何具有清晰指导方向的好文章。
我在我的Startup.cs文件中编写了以下代码,以在我的appsettings.json文件中选择两个连接字符串之间进行选择。是否有人知道创建/实现安装程序的最佳方法?是否有开源解决方案可处理此类问题?我在这个问题上感到很迷茫....
protected virtual IServiceCollection ConfigureDbContext(IServiceCollection services)
        {
            var isSqlServerConnection = Configuration.GetValue<bool>("UseSql");

            if (isSqlServerConnection)
            {
                services.AddDbContext<SecurityDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("Default")).UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll),
                ServiceLifetime.Transient);

                services.AddDbContext<StorageContext>(options =>
                options.UseSqlite(Configuration.GetConnectionString("Default")).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking),
                ServiceLifetime.Transient);
            }
            else
            {
                services.AddDbContext<SecurityDbContext>(options =>
                options.UseSqlite(Configuration.GetConnectionString("Sqlite")).UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll),
                ServiceLifetime.Transient);

                services.AddDbContext<StorageContext>(options =>
                options.UseSqlite(Configuration.GetConnectionString("Sqlite")).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
             , ServiceLifetime.Transient);
            }

            return services;
        }

2
你目前看了什么?有一些开源解决方案,例如Wix工具集NSIS,... 你还应该能够使用安装程序引导SQL Server - kapsiR
假设这是一个你想要分发的SASS产品,你是否考虑过创建两个Docker容器,一个用于SQLite,另一个用于SQL Server,并通过Docker进行分发? - Preben Huybrechts
2个回答

2
使用Visual Studio创建安装程序
  1. 关闭除一个以外的所有Visual Studio实例。
  2. 在运行的实例中,访问菜单“工具->扩展和更新”。
  3. 在该对话框中,选择“在线->Visual Studio Marketplace->工具->设置和部署”。
  4. 从出现的列表中选择Microsoft Visual Studio 2017 Installer Projects。
  5. 安装完成后,关闭并重新启动Visual Studio。转到“文件->新建项目”并搜索单词“Installer”。如果您看到类似于以下列表的列表,则知道已安装正确的模板:

vs example

  1. 使用设置项目创建适合您需求的安装程序。例如,您可以轻松地在安装程序上创建一个页面,用户可以选择SQLite或SQL Server作为数据支持。

以下是有关创建安装程序和所需扩展的其他资源。根据您的Visual Studio版本,您可能需要另一个版本的扩展。

https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects

https://codeteddy.com/2016/04/04/creating-an-msi-package-for-c-windows-application-using-a-visual-studio-setup-project/

https://www.add-in-express.com/docs/net-msi-setup-project.php


1
好的。那么我认为我最大的问题是关于用户在安装过程中选择数据库引擎时的情况。安装程序会根据用户的选择,仅替换appsettings.json文件中“UseSql”键的值吗? - Dean Friedland
1
是的,您可以在设置项目中编写方法来实现这个目的。不会有什么“开箱即用”的东西,您需要编写代码来创建所需的功能,以适应特定的安装程序和设置要求,例如更改appsettings.json。 - vvvv4d
1
对不起,我刚从度假回来。还有一个问题。我会选择你的答案作为正确答案,但除非你觉得你的想法比Wix更好,否则我会采用Wix方式。你觉得你的方法比使用Wix工具集更好有什么原因吗?我看了这个教程...https://www.youtube.com/watch?v=6Yf-eDsRrnM - Dean Friedland
Wix Toolset看起来是一个更好的方法!我对它不是很熟悉,但我在微软上找到了一篇好文章:请查看这篇文章,如果你有任何问题,我很乐意聊天解答。https://learn.microsoft.com/en-us/archive/msdn-magazine/2007/march/automate-releases-with-msbuild-and-windows-installer-xml - vvvv4d
@vvv4d 我已经建立了安装程序,现在需要找出如何更改 appsettings.json 的值。我认为我需要创建一个自定义操作项目,在安装程序中创建对话框和复选框,将参数从对话框和复选框传递给自定义操作,然后自定义操作将修改 appsettings.json 文件中的值。这是否正确?这完全是理论,我还没有找到实现这一点的方法... - Dean Friedland
显示剩余4条评论

2
您也可以使用 "Inno Setup" 来创建您的安装程序。有很多示例代码可供参考。基本上您需要做以下几件事情。
准备工作:创建自己的工具,将信息写入 appSettings.json 文件中。该 exe 应该接受参数作为参数,并根据这些参数生成 appSetting.json 文件。
  1. 在 Inno Setup 文件部分中,设置到您构建的工件和自己的 json 生成工具的路径。
// 示例代码
[Files]
    Source: bin\*; DestDir: {app}\bin; Flags: recursesubdirs uninsneveruninstall; Components: Main
    Source: Utilities\AppSettingGenerator.exe; DestDir: {app}\Utilities\AppSettingGenerator.exe; Components: " Main"; Tasks: ; Flags: uninsneveruninstall; 

创建一个屏幕,让用户选择数据库引擎,SQLite或SQL Server。
创建另一个屏幕来设置连接字符串。
//示例代码。创建安装程序屏幕。
procedure FormCreatePage(PreviousPageId: Integer);
begin
    pgeInstallType := CreateInputOptionPage(    wpWelcome,
                                                'Select Installation Type',
                                                'Which type of installation do you want to run?',
                                                'Select the type of installation that you would like to run. Click Next when you are ready to continue.',
                                                true,
                                                false
                                             );

    pgeInstallType.Add('Sqllite');
    pgeInstallType.Add('Sql Server');

    pgeInstallType.Values[0] := true;

    
    pgeInput1 := CreateCustomPage(  PreviousPageId,
                                    'Configure Sql Server Connection',
                                    'Please verify the details for those sections highlighted in red before continuing.'
                                 );

    pgeInput2 := CreateCustomPage(  pgeInput1.ID,
                                    'Configure Sql lite Connection',
                                    'Please verify the details for those sections highlighted in red before continuing.'
                                 );
end;

//示例代码。创建UI控件,让用户输入Sql Server连接信息
pnlSQL := TPanel.Create(pgeInput1);
    with pnlSQL do
        begin
            Parent := pgeInput1.Surface;
            Left := ScaleX(0);
            Top := ScaleY(30);
            Width := ScaleX(413);
            Height := ScaleY(125);
            BevelInner := bvLowered;
        end;

    { lblPnlSQL }
    lblPnlSQL := TLabel.Create(pgeInput1);
    with lblPnlSQL do
        begin
            Parent := pnlSQL;
            Left := ScaleX(340);
            Top := ScaleY(5);
            Width := ScaleX(70);
            Height := ScaleY(13);
            AutoSize := False;
            Caption := 'SQL Server';
            Font.Height := ScaleY(-11);
            Font.Style := [fsBold, fsItalic];
        end;

    { lblSrvName }
    lblSrvName := TLabel.Create(pgeInput1);
    with lblSrvName do
        begin
            Parent := pnlSQL;
            Left := ScaleX(5);
            Top := ScaleY(5);
            Width := ScaleX(66);
            Height := ScaleY(13);
            Caption := 'Server Name:';
            Font.Height := ScaleY(-11);
        end;

    { lblUserID }
    lblUserID := TLabel.Create(pgeInput1);
    with lblUserID do
        begin
            Parent := pnlSQL;
            Left := ScaleX(5);
            Top := ScaleY(25);
            Width := ScaleX(40);
            Height := ScaleY(13);
            Caption := 'User ID:';
            Font.Height := ScaleY(-11);
        end;

    { lblPassword }
    lblPassword := TLabel.Create(pgeInput1);
    with lblPassword do
        begin
            Parent := pnlSQL;
            Left := ScaleX(5);
            Top := ScaleY(46);
            Width := ScaleX(50);
            Height := ScaleY(13);
            Caption := 'Password:';
            Font.Height := ScaleY(-11);
        end;

    { lblDBName }
    lblDBName := TLabel.Create(pgeInput1);
    with lblDBName do
        begin
            Parent := pnlSQL;
            Left := ScaleX(5);
            Top := ScaleY(67);
            Width := ScaleX(80);
            Height := ScaleY(13);
            Caption := 'Database Name:';
            Font.Height := ScaleY(-11);
        end;

在安装程序屏幕上输入参数后,请调用您的appsettingGenerator工具。
//示例代码。在nextbuttonClick中调用它,并检查当前页面是否正确。
function NextButtonClick(CurPageID: Integer): Boolean; 
var     i: Integer; 
begin 
  if CurPageID = pgeInput2.ID then      
  begin                                 
    RunExe(gUtilAppsettingGenerator,' -dbuid "' + txtUserID.Text + '"
     -dbpass "' + txtPassword.Text + '" -c "server=' + txtSrvName.Text + ';database='    + txtDBName.Text + '" -dbinst "' + txtDSN.Text + '"', ewWaitUntilTerminated, true); 

end;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接