自引用循环,而我的持久天蓝色函数中有两个活动函数

Posted

技术标签:

【中文标题】自引用循环,而我的持久天蓝色函数中有两个活动函数【英文标题】:Self referencing loop while there are two activity functions in my durable azure function 【发布时间】:2020-03-04 06:04:22 【问题描述】:

这是我的场景:

我想列出存储帐户中容器的 blob URI。为了实现这个目标,我想使用带有两个活动的天蓝色持久功能(我知道它可以实现更简单,但我想用两个活动功能来做到这一点:))

process_file_GetBlobList 负责从容器中提取 blob process_file_ProcessBlob负责提取blob的URI

这是我的代码:

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);
        
    

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

System.Private.CoreLib:执行函数时出现异常: 进程文件。 System.Private.CoreLib:Orchestrator 函数 “process_file”失败:检测到属性的自引用循环 带有类型的“任务” 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.String,Microsoft.Azure.WebJobs.DurableOrchestrationContext+d__64`1[System.String]]'。 路径'[0].StateMachine.t__builder

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

更新 这是我的csprojcontent:

<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>

【问题讨论】:

您好,您可以提供您的功能应用程序的.csproj 文件吗?我尝试重现您的错误,但缺少一些信息 嗨,我已经更新了我的帖子。 Tnx 【参考方案1】:

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

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);
        
    

【讨论】:

以上是关于自引用循环,而我的持久天蓝色函数中有两个活动函数的主要内容,如果未能解决你的问题,请参考以下文章

把4个鸡蛋放进两个蓝子里(有一个蓝子可以不放)有几种放法 可以怎么放?

天蓝色函数中的自定义配置部分?

如何将自定义轮文件安装到天蓝色函数中?

如何清除子函数中的持久变量

c++中的时间函数引用

C语言中如何引用其它C源文件中的函数?