自参考循环,而我的持久性Azure函数中有两个活动函数 [英] Self referencing loop while there are two activity functions in my durable azure function

查看:81
本文介绍了自参考循环,而我的持久性Azure函数中有两个活动函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

我想在存储帐户中列出容器的Blob URI.为了实现此目标,我想对两个活动使用azure持久函数(我知道它可以更简单地实现,但我想通过两个活动函数来实现它:))

I would like to list blob URIs of a container in a storage account. To achieve this aim, I would like to use azure durable function with two activities (I know it could be implemented more simple, but I want to do it with two activity functions :) )

  • process_file_GetBlobList负责从容器中提取斑点
  • process_file_ProcessBlob负责提取Blob的URI
  • process_file_GetBlobList is responsible to extract blobs from a container
  • process_file_ProcessBlobis responsible to extract URI of the blob

这是我的代码:

using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using System.Threading.Tasks;
using System.Linq;

namespace process
{
    public static class process_file
    {
        [FunctionName("process_file")]
        public static async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] DurableOrchestrationContext context)
        {
            var outputs = new List<string>();

            // Replace "hello" with the name of your Durable Activity Function.
            var blobs= context.CallActivityAsync<string>("process_file_GetBlobList", "");
            await context.CallActivityAsync<string>("process_file_ProcessBlob", blobs);

            return outputs;
        }

        [FunctionName("process_file_GetBlobList")]
        public static IEnumerable<IListBlobItem> GetBlobList([ActivityTrigger] string name, ILogger log)
        {
            string storageConnectionString = @"myConnstring";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("Container");
            IEnumerable<IListBlobItem> blobs = new IListBlobItem[0];

            foreach (IListBlobItem blobItem in container.ListBlobs())
            {

                if (blobItem is CloudBlobDirectory)
                {
                    //Console.WriteLine(blobItem.Uri);
                    CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
                    blobs = directory.ListBlobs(true);

                }
            }

            return blobs;

        }
        [FunctionName("process_file_ProcessBlob")]
        public static void ProcessBlob([ActivityTrigger] IEnumerable<IListBlobItem> blobs, ILogger log)
        {
            var tasks = blobs.Select(currentblob => $"{currentblob.Uri.ToString()}");
        }
        [FunctionName("process_file_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")]HttpRequestMessage req,
            [OrchestrationClient]DurableOrchestrationClient starter,
            ILogger log)
        {
            // Function input comes from the request content.
            string instanceId = await starter.StartNewAsync("process_file", null);

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

            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }
}

运行此代码后,我收到以下错误消息:

After running this code, I get the following error message:

System.Private.CoreLib:执行函数时发生异常: process_file. System.Private.CoreLib:协调器功能 'process_file'失败:检测到属性的自引用循环 类型的任务" 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1+AsyncStateMachineBox 1 [System.String,Microsoft.Azure.WebJobs.DurableOrchestrationContext + d__64`1 [System.String]]'. 路径'[0] .StateMachine.<> t__builder

System.Private.CoreLib: Exception while executing function: process_file. System.Private.CoreLib: Orchestrator function 'process_file' failed: Self referencing loop detected for property 'Task' with type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.String,Microsoft.Azure.WebJobs.DurableOrchestrationContext+d__64`1[System.String]]'. Path '[0].StateMachine.<>t__builder

我应该怎么做才能解决这个问题?

What should I do to solve this problem?

更新 这是我的csproj内容:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="1.8.2" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.28" />
    <PackageReference Include="System.Xml.Linq" Version="3.5.21022.801" />
    <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

推荐答案

我可以使用以下代码解决问题:

I could solve my problem with the following code:

using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using System.Threading.Tasks;
using System.Linq;

namespace process
{
    public static class process_file
    {
        [FunctionName("process_file")]
        public static async Task RunOrchestrator(
            [OrchestrationTrigger] DurableOrchestrationContext context)
        {
            var parallelTasks = new List<Task<int>>();
            IEnumerable<IListBlobItem> blobs = new IListBlobItem[0];
            // Replace "hello" with the name of your Durable Activity Function.
            blobs = await context.CallActivityAsync< IEnumerable<IListBlobItem> > ("process_file_GetBlobList", null);

            foreach (IListBlobItem blob in blobs)
            {
                Task<int> task = context.CallActivityAsync<int>("process_file_ProcessBlob", blob);
                //parallelTasks.Add(task);
            }
            //// Task<int> task= context.CallActivityAsync<string>("process_file_ProcessBlob", blobs);


        }

        [FunctionName("process_file_GetBlobList")]
        public static IEnumerable<IListBlobItem> GetBlobList([ActivityTrigger] string name, ILogger log)
        {
            string storageConnectionString = @"myconn";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("container");
            IEnumerable<IListBlobItem> blobs = new IListBlobItem[0];

            foreach (IListBlobItem blobItem in container.ListBlobs())
            {

                if (blobItem is CloudBlobDirectory)
                {
                    //Console.WriteLine(blobItem.Uri);
                    CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
                    blobs = directory.ListBlobs(true);

                }
            }

            return blobs;

        }
        //IListBlobItem
        [FunctionName("process_file_ProcessBlob")]
        public static void ProcessBlob([ActivityTrigger]  IListBlobItem blob, ILogger log)
        {

            log.LogInformation("Simomn");
            //log.LogInformation(blobs.ToString());
            //var tasks = blobs.Select(currentblob => $"{currentblob.Uri.ToString()}");
        }
        [FunctionName("process_file_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")]HttpRequestMessage req,
            [OrchestrationClient]DurableOrchestrationClient starter,
            ILogger log)
        {
            // Function input comes from the request content.
            string instanceId = await starter.StartNewAsync("process_file", null);

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

            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }
}

这篇关于自参考循环,而我的持久性Azure函数中有两个活动函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆