【问题标题】:How to create installer for ASP.NET Core / Angular web app?如何为 ASP.NET Core / Angular Web 应用程序创建安装程序?
【发布时间】:2020-08-14 21:31:55
【问题描述】:

我们创建了一个“本地”网络应用程序,我的任务是为该应用程序创建一个安装程序,该安装程序将以编程方式允许用户在 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;
        }

【问题讨论】:

  • 到目前为止你看到了什么?有一些开源解决方案,例如WiX toolset, NSIS, ... 您应该也可以使用安装程序boostrap SQL Server
  • 假设这是您要分发的 SASS 产品,您是否考虑过创建 2 个 docker 容器,一个用于 SQLite,一个用于 SQL Server?并通过 docker 分发?

标签: c# .net sql-server deployment


【解决方案1】:

使用 Visual Studio 创建安装程序

  1. 关闭除一个 Visual Studio 实例之外的所有实例。
  2. 在正在运行的实例中,访问菜单工具->扩展和更新。
  3. 在该对话框中,选择 Online->Visual Studio Marketplace->Tools->Setup & Deployment。
  4. 从出现的列表中,选择 Microsoft Visual Studio 2017 安装程序项目。
  5. 安装后,关闭并重新启动 Visual Studio。转到 File->New Project 并搜索单词 Installer。如果您看到如下所示的列表,则表明您安装了正确的模板:

  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

【讨论】:

  • 好的。所以我认为我最大的问题是关于用户在安装过程中何时选择数据库引擎。安装程序是否会根据用户的选择替换我的 appsettings.json 文件中 UseSql 键的值?
  • 是的,您可以在安装项目中为此目的编写方法。不会有“开箱即用”的东西,您将不得不编写代码来创建满足特定安装程序和设置要求所需的功能,例如对 appsettings.json 的更改。
  • @Dean Friedland 您需要更多信息吗?
  • 对不起。刚放假回来。另一个问题。我选择你的答案是正确的,但除非你认为你的想法更好,否则我会选择 Wix 路线。你的方式比使用 Wix 工具集更好吗?我在看这个教程...youtube.com/watch?v=6Yf-eDsRrnM
  • Wix 工具集看起来是一种更好的方法!我对它不是很熟悉,但我确实找到了一篇关于 Microsoft 的好文章:看看这篇文章,如果你有任何问题很高兴可以加入聊天,请告诉我。 docs.microsoft.com/en-us/archive/msdn-magazine/2007/march/…
【解决方案2】:

您也可以使用“Inno Setup”来创建您的安装程序。那里有很多示例代码。基本上你需要做一些事情。

准备工作:创建您自己的工具以将信息写入您的 appSettings.json。 exe 应该将参数作为参数,并根据这些参数生成一个 appSetting.json 文件。

  1. 在 inno setup files 部分,设置构建工件的路径和您自己的 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; 
  1. 创建一个屏幕让用户选择数据库引擎、sqllite 或 Sql server。
  2. 创建另一个屏幕来设置连接字符串。

//示例代码。创建安装程序屏幕。

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;
  1. 在安装程序屏幕上输入参数后调用您的 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2016-06-14
    • 2020-02-03
    相关资源
    最近更新 更多