-1

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. The following are the code blocks in console app. How can I rewrite it as Azure's time-trigger function? 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.
    }
}
zawhtut
  • 229
  • 2
  • 14
  • 1
    Please share the attempts you've made yourself and explain what isn't working. – Daniel Mann Dec 05 '18 at 04:01
  • So I am trying out a sample code to get data using Google's API. I use service account authentication with key info by reading a JSON file. Or I can just simply use API key to get Google's data. And now I wish to try it as Azure function. The Azure function will call the Google' Data API and display the results. The data will be parsed using Google namespaces. – zawhtut Dec 05 '18 at 04:10
  • I guess I just need to put all the code in console app's Task Run() function into Azure's function Run(...) I just don't know Azure function's syntax. – zawhtut Dec 05 '18 at 04:19
  • 1
    Are you using VS? Which version? It should have templates for you to create a new Function app. – demokritos Dec 05 '18 at 04:39
  • I will try to use a template. I was using Azure portal. I am now reading dependency injection in Azure function. Thanks – zawhtut Dec 05 '18 at 04:43

2 Answers2

1

So I got this finally.

I used Azure Function Template in VS2017.

I need to add NuGet Packages (I had to use Azure V2 to match dependency requirement). 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( ....

I have yet to publish and test it on Azure. And by the way, Azure Storage Emulator has to be initialized and started with Admin mode in Windows CMD.

Function's output

zawhtut
  • 229
  • 2
  • 14
0

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.

in order to read a json file you can use :

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

Here you code in one Azure Function

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