我目前正在尝试连接 Azure Function,但未能成功。我创建了一个带有数据库的 SQL Server。
我还创建了一个Azure函数,我配置了cors以允许所有来源,我已经在我的Azure函数中设置了数据库的连接字符串,我拥有所有环境变量,例如
DB_USER
,DB_PASSWORD
等。设置完毕。
我在 SQL Server 中添加了 Function 应用程序的所有输出 IP,我还启用了它,以便所有 Azure 资源都可以使用它,我什至将我的 Azure 函数添加为 Microsoft Entra 管理员。
我使用 kudu 控制台安装了 SQL Server。
没有任何效果 - 我似乎总是收到没有日志的 500 错误。
这是我的
HttpTrigger
的代码,我只需要使用Azure门户编辑器,我无法在VS Code中创建另一个项目来发布,我该怎么办?
const sql = require('mssql');
module.exports = async function (context, req) {
try {
// Log that the function has started
context.log('Function execution started.');
// Set up connection to Azure SQL Database
const config = {
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
server: process.env.DB_SERVER,
database: process.env.DB_DATABASE,
options: {
encrypt: true,
enableArithAbort: true
}
};
// Connect to SQL Server
await sql.connect(config);
context.log('Successfully connected to SQL Server.');
// Send a success message
context.res = {
status: 200,
body: "Successfully connected to the database!" // Success message
};
} catch (err) {
// Log the error for debugging
context.log.error('Error occurred:', err);
// Return a 500 error with a message
context.res = {
status: 500,
body: `Error connecting to the database: ${err.message}` // Error message
};
} finally {
context.log('Function execution completed.');
}
};
这是我收到的错误
执行“Functions.GetGptwai”(原因=“此函数是通过主机 API 以编程方式调用的。”,Id=)
发送调用 ID:'
发布调用 id:在workerId:[错误]执行“Functions.GetGptwai”(失败,Id=,持续时间=1ms)
我尝试允许cors中的所有来源,将我的Azure功能设置为管理员,将所有ip添加到白名单
按照以下步骤将Azure函数应用程序连接到Azure SQL数据库,请参阅MSDOC获取代码。
我的功能代码:
const { app, input } = require('@azure/functions');
const sqlInput = input.sql({
commandText: 'select [Id], [order], [title], [url], [completed] from dbo.ToDo',
commandType: 'Text',
connectionStringSetting: 'SqlConnectionString',
});
app.http('httpTrigger1', {
methods: ['GET'],
authLevel: 'anonymous',
extraInputs: [sqlInput],
handler: (request, context) => {
context.log('HTTP trigger and SQL input binding function processed a request.');
const toDoItems = context.extraInputs.get(sqlInput);
return {
jsonBody: toDoItems,
};
},
});
在
Function App=>Environment Variables=>App Settings
中添加SQL服务器连接字符串:
"SqlConnectionString":"Server=tcp:servername.database.windows.net,1433;Initial Catalog=rkdb;Persist Security Info=False;User ID={Username};Password={Password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;"
导航到 SQL 服务器 => 访问控制 (IAM)=> 将
Sql Server contributor
角色分配给您的用户 ID 或服务主体。
运行部署的函数:
功能应用程序日志:
2024-12-06T10:56:28Z [Verbose] AuthenticationScheme: WebJobsAuthLevel was successfully authenticated.
2024-12-06T10:56:28Z [Verbose] AuthenticationScheme: Bearer was not authenticated.
2024-12-06T10:56:28Z [Verbose] Authorization was successful.
2024-12-06T10:56:28Z [Information] Executing 'Functions.httpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=aa8cf697-fd96-499c-90a9-0c7cb310b76d)
2024-12-06T10:56:28Z [Verbose] Sending event Create
2024-12-06T10:56:30Z [Verbose] Sending event Convert
2024-12-06T10:56:30Z [Information] 1 row(s) queried from database: rkdb using Command: select [Id], [order], [title], [url], [completed] from dbo.ToDo
2024-12-06T10:56:30Z [Verbose] Sending invocation id: 'aa8cf697-XX499c-90a9-0c7cb310b76d
2024-12-06T10:56:30Z [Verbose] Posting invocation id:aa8cf697-fd9XX9-0c7cb310b76d on workerId:4b6649e1-XX-b666-0abca6973c96
2024-12-06T10:56:30Z [Information] HTTP trigger and SQL input binding function processed a request.
2024-12-06T10:56:30Z [Information] Executed 'Functions.httpTrigger1' (Succeeded, Id=aa8cf697-fd96-XXb310b76d, Duration=1628ms)
回复: