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

时间:2019-11-07 15:52:32

标签: c# azure azure-functions

这是我的情况:

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

  • process_file_GetBlobList负责从容器中提取斑点
  • 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:执行函数时发生异常:   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

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

更新 这是我的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>

1 个答案:

答案 0 :(得分:0)

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

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