简体   繁体   English

在Asp.net核心启动时创建的SQL分布式缓存

[英]SQL distributed cache created on Asp.net core startup

I was wondering if there is a way to create the SQL distributed cache on startup of the ASP.NET Core 2.1 application. 我想知道是否有一种方法可以在ASP.NET Core 2.1应用程序启动时创建SQL分布式缓存。

I know I can run this command dotnet sql-cache create <connection string> <schema> <table name> but this is a manual process. 我知道我可以运行此命令dotnet sql-cache create <connection string> <schema> <table name>但这是一个手动过程。 I want to automate this process on startup. 我想在启动时自动执行此过程。

Is this possible? 这可能吗?

Thanks 谢谢

Here are two options: 这里有两个选择:

Options1 选项1

Since we could run command to create the table, we could run the command from code by Process . 由于我们可以运行命令来创建表,因此可以通过Process从代码中运行命令。

    public static IServiceCollection ConfigureSqlCacheFromCommand(this IServiceCollection services)
    {
        var options = services.BuildServiceProvider().GetRequiredService<IOptions<SqlServerCacheOptions>>();

        var process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Arguments = $"/c dotnet sql-cache create \"{options.Value.ConnectionString}\" { options.Value.SchemaName } { options.Value.TableName }",
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = false,
                WindowStyle = ProcessWindowStyle.Normal,
                RedirectStandardInput = true,
                RedirectStandardError = true
            }
        };
        process.Start();
        string input = process.StandardError.ReadToEnd();
        string result = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        return services;
    }

Option2 选项2

For dotnet sql-cache command, it also calls dotnet-sql-cache , and you could implement the code to create the table programmly. 对于dotnet sql-cache命令,它也调用dotnet-sql-cache ,您可以实现代码以编程方式创建表。

        private static int CreateTableAndIndexes(SqlServerCacheOptions options)
    {
        using (var connection = new SqlConnection(options.ConnectionString))
        {
            connection.Open();

            var sqlQueries = new SqlQueries(options.SchemaName, options.TableName);
            var command = new SqlCommand(sqlQueries.TableInfo, connection);

            using (var reader = command.ExecuteReader(CommandBehavior.SingleRow))
            {
                if (reader.Read())
                {
                    return 1;
                }
            }
            using (var transaction = connection.BeginTransaction())
            {
                try
                {
                    command = new SqlCommand(sqlQueries.CreateTable, connection, transaction);

                    command.ExecuteNonQuery();

                    command = new SqlCommand(
                        sqlQueries.CreateNonClusteredIndexOnExpirationTime,
                        connection,
                        transaction);

                    command.ExecuteNonQuery();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return 1;
                }
            }
        }
        return 0;
    }

For calling CreateTableAndIndexes , you could implement this extension method: 为了调用CreateTableAndIndexes ,可以实现以下扩展方法:

        public static IServiceCollection ConfigureSqlCache(this IServiceCollection services)
    {
        var options = services.BuildServiceProvider().GetRequiredService<IOptions<SqlServerCacheOptions>>();
        int result = CreateTableAndIndexes(options.Value);
        return services;
    }

For configuring in Startup.cs 用于在Startup.cs进行配置

        services.AddDistributedSqlServerCache(options => {
            options.ConnectionString = @"Server=localhost\MSSQLSERVER01;Database=IISWindows;Trusted_Connection=True;";
            options.TableName = "CacheFromCommand";
            options.SchemaName = "dbo";
        });

        //services.ConfigureSqlCache();
        services.ConfigureSqlCacheFromCommand();

Note 注意

  • SqlQueries is from SqlQueries . SqlQueries来自SqlQueries

  • install package Microsoft.Extensions.CommandLineUtils . 安装软件包Microsoft.Extensions.CommandLineUtils

  • Register ConfigureSqlCache after services.AddDistributedSqlServerCache services.AddDistributedSqlServerCache之后注册ConfigureSqlCache

How do you want to automate this? 您想如何自动化呢? I am sure but there are many ways to add a distributed cache. 我敢肯定,但是有很多方法可以添加分布式缓存。

You can see the samples the following link: 您可以通过以下链接查看样本:

https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-2.1 https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-2.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM