使用持久Azure函数将文档写入Cosmos DB

agxfikkp  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(110)

我有一个链接持久的Azure函数,我想在其中将文档写入Cosmos DB。我正在运行隔离模式。我的函数看起来像这样:

[Function(nameof(ScanLinksAndInsertIntoLinksContainer))]
        [CosmosDBOutput(databaseName: "dpubot", containerName: "test", Connection= "CosmosConnection"
            , CreateIfNotExists = true, PartitionKey = "id")]
        public static MultiResponse ScanLinksAndInsertIntoLinksContainer
            ([ActivityTrigger] string name, FunctionContext executionContext)
        {
        
            var message = "Life is short";
            return new MultiResponse()
            {
                Document = new MyDocument
                {
                    id = System.Guid.NewGuid().ToString(),
                    message = message
                },
                
            };

        }

字符串
我收到一个错误:

Executed 'Functions.ScanLinksAndInsertIntoLinksContainer' (Failed, Id=635107bb-afc7-46e6-ac6b-5e4454f07254, Duration=2040ms)
[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"]}


我的Host.json看起来像

"logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        },
        "extensions": {
          "cosmosDB": {
            "connectionMode": "Gateway",
            "userAgentSuffix": "MyDesiredUserAgentStamp"
          }
        }


我的本地设置json文件看起来像

"Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "CosmosConnection":
    
    }

smdncfj3

smdncfj31#

  • 我已在我的环境中重现了该问题,它按预期工作 *

最初,我使用vs代码和.NET 7隔离进程创建了一个默认的隔离Durable函数,然后将CosmosDBOutput合并到其中。

一米

using  Microsoft.Azure.Functions.Worker;
using  Microsoft.Azure.Functions.Worker.Http;
using  Microsoft.DurableTask;
using  Microsoft.DurableTask.Client;
using  Microsoft.Extensions.Logging;

namespace  Company.Function
{
public  static  class  DurableFunctionsOrchestrationCSharp1
{
[Function(nameof(DurableFunctionsOrchestrationCSharp1))]
public  static  async  Task<List<string>> RunOrchestrator(
[OrchestrationTrigger] TaskOrchestrationContext  context)
{
ILogger  logger  =  context.CreateReplaySafeLogger(nameof(DurableFunctionsOrchestrationCSharp1));

logger.LogInformation("Saying hello.");

var  outputs  =  new  List<string>();

outputs.Add(await  context.CallActivityAsync<string>(nameof(SayHello), "Tokyo"));
outputs.Add(await  context.CallActivityAsync<string>(nameof(SayHello), "Seattle"));
outputs.Add(await  context.CallActivityAsync<string>(nameof(SayHello), "London"));
return  outputs;
} 

[Function(nameof(SayHello))]
[CosmosDBOutput("outDatabase", "mycollection", Connection  =  "CosmosDBConnection", CreateIfNotExists  =  true, PartitionKey  =  "/id")]
public  static  MyDocument  SayHello([ActivityTrigger] string  name, FunctionContext  executionContext)
{
ILogger  logger  =  executionContext.GetLogger("SayHello");
logger.LogInformation("Saying hello to {name}.", name);
var  message  =  "Life is full of Joy";
var  document  =  new  MyDocument
{
id  =  Guid.NewGuid().ToString(),
message  =  message
};
return  document;
}

public  class  MyDocument
{
public  string?  id { get; set; }
public  string?  message { get; set; }
}

[Function("DurableFunctionsOrchestrationCSharp1_HttpStart")]
public  static  async  Task<HttpResponseData> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData  req,
[DurableClient] DurableTaskClient  client,
FunctionContext  executionContext)
{
ILogger  logger  =  executionContext.GetLogger("DurableFunctionsOrchestrationCSharp1_HttpStart");

// Function input comes from the request content.

string  instanceId  =  await  client.ScheduleNewOrchestrationInstanceAsync(
nameof(DurableFunctionsOrchestrationCSharp1));
logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);
return  client.CreateCheckStatusResponse(req, instanceId);
}
}
}

字符串

local.settings.json

{

"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"CosmosDBConnection": "**********************"

}
}

host.json

{

"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"

}
}
}
}


我的.csproj文件包含以下软件包

<ItemGroup>

<PackageReference  Include="Microsoft.Azure.Functions.Worker"  Version="1.10.0"  />
<PackageReference  Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB"  Version="4.3.0"  />
<PackageReference  Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask"  Version="1.0.0"  />
<PackageReference  Include="Microsoft.Azure.Functions.Worker.Extensions.Http"  Version="3.0.13"  />
<PackageReference  Include="Microsoft.Azure.Functions.Worker.Sdk"  Version="1.7.0"  />

</ItemGroup>

Output:


的数据


Cosmos DB



请对您的数据做一些修改,将CosmosDBOutput属性的格式修改为ms docs,用于隔离进程。

相关问题