0
0

Azure Functionsをローカル実行してローカルのSQL Server(Docker)に接続したメモ

Last updated at Posted at 2024-04-27

概要

Azure Functionsをローカル実行して、前回作ったローカル実行されているDocker上のSQL Serverに接続したメモ。

ソース

接続文字列はlocalhostを使わずに、127.0.0.1を使う。

local.settings.json
{
  // 省略
  "Values": {
   // 省略
+    "MyConnectionString": "Server=127.0.0.1,1433;Database=quickstart;User Id=SA;Password=<YourStrong@Passw0rd>;Encrypt=True;TrustServerCertificate=True;Connection Timeout=10;"
  }
}

src/functions/rankCharacter.ts
import {
  app,
  HttpRequest,
  HttpResponseInit,
  InvocationContext,
  output,
} from '@azure/functions';

const sqlOutput = output.sql({
  commandText: '[dbo].[Test]',
  connectionStringSetting: 'MyConnectionString',
});

export async function rankCharacter(
  request: HttpRequest,
  context: InvocationContext,
): Promise<HttpResponseInit> {
  const characterId = request.params.characterId;
  const {  data } = (await request.json()) as {
    data: string;
  };
  const ret = context.extraOutputs.set(sqlOutput, {
    id: characterId,
    data,
    updated: new Date(),
  });
  return {
    status: 201,
  };
}

app.http('rankCharacter', {
  methods: ['PUT'],
  authLevel: 'anonymous',
  route: 'rank-character/{characterId:alpha}',
  extraOutputs: [sqlOutput],
  handler: rankCharacter,
});

error

対象のコンピューターによって拒否されたため、接続できませんでした。

"MyConnectionString": "Server=localhost,1433;Database=quickstart;User Id=SA;Password=<YourStrong@Passw0rd>;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;"の時のエラー。localhostだめなの?

 System.Private.CoreLib: Exception while executing function:
 System.Private.CoreLib: One or more errors occurred. (A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0

信頼されていない機関によって証明書チェーンが発行されました。

"MyConnectionString": "Server=127.0.0.1,1433;Database=quickstart;User Id=SA;Password=MyPassword123;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
前回同様、TrustServerCertificate=Trueの設定値にする。

 System.Private.CoreLib: Exception while executing function:
System.Private.CoreLib: One or more errors occurred. (The default values for encryption on connections have been changed, please review your configuration to ensure you have the correct values for your server. See https://aka.ms/afsqlext-connection for more details.). Microsoft.Azure.WebJobs.Extensions.Sql: The default values for encryption on connections have been changed, please review your configuration to ensure you have the correct values for your server. See https://aka.ms/afsqlext-connection for more details. Core Microsoft SqlClient Data Provider: A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0

( 2024.05.19 追記 ) 32bitのメッセージが出ていたことを対応したメモ

ローカル実行はできているが、下記のエラーメッセージが出ていた。

File 'C:\Program Files (x86)\dotnet\dotnet.exe' is not found, 'dotnet' invocation will rely on the PATH environment variable.

ぐぐると、dotnetがない場合に出る模様。入っているか試してみる。

$ dotnet --version
8.0.300

インストールされているじゃん。パスも通っている。

$ where dotnet
C:\Program Files\dotnet\dotnet.exe

ここまできて、前回 --params "'/x64'"オプションをつけていたことを思い出した。

案の定、32bitになっている

Azure Functions Core Tools
Core Tools Version:       4.0.5801 Commit hash: N/A +5ac2f09758b98257e728dd1b5576ce5ea9ef68ff (32-bit)
Function Runtime Version: 4.34.1.22669

あらためて、インストールしなおしてみる。

choco uninstall -y azure-functions-core-tools
choco install -y azure-functions-core-tools --params "'/x64'"

無事64bit版がインストールされ、エラーメッセージもなくなった。

Azure Functions Core Tools
Core Tools Version:       4.0.5801 Commit hash: N/A +5ac2f09758b98257e728dd1b5576ce5ea9ef68ff (64-bit)
Function Runtime Version: 4.34.1.22669

chocolateyでも64bitをデフォルトにしませんかって言っている人いますね。

参考

Azure Functions の HTTP トリガー
Microsoft SQL Server (MSSQL) ストレージ プロバイダーを使用して Durable Functions を構成する
接続文字列の書き方をまとめてみる(2014 年版)

Azure 向け開発のためのローカルリソース準備
SQL Server Linux コンテナーをデプロイして接続する
Azure Functions の Azure SQL 出力バインド
Azure Functions をローカルでコーディングしてテストする
Azure Functions から SQL Database に書き込む (ポータル版とVS版)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0