Azure DB 连接字符串在 ASP.NET Core 应用中为空
Posted
技术标签:
【中文标题】Azure DB 连接字符串在 ASP.NET Core 应用中为空【英文标题】:Azure DB connection string is null with an ASP.NET Core app 【发布时间】:2022-01-03 00:02:05 【问题描述】:我正在尝试在 Azure 上部署使用 ASP.NET Core 制作的应用。所以我做的第一件事是在 Azure 中创建服务器和数据库。然后,我在我的 secrets.json 文件中添加了 Azure 提供的连接字符串以及我的本地数据库连接字符串:
"ConnectionStrings":
"HikingContext": "Server=******\\SQLEXPRESS;Database=***;Trusted_Connection=True;MultipleActiveResultSets=true;",
"HikingContextProd": "Data Source=****.database.windows.net;Initial Catalog=***;User ID=*****;Password=*****;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;"
当我在生产环境中本地运行我的应用程序以检查 azure db 连接是否有效时,我收到以下错误
System.ArgumentNullException
HResult=0x80004003
Message=Value cannot be null. Arg_ParamName_Name
错误发生在 startup.cs 文件中
if(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production")
services.AddDbContext<HikingContext>(options => options.UseSqlServer(Configuration.GetConnectionString("HikingContextProd")), ServiceLifetime.Scoped);
尽管我实际上可以使用 id 和密码通过 SSMS 连接到服务器,但启动文件似乎找不到 Azure db 服务器。我多次检查了连接字符串,但没有看到任何错误,所以我真的不明白为什么它不起作用?
这是完整的错误信息:
System.ArgumentNullException
HResult=0x80004003
Message=Value cannot be null. Arg_ParamName_Name
Source=Microsoft.EntityFrameworkCore.SqlServer
Arborescence des appels de procédure :
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 sqlServerOptionsAction)
at Hiking.Startup.<ConfigureServices>b__4_0(DbContextOptionsBuilder options) in C:\Users\*****\Desktop\BeCode - Personnel\.NET\Hiking\Hiking\Startup.cs:line 50
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass1_0`2.<AddDbContext>b__0(IServiceProvider p, DbContextOptionsBuilder b)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.CreateDbContextOptions[TContext](IServiceProvider applicationServiceProvider, Action`2 optionsAction)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass17_0`1.<AddCoreServices>b__0(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
【问题讨论】:
我想你要找的公主在另一座城堡里……这个应用是怎么上天蓝色的?您遵循什么流程来部署它? @CaiusJard 我还没有部署它。我刚刚使用 azure 在线创建了一个 sql server 数据库,我想尝试一下我的本地应用程序是否可以连接到在线数据库并且它不起作用。 哦,我明白了.. 那么,如果你在该行设置断点if
环境变量的实际值是多少?连接字符串是否正确设置为 azure?您的 ip 地址是否添加到 azure db 服务器防火墙中? Configuration.GetConnectionString("HikingContextProd")
是否返回预期的字符串(将其粘贴到即时窗口中)?
@CaiusJard 环境变量的值为Production。连接字符串是正确的,并且没有防火墙问题,但实际上 Configuration.GetConnectionString("HikingContextProd") 为空。我将更新问题并发布 Visual Studio 给出的完整错误消息。
【参考方案1】:
更新
在这个问题上花了一天时间,我终于找到了问题的根源。似乎如果您在生产环境中并且想要将本地 ASP.NET Core 应用程序连接到 Azure sql 服务器,那么如果连接字符串在 secrets.json中,它将无法工作> 文件。
因此,不要将您的 Azure 连接字符串放在 secrets.json 文件中,而是使用 Azure 提供的 Key Vault 来存储您的连接字符串。我做到了,现在我可以成功地将我的本地 ASP.NET Core 应用程序连接到 Azure sql 服务器。
【讨论】:
以上是关于Azure DB 连接字符串在 ASP.NET Core 应用中为空的主要内容,如果未能解决你的问题,请参考以下文章
在 Azure 中为 ASP.NET Core Web 应用设置 SQL 连接字符串
使用 Azure KeyVault 存储连接字符串与使用 ASP.net IIS_Reg 进行加密有啥区别?