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

C# 如何为ASP.NET Core/Angular web应用程序创建安装程序?,c#,.net,sql-server,deployment,C#,.net,Sql Server,Deployment,我们创建了一个“on-prem”web应用程序,我的任务是为该应用程序创建一个安装程序,该程序将以编程方式允许用户在SQLite或SQL Server实现之间进行选择。我对如何做到这一点没有任何线索,也没有找到任何方向明确的好文章 我所做的就是在我的Startup.cs文件中编写以下代码,以便在我的appsettings.json文件中的两个连接字符串之间进行选择。有人知道创建/实现安装程序的最佳方法吗?这类事情有开源解决方案吗?在这件事上我感到很失落 protected virtual ISe

我们创建了一个“on-prem”web应用程序,我的任务是为该应用程序创建一个安装程序,该程序将以编程方式允许用户在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;
        }
受保护的虚拟IServiceCollection ConfigureDbContext(IServiceCollection服务)
{
var isSqlServerConnection=Configuration.GetValue(“UseSql”);
if(isSqlServerConnection)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“默认”)).UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll),
服务寿命(瞬时);
services.AddDbContext(选项=>
options.UseSqlite(Configuration.GetConnectionString(“默认”)).UseQueryTrackingBehavior(QueryTrackingBehavior.NotTracking),
服务寿命(瞬时);
}
其他的
{
services.AddDbContext(选项=>
options.UseSqlite(Configuration.GetConnectionString(“Sqlite”)).UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll),
服务寿命(瞬时);
services.AddDbContext(选项=>
options.UseSqlite(Configuration.GetConnectionString(“Sqlite”)).UseQueryTrackingBehavior(QueryTrackingBehavior.NotTracking)
,服务寿命。瞬态);
}
返回服务;
}

使用Visual Studio创建安装程序

  • 关闭除一个Visual Studio实例外的所有实例
  • 在正在运行的实例中,访问菜单工具->扩展和更新
  • 在该对话框中,选择联机->Visual Studio市场->工具->设置和部署
  • 从显示的列表中,选择Microsoft Visual Studio 2017安装程序项目
  • 安装后,关闭并重新启动Visual Studio。转到文件->新建项目并搜索word Installer。如果您看到类似以下内容的列表,您将知道已安装正确的模板:
  • 使用安装项目创建安装程序以满足您的需要。您可以轻松地在安装程序上创建一个页面,例如,用户选择SQLite或SQL Server作为备份数据
  • 下面是一些关于创建安装程序和所需扩展的附加资源。根据您的Visual Studio版本,您可能需要其他版本的扩展

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

    准备:创建自己的工具,将信息写入appSettings.json。exe应将参数作为参数,并基于这些参数生成appSetting.json文件

  • 在inno设置文件部分,设置构建工件的路径,以及您自己的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; 
    
  • 创建一个屏幕,让用户选择数据库引擎、sqllite或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;
    

    到目前为止你看了什么?有一些开源解决方案,例如。。。您还应该能够使用InstallerSumming这是您想要分发的SASS产品,您是否考虑过创建两个docker容器,一个用于SQLite,另一个用于SQL Server?通过docker分发的?好的。因此,我认为我最大的问题是关于用户在安装过程中何时选择数据库引擎。安装程序会根据用户的选择替换my appsettings.json文件中的
    UseSql
    键的值吗?是的,您可以在安装项目中为此编写方法。不会有“开箱即用”的东西,您必须编写代码来创建满足特定安装程序和设置要求所需的功能,例如对appsettings.json的更改。@Dean Friedland您需要更多信息吗?对不起。刚度假回来。还有一个问题。我选择你的答案是正确的,但我会走Wix路线,除非你认为你的想法更好。您的方式比使用Wix工具集更好,有什么原因吗?我正在看这个教程。。。Wix工具集看起来是一个更好的方法!我对它不是很熟悉,但我确实找到了一篇关于微软的好文章:看看这篇文章,如果你有任何问题,请告诉我。