Azure函数应用程序无法获得连接字符串:值不能为null。 (参数' ConnectionsTring')
我具有DDD架构的Azure功能。我的项目结构看起来像这样:
local.settings.json
文件看起来像这样:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"ServiceBusConnectionString": "Endpoint=sb://sb.servicebus.windows.net/;*****"
},
"ConnectionStrings": {
"DefaultConnection": "Server=tcp:*************"
}
}
和我的 appsettings.json
看起来像这样:
{
"ConnectionStrings": {
"DefaultConnection": "*******"
}
}
and <代码> ApplicationDbContextFactory 文件如下:
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
return new ApplicationDbContext(optionsBuilder.Options);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要指定连接字符串前缀(请参阅文档):
此前缀分类为:
customConnstr_ =&gt;自定义提供商
mysqlconnstr_ =&gt; mysql
sqlazureconnstr_ =&gt; Azure SQL数据库
SQLConnStr_ =&gt; SQL Server
信用额为本文中的人:
You need the specify the connection string prefix (see documentation):
This prefix classification is:
CUSTOMCONNSTR_ => Custom provider
MYSQLCONNSTR_ => MySQL
SQLAZURECONNSTR_ => Azure SQL Database
SQLCONNSTR_ => SQL Server
Credit goes to the people in this post:
Get Connection String in Azure Function v3
这是一个烦人的问题。这是我的发现:
选项1:
local.settings.json
适当的前缀
然后通过简单地调用
azure应用(或函数等)配置
将它们添加到ConnectionsStrings部分,但没有前缀
选项2:
local.settings.json, 将它们获取 Azure应用程序(或函数等),将它们获取在您的函数应用程序中。
用适当的前缀
然后通过调用
通知 ConnectionsRings 前缀将它们获取在您的功能应用程序中!这将在本地起作用,但由于额外的 ConnectionsRings 前缀而无法使用。可以用一些。
azure应用程序(或函数等)配置
在“连接环”部分中添加相同的值,但没有前缀
选项3:
请参见这个答案关于如何注入IconFiguration以读取Conn字符串
This is an annoying issue. Here are my findings:
Option 1:
local.settings.json
add your connection strings to the Values section with appropriate prefixes
then get them in your function app by simply calling
Azure App (or Function, etc.) Configuration
Add the same values to the ConnectionStrings section but without the prefixes
Option 2:
local.settings.json
add your connection strings to the ConnectionStrings section with appropriate prefixes
then get them in your function app by calling
Notice the ConnectionStrings prefix! This will work locally but not in Azure because of that extra ConnectionStrings prefix. Can be addressed with some preprocessor directives though.
Azure App (or Function, etc.) Configuration
Add the same values to the ConnectionStrings section but without the prefixes
Option 3:
See this answer on how to inject IConfiguration to read the conn strings like
中初始化配置。
遵循指令,您可以在启动 https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-depperity-indoction
在单位测试中使用以下代码来注入本地设置文件
Follow the instruction, you can initialize the Configuration in Startup
https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
Use below code in Unit tests Init to inject the local settings file