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

时间:2018-11-21 14:58:18

标签: c# sql-server asp.net-core

我想知道是否可以在ASP.NET Core 2.1应用程序启动时创建SQL分布式缓存。

我知道我可以运行此命令 dotnet sql-cache create <connection string> <schema> <table name>,但这是一个手动过程。我想在启动时自动执行此过程。

这可能吗?

谢谢

2 个答案:

答案 0 :(得分:3)

有两个选项:

Options1

由于我们可以运行命令来创建表,因此可以通过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;
    }

选项2

对于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;
    }

要调用CreateTableAndIndexes,可以实现以下扩展方法:

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

用于在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();

注意

  • SqlQueries来自SqlQueries

  • 安装软件包Microsoft.Extensions.CommandLineUtils

  • ConfigureSqlCache之后注册services.AddDistributedSqlServerCache

答案 1 :(得分:0)

您要如何自动执行此操作?我敢肯定,但是有很多方法可以添加分布式缓存。

您可以通过以下链接查看样本:

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