如何将控制台应用程序代码块转换为 Azure Functions 代码块?

How do I convert Console App's code block to Azure Function's code block?

我已经编写了我的示例控制台应用程序,它已被编译并很好地获取了数据。现在我想将其作为 Azure 函数进行测试。以下是控制台应用程序中的代码块。如何将其重写为 Azure 的时间触发函数?谢谢

using System;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;

namespace Google.Apis.Samples

internal class MyData
{
    [STAThread]
    static void Main(string[] args)
    {
        Console.WriteLine("Blah Blah Blah");
        Console.WriteLine("==============");

        try
        {
            new MyData().Run().Wait();
        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }
    }
    private async Task Run()
    {
    // I can either use service account or supply api key.
    // How do I read a JSON file from Azure function?
    // then I can Get data and display results.
    }
}

我不确定你的意图是什么,但如果你想在 azure 函数中编码你的代码,也许这可以帮助你。

要读取 json 文件,您可以使用:

  FileStream fs = new FileStream(@"your_json", FileMode.Open)

在这里你在一个 Azure 函数中编码

using System.Net;
using System.IO;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("Blah Blah Blah");
    log.Info("==============");

    try
        {
            await Run_Function();
        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                log.Info("Error: " + e.Message);
            }
        }


     return req.CreateResponse(HttpStatusCode.OK, "OK");
}

 private static Task Run_Function()
    {
    // I can either use service account or supply api key.
    // How do I read a JSON file from Azure function?
      using (FileStream fs = new FileStream(@"your_json", FileMode.Open))
        {
                // then I can Get data and display results.                
        }
    return null;
    }

所以我终于明白了。

我在 VS2017 中使用了 Azure 函数模板。

我需要添加 NuGet 包(我必须使用 Azure V2 来匹配依赖项要求)。 我只需要将 Console Appprivate async Task Run() 中的所有代码放到 Azure Functionpublic static void Run([TimerTrigger( ... 中。

我还没有在 Azure 上发布和测试它。顺便说一下,Azure 存储模拟器必须在 Windows CMD 中以管理员模式初始化和启动。