【问题标题】:Executing Azure Stored Procedure from Azure Triggering Function从 Azure 触发函数执行 Azure 存储过程
【发布时间】:2019-08-21 07:06:26
【问题描述】:

我编写了一个存储过程,用于根据文档的更新日期从 cosmosDB 中删除记录。 cosmosDB 集合具有大量(例如 500 个)分区键,因此我必须执行存储过程 500 次。 我正在尝试从 Azure 时间触发函数执行一个 azure 存储过程。

下面是用azure触发函数编写的代码:

添加了新文件 project.json

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.Azure.DocumentDB": "2.0.0"
      }
    }
   }
}

run.csx 文件更新如下。

#r "Microsoft.Azure.Documents.Client"

    using System;
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.Documents.Client;

    public static async Task Run(TimerInfo myTimer,  IEnumerable<dynamic> 
    inputDocument, TraceWriter log)
    {
    log.Info($"C# Timer trigger function started at: {DateTime.Now}");

    // Get the date 6 months before from Current Time in IST and convert to Epoch value. 
    TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India 
    Standard Time");
    DateTime indianTime =  
    TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow.AddDays(-180), 
    INDIAN_ZONE);


    long epochTime = (long)(indianTime - new DateTime(1970, 1, 
    1)).TotalSeconds;

    DocumentClient client;
    string endpoint = "https://***cosmosdb.documents.azure.com:443/";
    string key = "****";
    client = new DocumentClient(new Uri(endpoint), key);

    foreach (var doc in inputDocument)
        {
            string partKey = doc.NUM;
            StoredProcedureResponse<bool> sprocResponse = await 
    client.ExecuteStoredProcedureAsync<bool>(

   "/dbs/DB_NAME/colls/COLLECTION_NAME/sprocs/STORED PROC_NAME/",new 
    RequestOptions { PartitionKey = new PartitionKey(partKey) });

            log.Info($"Cosmos DB is updated at: {DateTime.Now}");
        }
    }

编译上述代码时出现以下错误。

2019-08-22T15:30:01.759 [Error] Exception while executing function: Functions.TimerTriggerCSharp1. Microsoft.Azure.WebJobs.Script: One or more errors occurred. Microsoft.Azure.Documents.Client: Failed to deserialize stored procedure response or convert it to type 'System.Boolean': Unexpected character encountered while parsing value: {. Path '', line 1, position 1. Newtonsoft.Json: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.
2019-08-22T15:30:01.822 [Error] Function completed (Failure, Id=02da4d71-8207-4227-9c79-1cc277439c20, Duration=1814ms)

在执行存储过程时,我必须传递一个日期参数和分区键数组。 我完全被困在这个阶段。可以做些什么来解决这个问题?

【问题讨论】:

    标签: azure azure-functions azure-cosmosdb azure-cosmosdb-sqlapi


    【解决方案1】:

    错误信息表示您的 Azure Function 的脚本代码不正确。

    删除函数中的所有 privatestatic 声明。

    private static DocumentClient client;
    static string endpoint;
    static string key
    

    ==>

    DocumentClient client;
    string endpoint;
    string key;
    

    一般建议:不要在 Azure 门户(在 .csx 中)中创作函数。帮自己一个忙,使用合适的 IDE,例如 VS Code。

    【讨论】:

    • 我做了上面提到的更改,但我仍然遇到同样的错误!
    • 同样的错误?还是脚本中有更多我没有发现的语法错误?
    • 看,这是一个非常不同的错误。它可能缺少对Microsoft.Azure.Documents.Client nuget 包的引用。再次,请帮自己一个忙,用 VS Code 编写函数。您在这里遇到的所有这些问题都会消失。 docs.microsoft.com/en-us/azure/azure-functions/…
    【解决方案2】:

    如果您使用的是 Portal,您需要像这样在 Function 顶部拉入依赖项:

    #r "Microsoft.Azure.Documents.Client"
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.Documents.Client;
    

    请使用更新版本的 SDK,1.7.1 来自 3 多年前:https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/

    【讨论】:

    • 我使用的是 Azure function2.x,现在我已更改为 1.x。现在我在运行该函数时遇到另一个错误{"Errors":["Owner resource does not exist"]}。更新了代码和错误。您能否检查一下我为执行存储过程而传递的参数?
    • 所有者资源不存在意味着数据库或集合(或两者)都不存在。
    • "/dbs/DB_NAME/colls/COLLECTION_NAME/sprocs/STORED PROC_NAME/" 需要是真实路径,其中 DB_NAME 需要是您的真实数据库名称,COLLECTION_NAME 是您的真实集合名称,STORED PROC_NAME你的真实存储过程名称。
    • 谢谢@Matias Quaranta:该错误已解决,必须将参数(代码中的epochTime)传递给存储过程。如何从现有代码中传递?
    • 将其作为 ExecuteStoredProcedureAsync 调用的最后一个参数传递,请参阅文档:docs.microsoft.com/en-us/dotnet/api/…
    猜你喜欢
    • 2018-07-17
    • 2021-10-08
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2013-09-23
    • 2021-07-07
    相关资源
    最近更新 更多