Azure Durable Functionsのアクティビティ関数でSQL DBへの挿入を行ったときの挙動メモ

## 概要
Durable Functionsを使ってDBへ値を挿入しようとしたときの挙動メモ。
DIを使うと、接続でエラー。。
DIを使わなければうまくいくけれど、その分だけコネクションを張るのが懸念。

## 結論

* 接続文字列に`MultipleActiveResultSets`が含まれておらず、MARSが有効な状態でなかった
  * 同時に複数の接続をDBに行ってエラーとなっていた。
  * `MultipleActiveResultSets=True`の文言を接続文字列に追加することで解決

## 結論までにやったこと
以下の作業を行い、DIを使ってアクティビティ関数を非同期的に動かすことができた。

* SQLの実行を非同期にする。
* アクティビティ関数を非同期で実行する。
* オーケストラ関数を非同期にし、`Task.WhenAll`でアクティビティ関数の終了を待つ
  * オーケストラ関数が先に終わり、アクティビティ関数がすべて実行されない場合があるため
* 複数のコマンドを同時に実行するMARSを有効にする
  * 接続文字列に`MultipleActiveResultSets=True`を入れる
* MARSが有効な状態でSQLの実行を同期的なソースに戻す

[うまくいったときのソース](https://github.com/hibohiboo/develop/blob/2b4102bd9a6532d476f885d29410ccf94016b19f/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)
うまくいったときの、接続文字列の設定

```json:local.settings.json
{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "SQLServerConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DurableFunctionsTest;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=True"
    }
}
```

## DIを使用して挿入を行おうとしたソース

* DIでSQLコネクションを作成して渡している。

```c#:Startup.cs
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Data.SqlClient;
using System.Configuration;
using System;

[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))]

namespace FunctionApp1
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton((s) => {
                var sqlConnection = Environment.GetEnvironmentVariable("SQLServerConnectionString");
                var con = new SqlConnection(sqlConnection);
                con.Open();
                return con;
            });
        }
    }
}
```

```C#:FunctionsApp1.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
        {
            var outputs = new List<string>();

            for(var i = 0; i<5; i++)
            {
                context.CallActivityAsync("SQLTest", i);
            }
        }

        [FunctionName("Function1_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
            [DurableClient] IDurableOrchestrationClient starter,
            ILogger log)
        {
            string instanceId = await starter.StartNewAsync("Function1", null);
            log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }
    public class SqlActivity
    {
        private readonly SqlConnection _con;
        public SqlActivity(SqlConnection con)
        {
            _con = con;
        }
        [FunctionName("SQLTest")]
        public void SQLTest([ActivityTrigger] int num, ILogger log)
        {
            log.LogInformation($"SQL start {num}.");
            var sql = "insert into Users(Id, Name) values (@Id, @Name)";

            using (SqlCommand command = new SqlCommand(sql, _con))
            {
                command.Parameters.AddWithValue("@Id", num);
                command.Parameters.AddWithValue("@Name", $"name{num}");
                command.ExecuteNonQuery();
            }
            log.LogInformation($"SQL end {num}.");
        }
    }
}
```

[ソース](https://github.com/hibohiboo/develop/blob/c9274ef86548feea30f09cf50d342b7f517dfd6b/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)
### 結果

* duplicatedになってしぬ。
* [アクティビティ関数の説明に「※アクティビティ関数は "少なくとも 1 回" の実行を保証するのみであるため、可能な限りアクティビティ関数のロジックを "べき等" にすることをお勧めします。」](https://docs.microsoft.com/ja-jp/azure/azure-functions/durable/durable-functions-types-features-overview#activity-functions)とある。

```
[2020-11-18T13:43:03.631Z] SQL start 0.
[2020-11-18T13:43:03.638Z] SQL start 2.
[2020-11-18T13:43:03.633Z] SQL start 1.
[2020-11-18T13:43:03.631Z] SQL start 4.
[2020-11-18T13:43:03.638Z] SQL start 3.
[2020-11-18T13:43:03.683Z] SQL end 2.
[2020-11-18T13:43:03.683Z] SQL end 0.
[2020-11-18T13:43:03.683Z] SQL end 4.
[2020-11-18T13:43:03.683Z] SQL end 3.
[2020-11-18T13:43:03.683Z] SQL end 1.
[2020-11-18T13:43:05.431Z] SQL start 0.
[2020-11-18T13:43:05.434Z] SQL end 0.
[2020-11-18T13:43:05.446Z] SQL start 2.
[2020-11-18T13:45:40.957Z] System.Private.CoreLib: Exception while executing function: SQLTest. Core .Net SqlClient Data Provider: Violation of PRIMARY KEY constraint 'PK__Users__3214EC07265AC78C'. Cannot insert duplicate key in object 'dbo.Users'. The duplicate key value is (0).
```

<details><summary>全文</summary><div>

```
For detailed output, run func with --verbose flag.
[2020-11-18T13:43:00.457Z] Host lock lease acquired by instance ID '00000000000000000000000089F91BFB'.
[2020-11-18T13:43:03.033Z] Executing 'Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=7186bed9-cade-4625-939b-37c3108d053a)
[2020-11-18T13:43:03.167Z] Started orchestration with ID = 'e5892c98a15a4890ba72411b07c19bf6'.
[2020-11-18T13:43:03.224Z] Executed 'Function1_HttpStart' (Succeeded, Id=7186bed9-cade-4625-939b-37c3108d053a, Duration=214ms)
[2020-11-18T13:43:03.334Z] Executing 'Function1' (Reason='(null)', Id=b60c3cc9-132f-4f34-a6ed-03a7a044ed81)
[2020-11-18T13:43:03.365Z] Executed 'Function1' (Succeeded, Id=b60c3cc9-132f-4f34-a6ed-03a7a044ed81, Duration=33ms)
[2020-11-18T13:43:03.627Z] Executing 'SQLTest' (Reason='(null)', Id=d04779c9-15b0-477f-a58c-8b0d525d55ce)
[2020-11-18T13:43:03.627Z] Executing 'SQLTest' (Reason='(null)', Id=64d0300c-5799-450d-93f5-bf35d121adb5)
[2020-11-18T13:43:03.627Z] Executing 'SQLTest' (Reason='(null)', Id=67087cbf-b3b5-4b22-9a6f-d56b9cd94f07)
[2020-11-18T13:43:03.627Z] Executing 'SQLTest' (Reason='(null)', Id=075c925a-b197-4850-8887-f06c9d543335)
[2020-11-18T13:43:03.627Z] Executing 'SQLTest' (Reason='(null)', Id=8f02088e-8181-44f0-b18b-110fe954a932)
[2020-11-18T13:43:03.631Z] SQL start 0.
[2020-11-18T13:43:03.638Z] SQL start 2.
[2020-11-18T13:43:03.633Z] SQL start 1.
[2020-11-18T13:43:03.631Z] SQL start 4.
[2020-11-18T13:43:03.638Z] SQL start 3.
[2020-11-18T13:43:03.683Z] SQL end 2.
[2020-11-18T13:43:03.683Z] SQL end 0.
[2020-11-18T13:43:03.683Z] SQL end 4.
[2020-11-18T13:43:03.687Z] Executed 'SQLTest' (Succeeded, Id=8f02088e-8181-44f0-b18b-110fe954a932, Duration=221ms)
[2020-11-18T13:43:03.689Z] Executed 'SQLTest' (Succeeded, Id=d04779c9-15b0-477f-a58c-8b0d525d55ce, Duration=253ms)
[2020-11-18T13:43:03.691Z] Executed 'SQLTest' (Succeeded, Id=64d0300c-5799-450d-93f5-bf35d121adb5, Duration=187ms)
[2020-11-18T13:43:03.683Z] SQL end 3.
[2020-11-18T13:43:03.683Z] SQL end 1.
[2020-11-18T13:43:03.697Z] Executed 'SQLTest' (Succeeded, Id=075c925a-b197-4850-8887-f06c9d543335, Duration=211ms)
[2020-11-18T13:43:03.698Z] Executed 'SQLTest' (Succeeded, Id=67087cbf-b3b5-4b22-9a6f-d56b9cd94f07, Duration=252ms)
[2020-11-18T13:43:03.799Z] Executing 'Function1' (Reason='(null)', Id=d7827e04-530c-4f43-8f45-bb7578ff7795)
[2020-11-18T13:43:03.807Z] Executed 'Function1' (Succeeded, Id=d7827e04-530c-4f43-8f45-bb7578ff7795, Duration=7ms)
[2020-11-18T13:43:05.425Z] Executing 'SQLTest' (Reason='(null)', Id=5da312a4-2e7c-47b7-b314-7f42f164c314)
[2020-11-18T13:43:05.431Z] SQL start 0.
[2020-11-18T13:43:05.434Z] SQL end 0.
[2020-11-18T13:43:05.437Z] Executed 'SQLTest' (Succeeded, Id=5da312a4-2e7c-47b7-b314-7f42f164c314, Duration=13ms)
[2020-11-18T13:43:05.443Z] Executing 'SQLTest' (Reason='(null)', Id=32a5eb0a-d571-406d-a1f7-3c4efcdcb3eb)
[2020-11-18T13:43:05.446Z] SQL start 2.
[2020-11-18T13:43:05.446Z] SQL start 2.
[2020-11-18T13:45:25.399Z] Executing 'SQLTest' (Reason='(null)', Id=f4f36297-787d-4516-8560-7726746a46a7)
[2020-11-18T13:45:25.405Z] SQL start 3.
[2020-11-18T13:45:25.484Z] Executing 'SQLTest' (Reason='(null)', Id=6b758b3f-f5fc-4f86-995b-a7ffc157e8e2)
[2020-11-18T13:45:33.833Z] SQL start 1.
[2020-11-18T13:45:33.842Z] Executing 'SQLTest' (Reason='(null)', Id=6926e3b6-38ed-4bdf-b82d-938361393a24)
[2020-11-18T13:45:36.178Z] SQL start 4.
[2020-11-18T13:45:36.192Z] Executing 'Function1' (Reason='(null)', Id=5f611484-6679-4429-aecd-0ddf5ac9835c)
[2020-11-18T13:45:36.178Z] Executing 'SQLTest' (Reason='(null)', Id=ff3ee98a-19eb-4ccd-a490-e95545cef65a)
[2020-11-18T13:45:36.205Z] Executing 'SQLTest' (Reason='(null)', Id=18305f12-2f37-4e85-9080-52eaa34e29fb)
[2020-11-18T13:45:37.711Z] Executing 'SQLTest' (Reason='(null)', Id=a321824e-c3b1-4119-b0d7-8b741898e32c)
[2020-11-18T13:45:37.730Z] SQL start 0.
[2020-11-18T13:45:37.740Z] SQL start 4.
[2020-11-18T13:45:37.759Z] Executing 'SQLTest' (Reason='(null)', Id=0d9e5d2c-27ed-4025-8b7a-571850fd813c)[2020-11-18T13:45:37.740Z] SQL start 1.[2020-11-18T13:45:37.777Z] Executed 'Function1' (Succeeded, Id=5f611484-6679-4429-aecd-0ddf5ac9835c, Duration=1576ms)[2020-11-18T13:45:38.840Z] Executing 'SQLTest' (Reason='(null)', Id=a730733b-1d00-4850-8f3e-ed1e141c4727)
[2020-11-18T13:45:39.828Z] SQL start 3.[2020-11-18T13:45:39.825Z] Executed 'SQLTest' (Failed, Id=ff3ee98a-19eb-4ccd-a490-e95545cef65a, Duration=3643ms)
[2020-11-18T13:45:39.825Z] Executed 'SQLTest' (Failed, Id=32a5eb0a-d571-406d-a1f7-3c4efcdcb3eb, Duration=154380ms)
[2020-11-18T13:45:40.957Z] Executed 'SQLTest' (Failed, Id=18305f12-2f37-4e85-9080-52eaa34e29fb, Duration=4746ms)

[2020-11-18T13:45:39.825Z] Executed 'SQLTest' (Failed, Id=6b758b3f-f5fc-4f86-995b-a7ffc157e8e2, Duration=14338ms)
[2020-11-18T13:45:40.961Z] SQL start 2.
[2020-11-18T13:45:40.957Z] System.Private.CoreLib: Exception while executing function: SQLTest. Core .Net SqlClient Data Provider: Violation of PRIMARY KEY constraint 'PK__Users__3214EC07265AC78C'. Cannot insert duplicate key in object 'dbo.Users'. The duplicate key value is (0).
```
</div>

## 冪等に修正後のソース

```C#:Functions1.cs

    public class SqlActivity
    {
        private readonly SqlConnection _con;
        public SqlActivity(SqlConnection con)
        {
            _con = con;
        }
        [FunctionName("SQLTest")]
        public void SQLTest([ActivityTrigger] int num, ILogger log)
        {
            log.LogInformation($"SQL start {num}.");
            if (ExistsUser(num))
            {
                log.LogInformation($"SQL stop {num}. already inserted");
                return;
            }
            var sql = "insert into Users(Id, Name) values (@Id, @Name)";

            using (SqlCommand command = new SqlCommand(sql, _con))
            {
                command.Parameters.AddWithValue("@Id", num);
                command.Parameters.AddWithValue("@Name", $"name{num}");
                command.ExecuteNonQuery();
            }
            log.LogInformation($"SQL end {num}.");
        }

        private bool ExistsUser(int num)
        {
            var sql = "select Id from Users where Id = @Id";
            using (SqlCommand command = new SqlCommand(sql, _con))
            {
                command.Parameters.AddWithValue("@Id", num);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("{0}", reader.GetInt32(0));
                        return reader.GetInt32(0) == num;
                    }
                }
                return false;
            }
        }
    }
```

[ソース](https://github.com/hibohiboo/develop/blob/6d1fdb14b0ab9853bf19f9dbf323fe30b0c2b3ba/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)

### 結果

`System.Private.CoreLib: Exception while executing function: SQLTest. System.Data.SqlClient: The requested operation cannot be completed because the connection has been broken.`のエラーが発生するようになった。

<details><summary>全文</summary><div>


```

For detailed output, run func with --verbose flag.
[2020-11-18T14:10:23.314Z] Host lock lease acquired by instance ID '00000000000000000000000089F91BFB'.
[2020-11-18T14:10:25.584Z] Executing 'Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=50d02f18-0db6-4008-b683-b5cbbeaef40d)
[2020-11-18T14:10:25.739Z] Started orchestration with ID = 'bbd15ca5eb494d5c83632b4fe9530fca'.
[2020-11-18T14:10:25.788Z] Executed 'Function1_HttpStart' (Succeeded, Id=50d02f18-0db6-4008-b683-b5cbbeaef40d, Duration=224ms)
[2020-11-18T14:10:25.907Z] Executing 'Function1' (Reason='(null)', Id=dcdacc8b-63dd-4091-bf04-d9c4c7697572)
[2020-11-18T14:10:25.943Z] Executed 'Function1' (Succeeded, Id=dcdacc8b-63dd-4091-bf04-d9c4c7697572, Duration=37ms)
[2020-11-18T14:10:26.199Z] Executing 'SQLTest' (Reason='(null)', Id=17c26861-a58a-430c-988e-f3307374156b)
[2020-11-18T14:10:26.199Z] Executing 'SQLTest' (Reason='(null)', Id=165f2119-26dd-43b8-9fcb-3e820d14b793)
[2020-11-18T14:10:26.199Z] Executing 'SQLTest' (Reason='(null)', Id=b20851c9-cf7e-4be6-8c67-00cb06bc0291)
[2020-11-18T14:10:26.205Z] SQL start 0.
[2020-11-18T14:10:26.199Z] Executing 'SQLTest' (Reason='(null)', Id=553fa297-3721-4665-bd25-310201f3ee62)
[2020-11-18T14:10:26.205Z] SQL start 4.
[2020-11-18T14:10:26.199Z] Executing 'SQLTest' (Reason='(null)', Id=07ca931d-7659-4777-8a66-b70ad59bbbb8)
[2020-11-18T14:10:26.214Z] SQL start 1.
[2020-11-18T14:10:26.206Z] SQL start 3.
[2020-11-18T14:10:26.217Z] SQL start 2.
[2020-11-18T14:11:10.373Z] Executed 'SQLTest' (Failed, Id=165f2119-26dd-43b8-9fcb-3e820d14b793, Duration=44317ms)
[2020-11-18T14:11:10.374Z] Executed 'SQLTest' (Failed, Id=07ca931d-7659-4777-8a66-b70ad59bbbb8, Duration=44300ms)
[2020-11-18T14:11:10.375Z] Executed 'SQLTest' (Failed, Id=553fa297-3721-4665-bd25-310201f3ee62, Duration=44332ms)
[2020-11-18T14:11:10.375Z] System.Private.CoreLib: Exception while executing function: SQLTest. System.Data.SqlClient: The requested operation cannot be completed because the connection has been broken.
[2020-11-18T14:11:10.377Z] System.Private.CoreLib: Exception while executing function: SQLTest. System.Data.SqlClient: The requested operation cannot be completed because the connection has been broken.
[2020-11-18T14:11:10.379Z] System.Private.CoreLib: Exception while executing function: SQLTest. System.Data.SqlClient: The SqlDbType enumeration value, 0, is invalid. (Parameter 'SqlDbType').
[2020-11-18T14:11:10.373Z] Executed 'SQLTest' (Failed, Id=17c26861-a58a-430c-988e-f3307374156b, Duration=44343ms)
[2020-11-18T14:11:10.387Z] System.Private.CoreLib: Exception while executing function: SQLTest. System.Data.SqlClient: Internal connection fatal error.
[2020-11-18T14:11:10.374Z] Executed 'SQLTest' (Failed, Id=b20851c9-cf7e-4be6-8c67-00cb06bc0291, Duration=44282ms)
[2020-11-18T14:11:10.387Z] bbd15ca5eb494d5c83632b4fe9530fca: Function 'SQLTest (Activity)' failed with an error. Reason: System.ArgumentOutOfRangeException: The SqlDbType enumeration value, 0, is invalid. (Parameter 'SqlDbType')
[2020-11-18T14:11:10.387Z] bbd15ca5eb494d5c83632b4fe9530fca: Function 'SQLTest (Activity)' failed with an error. Reason: System.InvalidOperationException: The requested operation cannot be completed because the connection has been broken.
[2020-11-18T14:11:10.392Z] bbd15ca5eb494d5c83632b4fe9530fca: Function 'SQLTest (Activity)' failed with an error. Reason: System.InvalidOperationException: Internal connection fatal error.
[2020-11-18T14:11:10.394Z]    at System.Data.SqlClient.MetaType.GetSqlDataType(Int32 tdsType, UInt32 userType, Int32 length)
[2020-11-18T14:11:10.396Z]    at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
[2020-11-18T14:11:10.399Z]    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
[2020-11-18T14:11:10.403Z]    at System.Data.SqlClient.TdsParser.TryCommonProcessMetaData(TdsParserStateObject stateObj, _SqlMetaData col)
[2020-11-18T14:11:10.405Z]    at System.Data.SqlClient.SqlDataReader.get_MetaData()
[2020-11-18T14:11:10.407Z]    at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
[2020-11-18T14:11:10.410Z]    at System.Data.SqlClient.TdsParser.TryProcessMetaData(Int32 cColumns, TdsParserStateObject stateObj, _SqlMetaDataSet& metaData)
[2020-11-18T14:11:10.411Z]    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
[2020-11-18T14:11:10.413Z]    at System.Data.SqlClient.SqlDataReader.get_MetaData()
[2020-11-18T14:11:10.415Z]    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
[2020-11-18T14:11:10.419Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
[2020-11-18T14:11:10.420Z]    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
[2020-11-18T14:11:10.425Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
[2020-11-18T14:11:10.392Z] System.Private.CoreLib: Exception while executing function: SQLTest. System.Data.SqlClient: The SqlDbType enumeration value, 209, is invalid. (Parameter 'SqlDbType').
[2020-11-18T14:11:10.428Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
[2020-11-18T14:11:10.430Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
[2020-11-18T14:11:10.437Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
[2020-11-18T14:11:10.436Z] bbd15ca5eb494d5c83632b4fe9530fca: Function 'SQLTest (Activity)' failed with an error. Reason: System.ArgumentOutOfRangeException: The SqlDbType enumeration value, 209, is invalid. (Parameter 'SqlDbType')
[2020-11-18T14:11:10.440Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
[2020-11-18T14:11:10.443Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
[2020-11-18T14:11:10.449Z]    at System.Data.SqlClient.MetaType.GetSqlDataType(Int32 tdsType, UInt32 userType, Int32 length)
[2020-11-18T14:11:10.450Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader()
[2020-11-18T14:11:10.452Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
[2020-11-18T14:11:10.454Z]    at System.Data.SqlClient.TdsParser.TryCommonProcessMetaData(TdsParserStateObject stateObj, _SqlMetaData col)
[2020-11-18T14:11:10.455Z]    at FunctionApp1.SqlActivity.ExistsUser(Int32 num) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 84
[2020-11-18T14:11:10.457Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader()
[2020-11-18T14:11:10.459Z]    at System.Data.SqlClient.TdsParser.TryProcessMetaData(Int32 cColumns, TdsParserStateObject stateObj, _SqlMetaDataSet& metaData)
[2020-11-18T14:11:10.460Z]    at FunctionApp1.SqlActivity.SQLTest(Int32 num, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 62
[2020-11-18T14:11:10.462Z]    at FunctionApp1.SqlActivity.ExistsUser(Int32 num) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 84
[2020-11-18T14:11:10.465Z]    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
[2020-11-18T14:11:10.467Z]    at lambda_method(Closure , SqlActivity , Object[] )
[2020-11-18T14:11:10.469Z]    at FunctionApp1.SqlActivity.SQLTest(Int32 num, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 62
[2020-11-18T14:11:10.471Z]    at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
[2020-11-18T14:11:10.472Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidMethodInvoker.cs:line 21
[2020-11-18T14:11:10.474Z]    at lambda_method(Closure , SqlActivity , Object[] )
[2020-11-18T14:11:10.475Z]    at System.Data.SqlClient.SqlDataReader.get_MetaData()
[2020-11-18T14:11:10.477Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T14:11:10.480Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidMethodInvoker.cs:line 21
[2020-11-18T14:11:10.481Z]    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
[2020-11-18T14:11:10.483Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 547
[2020-11-18T14:11:10.484Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T14:11:10.486Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
[2020-11-18T14:11:10.489Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 492
[2020-11-18T14:11:10.491Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 547
[2020-11-18T14:11:10.493Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
[2020-11-18T14:11:10.497Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 438
[2020-11-18T14:11:10.499Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 492
[2020-11-18T14:11:10.500Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
[2020-11-18T14:11:10.502Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 245. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.1.1. SequenceNumber: 16.
[2020-11-18T14:11:10.504Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 438
[2020-11-18T14:11:10.506Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
[2020-11-18T14:11:10.423Z]    at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
[2020-11-18T14:11:10.387Z] bbd15ca5eb494d5c83632b4fe9530fca: Function 'SQLTest (Activity)' failed with an error. Reason: System.InvalidOperationException: The requested operation cannot be completed because the connection has been broken.
[2020-11-18T14:11:10.514Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader()
[2020-11-18T14:11:10.513Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 245. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.1.1. SequenceNumber: 18.
[2020-11-18T14:11:10.515Z]    at System.Data.SqlClient.SqlDataReader.get_MetaData()
[2020-11-18T14:11:10.518Z]    at FunctionApp1.SqlActivity.ExistsUser(Int32 num) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 84
[2020-11-18T14:11:10.522Z]    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
[2020-11-18T14:11:10.517Z]    at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
[2020-11-18T14:11:10.528Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
[2020-11-18T14:11:10.530Z]    at System.Data.SqlClient.SqlDataReader.get_MetaData()
[2020-11-18T14:11:10.532Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
[2020-11-18T14:11:10.533Z]    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
[2020-11-18T14:11:10.535Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
[2020-11-18T14:11:10.537Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
[2020-11-18T14:11:10.524Z]    at FunctionApp1.SqlActivity.SQLTest(Int32 num, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 62
[2020-11-18T14:11:10.544Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
[2020-11-18T14:11:10.547Z]    at lambda_method(Closure , SqlActivity , Object[] )
[2020-11-18T14:11:10.549Z]    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
[2020-11-18T14:11:10.550Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidMethodInvoker.cs:line 21
[2020-11-18T14:11:10.552Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
[2020-11-18T14:11:10.555Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T14:11:10.557Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader()
[2020-11-18T14:11:10.561Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 547
[2020-11-18T14:11:10.563Z]    at FunctionApp1.SqlActivity.ExistsUser(Int32 num) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 84
[2020-11-18T14:11:10.566Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 492
[2020-11-18T14:11:10.568Z]    at FunctionApp1.SqlActivity.SQLTest(Int32 num, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 62
[2020-11-18T14:11:10.570Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 438
[2020-11-18T14:11:10.572Z]    at lambda_method(Closure , SqlActivity , Object[] )
[2020-11-18T14:11:10.577Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 245. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.1.1. SequenceNumber: 19.
[2020-11-18T14:11:10.582Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidMethodInvoker.cs:line 21
[2020-11-18T14:11:10.540Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
[2020-11-18T14:11:10.587Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T14:11:10.591Z]    at System.Data.SqlClient.SqlCommand.ExecuteReader()
[2020-11-18T14:11:10.594Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 547
[2020-11-18T14:11:10.596Z]    at FunctionApp1.SqlActivity.ExistsUser(Int32 num) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 84
[2020-11-18T14:11:10.599Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 492
[2020-11-18T14:11:10.601Z]    at FunctionApp1.SqlActivity.SQLTest(Int32 num, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 62
[2020-11-18T14:11:10.602Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 438
[2020-11-18T14:11:10.608Z]    at lambda_method(Closure , SqlActivity , Object[] )
[2020-11-18T14:11:10.611Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 245. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.1.1. SequenceNumber: 15.
[2020-11-18T14:11:10.613Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidMethodInvoker.cs:line 21
[2020-11-18T14:11:10.624Z] Executing 'Function1' (Reason='(null)', Id=6a6f16e4-040a-4d08-aca8-e2ec58ace1b8)
[2020-11-18T14:11:10.631Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T14:11:10.637Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 547
[2020-11-18T14:11:10.641Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 492
[2020-11-18T14:11:10.643Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 438
[2020-11-18T14:11:10.646Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 245. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.1.1. SequenceNumber: 17.
[2020-11-18T14:11:10.661Z] Executed 'Function1' (Succeeded, Id=6a6f16e4-040a-4d08-aca8-e2ec58ace1b8, Duration=37ms)
[2020-11-18T14:11:10.791Z] Executing 'Function1' (Reason='(null)', Id=08a733a1-8b4b-4e09-b14d-eb3316633c86)
[2020-11-18T14:11:10.810Z] Executed 'Function1' (Succeeded, Id=08a733a1-8b4b-4e09-b14d-eb3316633c86, Duration=19ms)

C:\Users\hiboh\AppData\Local\AzureFunctionsTools\Releases\3.15.0\cli_x64\func.exe (プロセス 2756) は、コード -1 で終了しました。
デバッグが停止したときに自動的にコンソールを閉じるには、[ツール] -> [オプション] -> [デバッグ] -> [デバッグの停止時に自 動的にコンソールを閉じる] を有効にします。
このウィンドウを閉じるには、任意のキーを押してください...
```

</div>

## 冪等に修正後、awaitをしたパターン

```diff
        [FunctionName("Function1")]
-        public static void RunOrchestrator(
+        public static async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
        {
            var outputs = new List<string>();

            for (var i = 0; i < 5; i++)
            {
-                context.CallActivityAsync("SQLTest", i);
+                try
+                {
+                    await context.CallActivityAsync("SQLTest", i);
+                }
+                catch { }
            }
        }
```

[ソース](https://github.com/hibohiboo/develop/blob/75df2cbd50e2a157a4d2c57087317204984cd2a1/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)
[ソース(テスト：try-catchをしないと例外がおきるとオーケストラ関数がそこで止まる）](https://github.com/hibohiboo/develop/blob/6d1fdb14b0ab9853bf19f9dbf323fe30b0c2b3ba/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)

### 実行結果

* エラーは発生しなくなったが、終了までにそれなりに時間がかかるようになった。

```
[2020-11-18T14:32:58.513Z] SQL start 0.
[2020-11-18T14:32:58.571Z] SQL stop 0. already inserted
[2020-11-18T14:32:58.711Z] SQL start 1.
```


<details><summary>全文</summary><div>

```
[2020-11-18T14:31:58.141Z] Host lock lease acquired by instance ID '00000000000000000000000089F91BFB'.
[2020-11-18T14:32:57.899Z] Executing 'Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=4209911c-b618-4ce4-98b7-8a96b23ca645)
[2020-11-18T14:32:58.038Z] Started orchestration with ID = '4e27b61b7af845e3b4c3d7568e1a987f'.
[2020-11-18T14:32:58.107Z] Executed 'Function1_HttpStart' (Succeeded, Id=4209911c-b618-4ce4-98b7-8a96b23ca645, Duration=229ms)
[2020-11-18T14:32:58.220Z] Executing 'Function1' (Reason='(null)', Id=106db686-493b-428a-934a-2ecd97c05ac9)
[2020-11-18T14:32:58.248Z] Executed 'Function1' (Succeeded, Id=106db686-493b-428a-934a-2ecd97c05ac9, Duration=30ms)
[2020-11-18T14:32:58.510Z] Executing 'SQLTest' (Reason='(null)', Id=cf73da0a-0e75-4b46-b069-db3fb0cb8fa9)
[2020-11-18T14:32:58.513Z] SQL start 0.
0
[2020-11-18T14:32:58.571Z] SQL stop 0. already inserted
[2020-11-18T14:32:58.574Z] Executed 'SQLTest' (Succeeded, Id=cf73da0a-0e75-4b46-b069-db3fb0cb8fa9, Duration=252ms)
[2020-11-18T14:32:58.662Z] Executing 'Function1' (Reason='(null)', Id=0a4ee576-2f1d-4e44-9219-c00da9e2b0cc)
[2020-11-18T14:32:58.671Z] Executed 'Function1' (Succeeded, Id=0a4ee576-2f1d-4e44-9219-c00da9e2b0cc, Duration=8ms)
[2020-11-18T14:32:58.707Z] Executing 'SQLTest' (Reason='(null)', Id=ec4126f4-e34d-47b1-8782-23f163116f5e)
[2020-11-18T14:32:58.711Z] SQL start 1.
1
[2020-11-18T14:32:58.717Z] SQL stop 1. already inserted
[2020-11-18T14:32:58.720Z] Executed 'SQLTest' (Succeeded, Id=ec4126f4-e34d-47b1-8782-23f163116f5e, Duration=14ms)
[2020-11-18T14:32:58.822Z] Executing 'Function1' (Reason='(null)', Id=05dacd09-d891-479a-a891-fd34ec828df2)
[2020-11-18T14:32:58.827Z] Executed 'Function1' (Succeeded, Id=05dacd09-d891-479a-a891-fd34ec828df2, Duration=5ms)
[2020-11-18T14:32:58.865Z] Executing 'SQLTest' (Reason='(null)', Id=50ec0d91-b6b7-46da-bdc8-9a5e85290e87)
[2020-11-18T14:32:58.869Z] SQL start 2.
2
[2020-11-18T14:32:58.872Z] SQL stop 2. already inserted
[2020-11-18T14:32:58.875Z] Executed 'SQLTest' (Succeeded, Id=50ec0d91-b6b7-46da-bdc8-9a5e85290e87, Duration=10ms)
[2020-11-18T14:32:58.983Z] Executing 'Function1' (Reason='(null)', Id=64c621a5-3d61-4015-8bce-3288a6221179)
[2020-11-18T14:32:58.991Z] Executed 'Function1' (Succeeded, Id=64c621a5-3d61-4015-8bce-3288a6221179, Duration=8ms)
[2020-11-18T14:32:59.034Z] Executing 'SQLTest' (Reason='(null)', Id=08940f1c-9fbb-41df-b3cb-2cf9655bacf9)
[2020-11-18T14:32:59.038Z] SQL start 3.
3
[2020-11-18T14:32:59.042Z] SQL stop 3. already inserted
[2020-11-18T14:32:59.045Z] Executed 'SQLTest' (Succeeded, Id=08940f1c-9fbb-41df-b3cb-2cf9655bacf9, Duration=10ms)
[2020-11-18T14:32:59.145Z] Executing 'Function1' (Reason='(null)', Id=3d54290c-6651-4fe8-a4b8-1f5ab1bef906)
[2020-11-18T14:32:59.152Z] Executed 'Function1' (Succeeded, Id=3d54290c-6651-4fe8-a4b8-1f5ab1bef906, Duration=6ms)
[2020-11-18T14:32:59.189Z] Executing 'SQLTest' (Reason='(null)', Id=505da10e-e767-4ef5-a79b-17a0624becf4)
[2020-11-18T14:32:59.192Z] SQL start 4.
4
[2020-11-18T14:32:59.195Z] SQL stop 4. already inserted
[2020-11-18T14:32:59.197Z] Executed 'SQLTest' (Succeeded, Id=505da10e-e767-4ef5-a79b-17a0624becf4, Duration=10ms)
[2020-11-18T14:32:59.285Z] Executing 'Function1' (Reason='(null)', Id=26ec7cd2-f3ef-43c5-b4ff-3efa91d8081c)
[2020-11-18T14:32:59.293Z] Executed 'Function1' (Succeeded, Id=26ec7cd2-f3ef-43c5-b4ff-3efa91d8081c, Duration=9ms)
```
</div>

## DIを使わないパターン

```c#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
        {
            var outputs = new List<string>();

            for (var i = 0; i < 5; i++)
            {
               context.CallActivityAsync("SQLTest", i);
            }
        }

        [FunctionName("Function1_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
            [DurableClient] IDurableOrchestrationClient starter,
            ILogger log)
        {
            string instanceId = await starter.StartNewAsync("Function1", null);

            log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }
    public static class SqlActivity
    {
        [FunctionName("SQLTest")]
        public static void SQLTest([ActivityTrigger] int num, ILogger log)
        {
            log.LogInformation($"SQL start {num}.");
            var sqlConnection = Environment.GetEnvironmentVariable("SQLServerConnectionString");
            using (var con = new SqlConnection(sqlConnection))
            {
                con.Open();

                if (ExistsUser(num, con))
                {
                    log.LogInformation($"SQL stop {num}. already inserted");
                    return;
                }
                var sql = "insert into Users(Id, Name) values (@Id, @Name)";

                using (SqlCommand command = new SqlCommand(sql, con))
                {
                    command.Parameters.AddWithValue("@Id", num);
                    command.Parameters.AddWithValue("@Name", $"name{num}");
                    command.ExecuteNonQuery();
                }
                log.LogInformation($"SQL end {num}.");
            }

        }

        private static bool ExistsUser(int num, SqlConnection con)
        {
            var sql = "select Id from Users where Id = @Id";
            using (SqlCommand command = new SqlCommand(sql, con))
            {
                command.Parameters.AddWithValue("@Id", num);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("{0}", reader.GetInt32(0));
                        return reader.GetInt32(0) == num;
                    }
                }
                return false;
            }
        }
    }
}
```

[ソース](https://github.com/hibohiboo/develop/blob/40f4dbecbd6c44f4243b4985d1b5b05f72d6939f/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)

### 実行結果

* 期待どおりの実行結果
* DIがなく、SQL Connectionがその分増えているので、SQL DBへの負荷が心配。

```
[2020-11-18T14:46:08.698Z] SQL start 0.
[2020-11-18T14:46:08.703Z] SQL start 1.
[2020-11-18T14:46:08.721Z] SQL start 2.
[2020-11-18T14:46:08.749Z] SQL start 3.
[2020-11-18T14:46:08.762Z] SQL start 4.
0
2
4
1
3
[2020-11-18T14:46:08.962Z] SQL stop 3. already inserted
[2020-11-18T14:46:08.962Z] SQL stop 4. already inserted
[2020-11-18T14:46:08.962Z] SQL stop 1. already inserted
[2020-11-18T14:46:08.962Z] SQL stop 0. already inserted
[2020-11-18T14:46:08.962Z] SQL stop 2. already inserted

```

<details><summary>全文</summary><div>
```

For detailed output, run func with --verbose flag.
[2020-11-18T14:46:04.039Z] Host lock lease acquired by instance ID '00000000000000000000000089F91BFB'.
[2020-11-18T14:46:08.275Z] Executing 'Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=34d150b4-1442-41ee-a668-7a933352a021)
[2020-11-18T14:46:08.411Z] Started orchestration with ID = '5734507d443f4f83b5e7192e706ce624'.
[2020-11-18T14:46:08.469Z] Executed 'Function1_HttpStart' (Succeeded, Id=34d150b4-1442-41ee-a668-7a933352a021, Duration=215ms)
[2020-11-18T14:46:08.577Z] Executing 'Function1' (Reason='(null)', Id=527f7746-c86e-455e-b697-acaa2c669481)
[2020-11-18T14:46:08.606Z] Executed 'Function1' (Succeeded, Id=527f7746-c86e-455e-b697-acaa2c669481, Duration=31ms)
[2020-11-18T14:46:08.693Z] Executing 'SQLTest' (Reason='(null)', Id=b937dad1-1355-4aba-8dd5-0f3603e1ef5a)
[2020-11-18T14:46:08.698Z] SQL start 0.
[2020-11-18T14:46:08.700Z] Executing 'SQLTest' (Reason='(null)', Id=c3e5425a-5dd8-47a3-8ba6-8bb8f9d7c217)
[2020-11-18T14:46:08.703Z] SQL start 1.
[2020-11-18T14:46:08.719Z] Executing 'SQLTest' (Reason='(null)', Id=c0c09784-db22-4c88-ae88-da36cade247c)
[2020-11-18T14:46:08.721Z] SQL start 2.
[2020-11-18T14:46:08.739Z] Executing 'SQLTest' (Reason='(null)', Id=4c4d4cf3-6ccb-49ca-8be7-34d46545f42a)
[2020-11-18T14:46:08.749Z] SQL start 3.
[2020-11-18T14:46:08.759Z] Executing 'SQLTest' (Reason='(null)', Id=c30179a6-ed56-4acf-915a-b991c88f5180)
[2020-11-18T14:46:08.762Z] SQL start 4.
0
2
4
1
3
[2020-11-18T14:46:08.962Z] SQL stop 3. already inserted
[2020-11-18T14:46:08.962Z] SQL stop 4. already inserted
[2020-11-18T14:46:08.962Z] SQL stop 1. already inserted
[2020-11-18T14:46:08.962Z] SQL stop 0. already inserted
[2020-11-18T14:46:08.962Z] SQL stop 2. already inserted
[2020-11-18T14:46:08.976Z] Executed 'SQLTest' (Succeeded, Id=4c4d4cf3-6ccb-49ca-8be7-34d46545f42a, Duration=238ms)
[2020-11-18T14:46:08.976Z] Executed 'SQLTest' (Succeeded, Id=c30179a6-ed56-4acf-915a-b991c88f5180, Duration=218ms)
[2020-11-18T14:46:08.976Z] Executed 'SQLTest' (Succeeded, Id=c0c09784-db22-4c88-ae88-da36cade247c, Duration=259ms)
[2020-11-18T14:46:08.976Z] Executed 'SQLTest' (Succeeded, Id=b937dad1-1355-4aba-8dd5-0f3603e1ef5a, Duration=286ms)
[2020-11-18T14:46:08.976Z] Executed 'SQLTest' (Succeeded, Id=c3e5425a-5dd8-47a3-8ba6-8bb8f9d7c217, Duration=277ms)
[2020-11-18T14:46:09.115Z] Executing 'Function1' (Reason='(null)', Id=dd4a29ff-f176-478c-8a8e-fb4b166ee38c)
[2020-11-18T14:46:09.126Z] Executed 'Function1' (Succeeded, Id=dd4a29ff-f176-478c-8a8e-fb4b166ee38c, Duration=10ms)
```
</div>

## Activityを非同期にした場合

```C#
    public class SqlActivity
    {
        private readonly SqlConnection _con;
        public SqlActivity(SqlConnection con)
        {
            _con = con;
        }
        [FunctionName("SQLTest")]
        public async Task SQLTest([ActivityTrigger] int num, ILogger log)
        {
            log.LogInformation($"SQL start {num}.");
            if (await ExistsUser(num))
            {
                log.LogInformation($"SQL stop {num}. already inserted");
                return;
            }
            var sql = "insert into Users(Id, Name) values (@Id, @Name)";

            using (SqlCommand command = new SqlCommand(sql, _con))
            {
                command.Parameters.AddWithValue("@Id", num);
                command.Parameters.AddWithValue("@Name", $"name{num}");
                await command.ExecuteNonQueryAsync();
            }
            log.LogInformation($"SQL end {num}.");
        }

        private async Task<bool> ExistsUser(int num)
        {
            var sql = "select Id from Users where Id = @Id";
            using (SqlCommand command = new SqlCommand(sql, _con))
            {
                command.Parameters.AddWithValue("@Id", num);
                using (SqlDataReader reader = await command.ExecuteReaderAsync())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("{0}", reader.GetInt32(0));
                        return reader.GetInt32(0) == num;
                    }
                }
                return false;
            }
        }
```

[ソース](https://github.com/hibohiboo/develop/blob/45b1f96d948dfc9155cd1dc127a456367790823e/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)



### 実行結果

* SQL エラーは出なくなった
* 1つ目の関数を行っている間にオーケストラ関数が終了して、続きが実行されなくなった。

```
[2020-11-18T15:52:16.769Z] Executing 'Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=5a21be28-eeb1-46ea-9b06-979b2bfb8382)
[2020-11-18T15:52:16.901Z] Started orchestration with ID = 'df3fc1b5dc2d44929f7caf6a0942c4c2'.
[2020-11-18T15:52:16.957Z] Executed 'Function1_HttpStart' (Succeeded, Id=5a21be28-eeb1-46ea-9b06-979b2bfb8382, Duration=186ms)
[2020-11-18T15:52:17.066Z] Executing 'Function1' (Reason='(null)', Id=2bdffcfc-226e-4fda-bcd0-5eed6506c968)
[2020-11-18T15:52:17.099Z] Executed 'Function1' (Succeeded, Id=2bdffcfc-226e-4fda-bcd0-5eed6506c968, Duration=35ms)
[2020-11-18T15:52:17.357Z] SQL start 1.
[2020-11-18T15:52:17.357Z] SQL start 2.
[2020-11-18T15:52:17.357Z] SQL start 3.
[2020-11-18T15:52:17.364Z] SQL start 0.
[2020-11-18T15:52:17.359Z] SQL start 4.
0
[2020-11-18T15:52:17.435Z] SQL stop 0. already inserted
[2020-11-18T15:52:17.546Z] Executed 'Function1' (Succeeded, Id=07f1a2bc-ac9c-4285-9c3f-ae5982f6e04d, Duration=10ms)
[2020-11-18T15:52:19.429Z] Host lock lease acquired by instance ID '00000000000000000000000089F91BFB'.
```

<details><summary>全文</summary><div>
```
For detailed output, run func with --verbose flag.
[2020-11-18T15:52:16.769Z] Executing 'Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=5a21be28-eeb1-46ea-9b06-979b2bfb8382)
[2020-11-18T15:52:16.901Z] Started orchestration with ID = 'df3fc1b5dc2d44929f7caf6a0942c4c2'.
[2020-11-18T15:52:16.957Z] Executed 'Function1_HttpStart' (Succeeded, Id=5a21be28-eeb1-46ea-9b06-979b2bfb8382, Duration=186ms)
[2020-11-18T15:52:17.066Z] Executing 'Function1' (Reason='(null)', Id=2bdffcfc-226e-4fda-bcd0-5eed6506c968)
[2020-11-18T15:52:17.099Z] Executed 'Function1' (Succeeded, Id=2bdffcfc-226e-4fda-bcd0-5eed6506c968, Duration=35ms)
[2020-11-18T15:52:17.347Z] Executing 'SQLTest' (Reason='(null)', Id=3f76d1ad-63be-4551-9bc6-d0a616376080)
[2020-11-18T15:52:17.347Z] Executing 'SQLTest' (Reason='(null)', Id=b9c67eae-f863-4c5f-a658-34757097c9b5)
[2020-11-18T15:52:17.347Z] Executing 'SQLTest' (Reason='(null)', Id=eea56815-499d-4c0a-b42d-b4b5f1177463)
[2020-11-18T15:52:17.347Z] Executing 'SQLTest' (Reason='(null)', Id=91d29fea-a9f6-4528-be6a-a14a06eb10a1)
[2020-11-18T15:52:17.347Z] Executing 'SQLTest' (Reason='(null)', Id=6c161c06-13f4-4e06-8822-581f65bd1a8f)
[2020-11-18T15:52:17.357Z] SQL start 1.
[2020-11-18T15:52:17.357Z] SQL start 2.
[2020-11-18T15:52:17.357Z] SQL start 3.
[2020-11-18T15:52:17.364Z] SQL start 0.
[2020-11-18T15:52:17.359Z] SQL start 4.
0
[2020-11-18T15:52:17.435Z] SQL stop 0. already inserted
[2020-11-18T15:52:17.439Z] Executed 'SQLTest' (Succeeded, Id=6c161c06-13f4-4e06-8822-581f65bd1a8f, Duration=269ms)
[2020-11-18T15:52:17.535Z] Executing 'Function1' (Reason='(null)', Id=07f1a2bc-ac9c-4285-9c3f-ae5982f6e04d)
[2020-11-18T15:52:17.546Z] Executed 'Function1' (Succeeded, Id=07f1a2bc-ac9c-4285-9c3f-ae5982f6e04d, Duration=10ms)
[2020-11-18T15:52:19.429Z] Host lock lease acquired by instance ID '00000000000000000000000089F91BFB'.
```
</div>

## Activityを非同期にして待つ場合

```C#
        public static async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
        {
            var tasks = new List<Task>();

            for (var i = 0; i < 5; i++)
            {
                 tasks.Add(context.CallActivityAsync("SQLTest", i));
            }
            await Task.WhenAll(tasks);
        }
```
[ソース](https://github.com/hibohiboo/develop/blob/2b4102bd9a6532d476f885d29410ccf94016b19f/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)



### 実行結果
* `The connection does not support MultipleActiveResultSets`のエラーが出るようになった。

```
[2020-11-18T16:06:11.887Z] Executing 'SQLTest' (Reason='(null)', Id=9c7f112d-3bd3-4a4a-83ef-1e3f40e564e1)
[2020-11-18T16:06:11.893Z] SQL start 1.
[2020-11-18T16:06:11.893Z] SQL start 3.
[2020-11-18T16:06:11.893Z] SQL start 0.
[2020-11-18T16:06:11.899Z] SQL start 4.
[2020-11-18T16:06:11.894Z] SQL start 2.
0
[2020-11-18T16:06:12.076Z] Executed 'SQLTest' (Failed, Id=04af42e7-2192-4c0d-8d8a-ab87bdd8f650, Duration=296ms)
[2020-11-18T16:06:12.078Z] System.Private.CoreLib: Exception while executing function: SQLTest. System.Data.SqlClient: The connection does not support MultipleActiveResultSets.
```


## Multiple を Trueにした場合

```diff:local.settings.json
{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
-        "SQLServerConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DurableFunctionsTest;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
+        "SQLServerConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DurableFunctionsTest;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=True"

    }
}
```

### 実行結果
* 期待している結果となった。

```
[2020-11-18T16:17:51.746Z] Started orchestration with ID = '11f04d9c376e4c5b8019d54e4011a81d'.
[2020-11-18T16:17:51.812Z] Executed 'Function1_HttpStart' (Succeeded, Id=1e66a6d3-a569-48f1-83e5-a7bbde965107, Duration=229ms)
[2020-11-18T16:17:52.216Z] SQL start 1.
[2020-11-18T16:17:52.216Z] SQL start 4.
[2020-11-18T16:17:52.223Z] SQL start 0.
[2020-11-18T16:17:52.223Z] SQL start 3.
[2020-11-18T16:17:52.216Z] SQL start 2.
[2020-11-18T16:17:52.296Z] SQL stop 0. already inserted
[2020-11-18T16:17:52.296Z] SQL stop 1. already inserted
[2020-11-18T16:17:52.296Z] SQL stop 3. already inserted[2020-11-18T16:17:52.296Z] SQL stop 4. already inserted
[2020-11-18T16:17:52.296Z] SQL stop 2. already inserted
[2020-11-18T16:17:52.410Z] Executing 'Function1' (Reason='(null)', Id=7c508b6b-b80b-43c7-8bbd-a20c6e8105bb)
[2020-11-18T16:17:52.420Z] Executed 'Function1' (Succeeded, Id=7c508b6b-b80b-43
```

## 上記で例外が発生するときの挙動を確認

```diff
            [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
        {
            var tasks = new List<Task>();
-            for (var i = 0; i < 5; i++)
+            for (var i = 0; i < 10; i++)
```


```diff
        public async Task SQLTest([ActivityTrigger] int num, ILogger log)
        {
            log.LogInformation($"SQL start {num}.");
+            if (num == 0 || num == 6) throw new Exception();
```

[ソース](https://github.com/hibohiboo/develop/blob/410f954318d2d93e7d5abda9cfaaac6e0f85de0f/tutorial/lesson/ms-functions/durable-functions/FunctionApp1/FunctionApp1/Function1.cs)

### 結果
* 1-4は既に挿入されている状態で実施


```
[2020-11-18T16:25:22.067Z] SQL start 0.
[2020-11-18T16:25:22.067Z] SQL start 1.
[2020-11-18T16:25:22.067Z] SQL start 9.
[2020-11-18T16:25:22.067Z] SQL start 3.
[2020-11-18T16:25:22.067Z] SQL start 5.
[2020-11-18T16:25:22.069Z] SQL start 8.
[2020-11-18T16:25:22.067Z] SQL start 4.
[2020-11-18T16:25:22.072Z] SQL start 7.
[2020-11-18T16:25:22.067Z] SQL start 2.
[2020-11-18T16:25:22.157Z] System.Private.CoreLib: Exception while executing function: SQLTest. FunctionApp1: Exception of type 'System.Exception' was thrown.
[2020-11-18T16:25:22.201Z] SQL stop 4. already inserted
[2020-11-18T16:25:22.203Z] SQL stop 1. already inserted
[2020-11-18T16:25:22.202Z] SQL stop 2. already inserted
[2020-11-18T16:25:22.204Z] SQL stop 3. already inserted
[2020-11-18T16:25:22.207Z] SQL start 6.
[2020-11-18T16:25:22.211Z] SQL end 5.
[2020-11-18T16:25:22.211Z] SQL end 7.
[2020-11-18T16:25:22.243Z] SQL end 9.
[2020-11-18T16:25:22.211Z] SQL end 8.
```

* DBにも、0番と6番が入っていない状態で格納されている
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/99876/751b41cc-8d2b-99d8-a87f-2e66d6906775.png)

<details><summary>全文</summary><div>

```
For detailed output, run func with --verbose flag.
[2020-11-18T16:25:21.417Z] Executing 'Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=3d09fd40-98b8-4c1d-a900-1feed3e0f30d)
[2020-11-18T16:25:21.551Z] Started orchestration with ID = 'c4d23c8aefe04c869f2fb93adbd0ed1c'.
[2020-11-18T16:25:21.608Z] Executed 'Function1_HttpStart' (Succeeded, Id=3d09fd40-98b8-4c1d-a900-1feed3e0f30d, Duration=195ms)
[2020-11-18T16:25:21.727Z] Executing 'Function1' (Reason='(null)', Id=58dcd520-4f00-4613-b5aa-7711f08ffc9d)
[2020-11-18T16:25:21.761Z] Executed 'Function1' (Succeeded, Id=58dcd520-4f00-4613-b5aa-7711f08ffc9d, Duration=35ms)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=13ec5fe5-e8a7-4176-8f53-fd906b77a049)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=2746ddf9-c261-429a-ac33-92dd0df47902)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=a1ca4073-67fb-46c4-9cf8-19ea033789c7)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=d01063af-180e-4a86-88b9-e1b86621a38f)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=1a29f7a9-5bab-4653-9182-48bbf2c1b3e9)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=5f3e768e-ff6e-41d0-84c6-554be2afdc62)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=ae0baa7e-1c9c-4855-a134-642ed33a5dd4)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=e72443f9-23f5-44af-8616-92af804117fc)
[2020-11-18T16:25:22.049Z] Executing 'SQLTest' (Reason='(null)', Id=5cb586d2-ad46-470d-98f8-f40b06e3e2bb)
[2020-11-18T16:25:22.064Z] Executing 'SQLTest' (Reason='(null)', Id=ae455242-658b-4115-a0d0-c56f71ad6d97)
[2020-11-18T16:25:22.067Z] SQL start 0.
[2020-11-18T16:25:22.067Z] SQL start 1.
[2020-11-18T16:25:22.067Z] SQL start 9.
[2020-11-18T16:25:22.067Z] SQL start 3.
[2020-11-18T16:25:22.067Z] SQL start 5.
[2020-11-18T16:25:22.069Z] SQL start 8.
[2020-11-18T16:25:22.067Z] SQL start 4.
[2020-11-18T16:25:22.072Z] SQL start 7.
[2020-11-18T16:25:22.067Z] SQL start 2.
[2020-11-18T16:25:22.155Z] Executed 'SQLTest' (Failed, Id=a1ca4073-67fb-46c4-9cf8-19ea033789c7, Duration=300ms)
[2020-11-18T16:25:22.157Z] System.Private.CoreLib: Exception while executing function: SQLTest. FunctionApp1: Exception of type 'System.Exception' was thrown.
[2020-11-18T16:25:22.163Z] c4d23c8aefe04c869f2fb93adbd0ed1c: Function 'SQLTest (Activity)' failed with an error. Reason: System.Exception: Exception of type 'System.Exception' was thrown.
[2020-11-18T16:25:22.164Z]    at FunctionApp1.SqlActivity.SQLTest(Int32 num, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 65
[2020-11-18T16:25:22.167Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidTaskMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidTaskMethodInvoker.cs:line 21
[2020-11-18T16:25:22.171Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T16:25:22.173Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 547
[2020-11-18T16:25:22.175Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 492
[2020-11-18T16:25:22.177Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 438
[2020-11-18T16:25:22.181Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 245. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.1.1. SequenceNumber: 25.
4
2
1
3
[2020-11-18T16:25:22.201Z] SQL stop 4. already inserted
[2020-11-18T16:25:22.203Z] SQL stop 1. already inserted
[2020-11-18T16:25:22.202Z] SQL stop 2. already inserted
[2020-11-18T16:25:22.204Z] SQL stop 3. already inserted
[2020-11-18T16:25:22.207Z] Executed 'SQLTest' (Succeeded, Id=ae0baa7e-1c9c-4855-a134-642ed33a5dd4, Duration=291ms)
[2020-11-18T16:25:22.207Z] SQL start 6.
[2020-11-18T16:25:22.211Z] SQL end 5.
[2020-11-18T16:25:22.211Z] SQL end 7.
[2020-11-18T16:25:22.241Z] Executed 'SQLTest' (Succeeded, Id=d01063af-180e-4a86-88b9-e1b86621a38f, Duration=308ms)
[2020-11-18T16:25:22.212Z] Executed 'SQLTest' (Succeeded, Id=2746ddf9-c261-429a-ac33-92dd0df47902, Duration=314ms)
[2020-11-18T16:25:22.243Z] Executed 'SQLTest' (Succeeded, Id=5cb586d2-ad46-470d-98f8-f40b06e3e2bb, Duration=275ms)
[2020-11-18T16:25:22.208Z] Executed 'SQLTest' (Succeeded, Id=13ec5fe5-e8a7-4176-8f53-fd906b77a049, Duration=346ms)
[2020-11-18T16:25:22.243Z] SQL end 9.
[2020-11-18T16:25:22.211Z] SQL end 8.[2020-11-18T16:25:22.262Z] Executed 'SQLTest' (Succeeded, Id=5f3e768e-ff6e-41d0-84c6-554be2afdc62, Duration=278ms)

[2020-11-18T16:25:22.210Z] Executed 'SQLTest' (Succeeded, Id=1a29f7a9-5bab-4653-9182-48bbf2c1b3e9, Duration=331ms)
[2020-11-18T16:25:22.243Z] Executed 'SQLTest' (Failed, Id=ae455242-658b-4115-a0d0-c56f71ad6d97, Duration=179ms)
[2020-11-18T16:25:22.273Z] Executed 'SQLTest' (Succeeded, Id=e72443f9-23f5-44af-8616-92af804117fc, Duration=319ms)
[2020-11-18T16:25:22.277Z] System.Private.CoreLib: Exception while executing function: SQLTest. FunctionApp1: Exception of type 'System.Exception' was thrown.
[2020-11-18T16:25:22.286Z] c4d23c8aefe04c869f2fb93adbd0ed1c: Function 'SQLTest (Activity)' failed with an error. Reason: System.Exception: Exception of type 'System.Exception' was thrown.
[2020-11-18T16:25:22.289Z]    at FunctionApp1.SqlActivity.SQLTest(Int32 num, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 65
[2020-11-18T16:25:22.292Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidTaskMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidTaskMethodInvoker.cs:line 21
[2020-11-18T16:25:22.299Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T16:25:22.302Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 547
[2020-11-18T16:25:22.306Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 492
[2020-11-18T16:25:22.309Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 438
[2020-11-18T16:25:22.319Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 245. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.1.1. SequenceNumber: 34.
[2020-11-18T16:25:22.366Z] Host lock lease acquired by instance ID '00000000000000000000000089F91BFB'.
[2020-11-18T16:25:22.376Z] Executing 'Function1' (Reason='(null)', Id=533ac4c7-9206-4613-a34b-1b7f05c1cc4f)
[2020-11-18T16:25:22.408Z] Executed 'Function1' (Succeeded, Id=533ac4c7-9206-4613-a34b-1b7f05c1cc4f, Duration=32ms)
[2020-11-18T16:25:22.589Z] Executing 'Function1' (Reason='(null)', Id=bf5bdf3e-b3b6-40eb-a852-e66d12dad3f8)
[2020-11-18T16:25:22.728Z] c4d23c8aefe04c869f2fb93adbd0ed1c: Function 'Function1 (Orchestrator)' failed with an error. Reason: Microsoft.Azure.WebJobs.Extensions.DurableTask.FunctionFailedException: The activity function 'SQLTest' failed: "Exception of type 'System.Exception' was thrown.". See the function execution logs for additional details.
[2020-11-18T16:25:22.731Z]  ---> System.Exception: Exception of type 'System.Exception' was thrown.
[2020-11-18T16:25:22.734Z]    at FunctionApp1.SqlActivity.SQLTest(Int32 num, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 65
[2020-11-18T16:25:22.735Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidTaskMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidTaskMethodInvoker.cs:line 21
[2020-11-18T16:25:22.738Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T16:25:22.740Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 547
[2020-11-18T16:25:22.742Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 492
[2020-11-18T16:25:22.744Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 438
[2020-11-18T16:25:22.746Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 245
[2020-11-18T16:25:22.748Z]    --- End of inner exception stack trace ---
[2020-11-18T16:25:22.750Z]    at Microsoft.Azure.WebJobs.Extensions.DurableTask.DurableOrchestrationContext.CallDurableTaskFunctionAsync[TResult](String functionName, FunctionType functionType, Boolean oneWay, String instanceId, String operation, RetryOptions retryOptions, Object input, Nullable`1 scheduledTimeUtc) in d:\a\r1\a\azure-functions-durable-extension\src\WebJobs.Extensions.DurableTask\ContextImplementations\DurableOrchestrationContext.cs:line 617
[2020-11-18T16:25:22.753Z]    at FunctionApp1.Function1.RunOrchestrator(IDurableOrchestrationContext context, ILogger log) in D:\develop\develop\tutorial\lesson\ms-functions\durable-functions\FunctionApp1\FunctionApp1\Function1.cs:line 25
[2020-11-18T16:25:22.757Z]    at Microsoft.Azure.WebJobs.Host.Executors.VoidTaskMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidTaskMethodInvoker.cs:line 21
[2020-11-18T16:25:22.759Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2020-11-18T16:25:22.761Z]    at Microsoft.Azure.WebJobs.Extensions.DurableTask.TaskOrchestrationShim.Execute(OrchestrationContext innerContext, String serializedInput) in d:\a\r1\a\azure-functions-durable-extension\src\WebJobs.Extensions.DurableTask\Listener\TaskOrchestrationShim.cs:line 86. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.1.1. SequenceNumber: 36.
[2020-11-18T16:25:22.775Z] Executed 'Function1' (Failed, Id=bf5bdf3e-b3b6-40eb-a852-e66d12dad3f8, Duration=186ms)
[2020-11-18T16:25:22.778Z] System.Private.CoreLib: Exception while executing function: Function1. System.Private.CoreLib: Orchestrator function 'Function1' failed: The activity function 'SQLTest' failed: "Exception of type 'System.Exception' was thrown.". See the function execution logs for additional details.
```

</div>


## Multiple を Trueにして同期メソッドにした場合

```cs
namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
        {
            log = context.CreateReplaySafeLogger(log);
            var tasks = new List<Task>();

            for (var i = 0; i < 10; i++) {
                 tasks.Add(context.CallActivityAsync("SQLTest", i));
            }
            try {
                await Task.WhenAll(tasks);
            } catch (Exception e) {
                log.LogError($"Error RunOrchestrator: ", e);
            }
        }

        [FunctionName("Function1_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
            [DurableClient] IDurableOrchestrationClient starter,
            ILogger log)
        {
            string instanceId = await starter.StartNewAsync("Function1", null);
            log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }
    public class SqlActivity
    {
        private readonly SqlConnection _con;
        public SqlActivity(SqlConnection con)
        {
            _con = con;
        }
        [FunctionName("SQLTest")]
        public void SQLTest([ActivityTrigger] int num, ILogger log)
        {
            log.LogInformation($"SQL start {num}.");
            if (num == 0 || num == 6) throw new Exception();
            if (ExistsUser(num))
            {
                log.LogInformation($"SQL stop {num}. already inserted");
                return;
            }
            var sql = "insert into Users(Id, Name) values (@Id, @Name)";

            using (SqlCommand command = new SqlCommand(sql, _con))
            {
                command.Parameters.AddWithValue("@Id", num);
                command.Parameters.AddWithValue("@Name", $"name{num}");
                command.ExecuteNonQuery();
            }
            log.LogInformation($"SQL end {num}.");
        }

        private bool ExistsUser(int num)
        {
            var sql = "select Id from Users where Id = @Id";
            using (SqlCommand command = new SqlCommand(sql, _con))
            {
                command.Parameters.AddWithValue("@Id", num);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("{0}", reader.GetInt32(0));
                        return reader.GetInt32(0) == num;
                    }
                }
                return false;
            }
        }
    }
}
```


### 結果

```
For detailed output, run func with --verbose flag.
[2020-11-25T12:01:19.892Z] Executing 'Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=50651cdc-aff7-42c7-9b7a-01eb0571a98d)
[2020-11-25T12:01:20.054Z] Started orchestration with ID = '85c4e5d2d6fc4fd7a028c0a8dbab5c69'.
[2020-11-25T12:01:20.114Z] Executed 'Function1_HttpStart' (Succeeded, Id=50651cdc-aff7-42c7-9b7a-01eb0571a98d, Duration=243ms)
[2020-11-25T12:01:20.614Z] SQL start 1.
[2020-11-25T12:01:20.614Z] SQL start 2.
[2020-11-25T12:01:20.614Z] SQL start 6.
[2020-11-25T12:01:20.614Z] SQL start 0.
[2020-11-25T12:01:20.615Z] SQL start 4.
[2020-11-25T12:01:22.949Z] SQL start 5.
[2020-11-25T12:01:22.931Z] SQL start 3.
[2020-11-25T12:01:22.950Z] SQL start 7.
[2020-11-25T12:01:24.120Z] SQL end 7.
[2020-11-25T12:01:24.121Z] SQL end 5.
[2020-11-25T12:01:24.120Z] SQL end 2.
[2020-11-25T12:01:24.136Z] SQL start 9.
[2020-11-25T12:01:24.153Z] SQL end 9.
[2020-11-25T12:01:24.145Z] SQL start 8.
[2020-11-25T12:01:24.120Z] SQL end 3.
[2020-11-25T12:01:24.193Z] SQL end 8.
[2020-11-25T12:01:24.121Z] SQL end 4.
[2020-11-25T12:01:24.705Z] Error RunOrchestrator:
[2020-11-25T12:01:24.713Z] Executed 'Function1' (Succeeded, Id=ee565a84-18ef-4bd2-bd52-73f667740056, Duration=124ms)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/99876/15c430fb-859e-9e0b-f01d-b373848457ca.png)


## 参考
[Durable Functions概要](https://docs.microsoft.com/ja-jp/azure/azure-functions/durable/durable-functions-types-features-overview)
[DI](https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-dotnet-dependency-injection)
[Azure Functions でも appsettings.json と User Secrets を使った設定とシークレットの管理を行う](https://blog.shibayan.jp/entry/20190921/1569050986)
[MultipleDataSets](https://docs.microsoft.com/ja-jp/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.multipleactiveresultsets?view=dotnet-plat-ext-5.0&viewFallbackFrom=netcore-3.1)
[when all exception](https://stackoverrun.com/ja/q/10906118)