Azure功能.net 5 UseSqlServer配置

rryofs0p  于 2023-01-21  发布在  .NET
关注(0)|答案(2)|浏览(129)

我正在尝试使用.net 5“dotnet-isolated”部署一个azure函数,但无法在其上运行sql server。这是我的配置启动

static async Task Main(string[] args)
{
#if DEBUG
    Debugger.Launch();
#endif
    var host = new HostBuilder()
        .ConfigureAppConfiguration(c =>
        {
            c.AddCommandLine(args);
        })
        .ConfigureFunctionsWorkerDefaults(
            (c, b) =>
            {
                b.UseFunctionExecutionMiddleware();
            })
        .ConfigureServices(
            (context, services) =>
            {
                services.AddDbContext<ApplicationDbContext>(
                    options =>
                        options.UseSqlServer(
                            context.Configuration.GetConnectionString("DefaultConnection")
                        )
                );
            })
        .Build();
    await host.RunAsync();
}

好像是

options.UseSqlServer(context.Configuration.GetConnectionString("DefaultConnection")))

无法识别Azure配置

disbfnqx

disbfnqx1#

问题出在ConfigureAppConfiguration中。必须是:

.ConfigureAppConfiguration(c =>
                {
                    c.AddEnvironmentVariables();
                    c.AddCommandLine(args);
                })

因此,最终Main为:

static async Task Main(string[] args)
        {
#if DEBUG
            Debugger.Launch();
#endif
            var host = new HostBuilder()
                .ConfigureAppConfiguration(c =>
                {
                    c.AddEnvironmentVariables();
                    c.AddCommandLine(args);
                })
                .ConfigureFunctionsWorkerDefaults((c, b) =>
                {
                    b.UseFunctionExecutionMiddleware();
                })
                .ConfigureServices((context, services) =>
                                        {
                                            services.AddDbContext<ApplicationDbContext>(options =>
                                                options.UseSqlServer(context.Configuration.GetConnectionString("DefaultConnection")));
                                        })
                .Build();
            
            await host.RunAsync();
        }
s71maibg

s71maibg2#

看起来azure与您的构建没有联系,要么是因为您对它的配置不稳定,要么是因为您希望它快速处理过多的sql数据,我猜是第二种。

相关问题