C# 如何转换控制台应用程序';s代码块到Azure函数';s代码块?

C# 如何转换控制台应用程序';s代码块到Azure函数';s代码块?,c#,azure,azure-functions,csx,C#,Azure,Azure Functions,Csx,我已经编写了我的示例控制台应用程序,它已经编译好并获得了良好的数据。现在我想将其作为Azure函数进行测试。以下是console应用程序中的代码块。如何将其重写为Azure的时间触发函数?谢谢 using System; using System.IO; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using Google.Apis.Auth.OAuth2; using Google.Apis.Services; name

我已经编写了我的示例控制台应用程序,它已经编译好并获得了良好的数据。现在我想将其作为Azure函数进行测试。以下是console应用程序中的代码块。如何将其重写为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;
    }
使用System.Net;
使用System.IO;
公共静态异步任务运行(HttpRequestMessage请求、TraceWriter日志)
{
日志信息(“诸如此类”);
log.Info(“============================”);
尝试
{
等待运行函数();
}
捕获(聚合异常)
{
foreach(ex.InnerExceptions中的变量e)
{
日志信息(“错误:+e.Message”);
}
}
返回请求CreateResponse(HttpStatusCode.OK,“OK”);
}
专用静态任务运行函数()
{
//我可以使用服务帐户或提供api密钥。
//如何从Azure函数读取JSON文件?
使用(FileStream fs=newfilestream(@“your_json”,FileMode.Open))
{
//然后我可以得到数据并显示结果。
}
返回null;
}
所以我终于得到了这个

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

我需要添加NuGet软件包(我必须使用Azure V2来满足依赖项要求)。 我只需要将控制台应用程序的
私有异步任务运行()
中的所有代码放到Azure函数的
公共静态无效运行([TimerTrigger(…
)中

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


请分享您自己所做的尝试,并解释哪些不起作用。因此,我正在尝试使用示例代码使用Google的API获取数据。我通过读取JSON文件使用密钥信息进行服务帐户身份验证。或者我可以简单地使用API密钥获取Google的数据。现在我希望将其作为Azure函数进行尝试。Azure函数将所有Google的数据API并显示结果。数据将使用Google名称空间进行解析。我想我只需要将console应用程序的Task Run()函数中的所有代码放入Azure的函数Run(…)我只是不知道Azure函数的语法。你在使用VS吗?哪个版本?它应该有模板供你创建新函数应用程序。我将尝试使用模板。我在使用Azure portal。我现在正在阅读Azure函数中的依赖项注入。谢谢