-1

我已经编写了我的示例控制台应用程序,它已被编译并很好地获取数据。现在我想将其作为 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.
    }
}
4

2 回答 2

1

所以我终于得到了这个。

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

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

我还没有在 Azure 上发布和测试它。顺便说一句,Azure Storage Emulator 必须在 Windows CMD 中以管理员模式进行初始化和启动。

函数的输出

于 2018-12-05T21:22:18.110 回答
0

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

为了读取 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;
    }
于 2018-12-05T11:41:51.287 回答