面对使用可持续的 Azure 函数将文档写入 Cosmos DB 时出现的问题。

huangapple go评论123阅读模式
英文:

Facing Issue writing document to Cosmos DB using Durable Azure Function

问题

I am having a chaining durable Azure function in which I want to write documents to Cosmos DB. I am running the Isolated mode. My function looks like that:

  1. [Function(nameof(ScanLinksAndInsertIntoLinksContainer))]
  2. [CosmosDBOutput(databaseName: "dpubot", containerName: "test", Connection= "CosmosConnection"
  3. , CreateIfNotExists = true, PartitionKey = "id")]
  4. public static MultiResponse ScanLinksAndInsertIntoLinksContainer
  5. ([ActivityTrigger] string name, FunctionContext executionContext)
  6. {
  7. var message = "Life is short";
  8. return new MultiResponse()
  9. {
  10. Document = new MyDocument
  11. {
  12. id = System.Guid.NewGuid().ToString(),
  13. message = message
  14. },
  15. };
  16. }

I am getting an error:

  1. Executed 'Functions.ScanLinksAndInsertIntoLinksContainer' (Failed, Id=635107bb-afc7-46e6-ac6b-5e4454f07254, Duration=2040ms)
  2. [2023-07-17T08:26:59.443Z] System.Private.CoreLib: Exception while executing function: Functions.ScanLinksAndInsertIntoLinksContainer. Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 94096d64-1801-4193-a0a2-2a7723eac59f; Reason: (Message: {"Errors":["One of the specified inputs is invalid"]}

My Host.json looks like:

  1. "logging": {
  2. "applicationInsights": {
  3. "samplingSettings": {
  4. "isEnabled": true,
  5. "excludedTypes": "Request"
  6. }
  7. },
  8. "extensions": {
  9. "cosmosDB": {
  10. "connectionMode": "Gateway",
  11. "userAgentSuffix": "MyDesiredUserAgentStamp"
  12. }
  13. }
  14. }

My local setting json file looks like:

  1. "Values": {
  2. "AzureWebJobsStorage": "UseDevelopmentStorage=true",
  3. "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
  4. "CosmosConnection": ""
  5. }
英文:

I am having a chaining durable Azure function in which I want to write documents to Cosmos DB. I am running the Isolated mode. My function looks like that:

  1. [Function(nameof(ScanLinksAndInsertIntoLinksContainer))]
  2. [CosmosDBOutput(databaseName: "dpubot", containerName: "test", Connection= "CosmosConnection"
  3. , CreateIfNotExists = true, PartitionKey = "id")]
  4. public static MultiResponse ScanLinksAndInsertIntoLinksContainer
  5. ([ActivityTrigger] string name, FunctionContext executionContext)
  6. {
  7. var message = "Life is short";
  8. return new MultiResponse()
  9. {
  10. Document = new MyDocument
  11. {
  12. id = System.Guid.NewGuid().ToString(),
  13. message = message
  14. },
  15. };
  16. }

I am getting an error:

  1. Executed 'Functions.ScanLinksAndInsertIntoLinksContainer' (Failed, Id=635107bb-afc7-46e6-ac6b-5e4454f07254, Duration=2040ms)
  2. [2023-07-17T08:26:59.443Z] System.Private.CoreLib: Exception while executing function: Functions.ScanLinksAndInsertIntoLinksContainer. Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 94096d64-1801-4193-a0a2-2a7723eac59f; Reason: (Message: {"Errors":["One of the specified inputs is invalid"]}

My Host.json looks like

  1. "logging": {
  2. "applicationInsights": {
  3. "samplingSettings": {
  4. "isEnabled": true,
  5. "excludedTypes": "Request"
  6. }
  7. },
  8. "extensions": {
  9. "cosmosDB": {
  10. "connectionMode": "Gateway",
  11. "userAgentSuffix": "MyDesiredUserAgentStamp"
  12. }
  13. }

My local setting json file looks like

  1. "Values": {
  2. "AzureWebJobsStorage": "UseDevelopmentStorage=true",
  3. "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
  4. "CosmosConnection":
  5. }

答案1

得分: 1

我已经在我的环境中重现了这个问题,并且它按预期工作。

最初,我使用VS Code和.NET 7隔离进程创建了一个默认的隔离Durable函数,然后我在其中加入了CosmosDBOutput

以下是您的代码部分:

  1. using Microsoft.Azure.Functions.Worker;
  2. using Microsoft.Azure.Functions.Worker.Http;
  3. using Microsoft.DurableTask;
  4. using Microsoft.DurableTask.Client;
  5. using Microsoft.Extensions.Logging;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading.Tasks;
  9. namespace Company.Function
  10. {
  11. public static class DurableFunctionsOrchestrationCSharp1
  12. {
  13. [Function(nameof(DurableFunctionsOrchestrationCSharp1))]
  14. public static async Task<List<string>> RunOrchestrator(
  15. [OrchestrationTrigger] TaskOrchestrationContext context)
  16. {
  17. ILogger logger = context.CreateReplaySafeLogger(nameof(DurableFunctionsOrchestrationCSharp1));
  18. logger.LogInformation("Saying hello.");
  19. var outputs = new List<string>();
  20. outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Tokyo"));
  21. outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Seattle"));
  22. outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "London"));
  23. return outputs;
  24. }
  25. [Function(nameof(SayHello))]
  26. [CosmosDBOutput("outDatabase", "mycollection", Connection = "CosmosDBConnection", CreateIfNotExists = true, PartitionKey = "/id")]
  27. public static MyDocument SayHello([ActivityTrigger] string name, FunctionContext executionContext)
  28. {
  29. ILogger logger = executionContext.GetLogger("SayHello");
  30. logger.LogInformation("Saying hello to {name}.", name);
  31. var message = "Life is full of Joy";
  32. var document = new MyDocument
  33. {
  34. id = Guid.NewGuid().ToString(),
  35. message = message
  36. };
  37. return document;
  38. }
  39. public class MyDocument
  40. {
  41. public string? id { get; set; }
  42. public string? message { get; set; }
  43. }
  44. [Function("DurableFunctionsOrchestrationCSharp1_HttpStart")]
  45. public static async Task<HttpResponseData> HttpStart(
  46. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
  47. [DurableClient] DurableTaskClient client,
  48. FunctionContext executionContext)
  49. {
  50. ILogger logger = executionContext.GetLogger("DurableFunctionsOrchestrationCSharp1_HttpStart");
  51. // Function input comes from the request content.
  52. string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(
  53. nameof(DurableFunctionsOrchestrationCSharp1));
  54. logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);
  55. return client.CreateCheckStatusResponse(req, instanceId);
  56. }
  57. }
  58. }

以下是您的local.settings.json文件:

  1. {
  2. "IsEncrypted": false,
  3. "Values": {
  4. "AzureWebJobsStorage": "UseDevelopmentStorage=true",
  5. "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
  6. "CosmosDBConnection": "**********************"
  7. }
  8. }

以下是您的host.json文件:

  1. {
  2. "version": "2.0",
  3. "logging": {
  4. "applicationInsights": {
  5. "samplingSettings": {
  6. "isEnabled": true,
  7. "excludedTypes": "Request"
  8. }
  9. }
  10. }
  11. }

您的.csproj文件包含以下包:

  1. <ItemGroup>
  2. <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.10.0" />
  3. <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.3.0" />
  4. <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.0.0" />
  5. <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
  6. <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" />
  7. </ItemGroup>

请根据MS Docs中隔离进程的示例更改您的数据并更改CosmosDBOutput属性的格式。

英文:

I have reproduced the issue at my environment and it worked as expected

Initially I have created a default isolated Durable function using vs code and .NET 7 isolated process, then I incorporated CosmosDBOutput in it.

Code:

  1. using Microsoft.Azure.Functions.Worker;
  2. using Microsoft.Azure.Functions.Worker.Http;
  3. using Microsoft.DurableTask;
  4. using Microsoft.DurableTask.Client;
  5. using Microsoft.Extensions.Logging;
  6. namespace Company.Function
  7. {
  8. public static class DurableFunctionsOrchestrationCSharp1
  9. {
  10. [Function(nameof(DurableFunctionsOrchestrationCSharp1))]
  11. public static async Task&lt;List&lt;string&gt;&gt; RunOrchestrator(
  12. [OrchestrationTrigger] TaskOrchestrationContext context)
  13. {
  14. ILogger logger = context.CreateReplaySafeLogger(nameof(DurableFunctionsOrchestrationCSharp1));
  15. logger.LogInformation(&quot;Saying hello.&quot;);
  16. var outputs = new List&lt;string&gt;();
  17. outputs.Add(await context.CallActivityAsync&lt;string&gt;(nameof(SayHello), &quot;Tokyo&quot;));
  18. outputs.Add(await context.CallActivityAsync&lt;string&gt;(nameof(SayHello), &quot;Seattle&quot;));
  19. outputs.Add(await context.CallActivityAsync&lt;string&gt;(nameof(SayHello), &quot;London&quot;));
  20. return outputs;
  21. }
  22. [Function(nameof(SayHello))]
  23. [CosmosDBOutput(&quot;outDatabase&quot;, &quot;mycollection&quot;, Connection = &quot;CosmosDBConnection&quot;, CreateIfNotExists = true, PartitionKey = &quot;/id&quot;)]
  24. public static MyDocument SayHello([ActivityTrigger] string name, FunctionContext executionContext)
  25. {
  26. ILogger logger = executionContext.GetLogger(&quot;SayHello&quot;);
  27. logger.LogInformation(&quot;Saying hello to {name}.&quot;, name);
  28. var message = &quot;Life is full of Joy&quot;;
  29. var document = new MyDocument
  30. {
  31. id = Guid.NewGuid().ToString(),
  32. message = message
  33. };
  34. return document;
  35. }
  36. public class MyDocument
  37. {
  38. public string? id { get; set; }
  39. public string? message { get; set; }
  40. }
  41. [Function(&quot;DurableFunctionsOrchestrationCSharp1_HttpStart&quot;)]
  42. public static async Task&lt;HttpResponseData&gt; HttpStart(
  43. [HttpTrigger(AuthorizationLevel.Anonymous, &quot;get&quot;, &quot;post&quot;)] HttpRequestData req,
  44. [DurableClient] DurableTaskClient client,
  45. FunctionContext executionContext)
  46. {
  47. ILogger logger = executionContext.GetLogger(&quot;DurableFunctionsOrchestrationCSharp1_HttpStart&quot;);
  48. // Function input comes from the request content.
  49. string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(
  50. nameof(DurableFunctionsOrchestrationCSharp1));
  51. logger.LogInformation(&quot;Started orchestration with ID = &#39;{instanceId}&#39;.&quot;, instanceId);
  52. return client.CreateCheckStatusResponse(req, instanceId);
  53. }
  54. }
  55. }

local.settings.json

  1. {
  2. &quot;IsEncrypted&quot;: false,
  3. &quot;Values&quot;: {
  4. &quot;AzureWebJobsStorage&quot;: &quot;UseDevelopmentStorage=true&quot;,
  5. &quot;FUNCTIONS_WORKER_RUNTIME&quot;: &quot;dotnet-isolated&quot;,
  6. &quot;CosmosDBConnection&quot;: &quot;**********************&quot;
  7. }
  8. }

host.json

  1. {
  2. &quot;version&quot;: &quot;2.0&quot;,
  3. &quot;logging&quot;: {
  4. &quot;applicationInsights&quot;: {
  5. &quot;samplingSettings&quot;: {
  6. &quot;isEnabled&quot;: true,
  7. &quot;excludedTypes&quot;: &quot;Request&quot;
  8. }
  9. }
  10. }
  11. }

My .csproj file contains below packages

  1. &lt;ItemGroup&gt;
  2. &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker&quot; Version=&quot;1.10.0&quot; /&gt;
  3. &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Extensions.CosmosDB&quot; Version=&quot;4.3.0&quot; /&gt;
  4. &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Extensions.DurableTask&quot; Version=&quot;1.0.0&quot; /&gt;
  5. &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Extensions.Http&quot; Version=&quot;3.0.13&quot; /&gt;
  6. &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Sdk&quot; Version=&quot;1.7.0&quot; /&gt;
  7. &lt;/ItemGroup&gt;

Output:

面对使用可持续的 Azure 函数将文档写入 Cosmos DB 时出现的问题。

面对使用可持续的 Azure 函数将文档写入 Cosmos DB 时出现的问题。

Cosmos DB

面对使用可持续的 Azure 函数将文档写入 Cosmos DB 时出现的问题。

Please make some modification in your data and change the format of CosmosDBOutput attribute as shown in ms docs for isolated process.

huangapple
  • 本文由 发表于 2023年7月17日 16:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定