简体   繁体   English

如何将Console App的代码块转换为Azure Function的代码块?

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

I've written my sample console application and it's complied and get the data well. 我已经编写了示例控制台应用程序,它已经编译并可以很好地获取数据。 Now I want to test it as an Azure Function. 现在,我想将其作为Azure函数进行测试。 The following are the code blocks in console app. 以下是控制台应用程序中的代码块。 How can I rewrite it as Azure's time-trigger function? 如何将其重写为Azure的时间触发功能? Thanks. 谢谢。

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

So I got this finally. 所以我终于明白了。

I used Azure Function Template in VS2017. 我在VS2017中使用了Azure功能模板。

I need to add NuGet Packages (I had to use Azure V2 to match dependency requirement). 我需要添加NuGet程序包 (我必须使用Azure V2来匹配依赖项要求)。 And I just have to put all the codes inside of private async Task Run() of Console App to Azure Function 's public static void Run([TimerTrigger( ... . 而且我只需要将控制台应用程序private async Task Run()内部的所有代码放入Azure Functionpublic static void Run([TimerTrigger( ...

I have yet to publish and test it on Azure. 我尚未在Azure上发布和测试它。 And by the way, Azure Storage Emulator has to be initialized and started with Admin mode in Windows CMD. 顺便说一句,必须在Windows CMD中以管理模式初始化和启动Azure存储模拟器。

函数的输出

I'm not sure what is your intention, but if you want to encode your code in an azure function maybe this can help you. 我不确定您的意图是什么,但是如果您想在azure函数中编码代码,也许可以为您提供帮助。

in order to read a json file you can use : 为了读取json文件,您可以使用:

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

Here you code in one Azure Function 在这里,您可以在一个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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM