0

Being honest I'm new to Azure Functions and really basic at C#, I've been looking for answers at some posts here but I'm they didn't work for me or I didn't understand them.

I have a self-triggered function that is doing a API call to Power BI. I want to 'await' until this call finish, but once I start running the function, it just finish without logging some result.

public async static void Run(TimerInfo myTimer, TraceWriter log){
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");    
    var credentials = new TokenCredentials(AZ_ACCESS_KEY, AZ_APP_ID);
    var PBIClient = new PowerBIClient(credentials);
    var report = await getReport(PBIClient);
    log.Info($"Response {report}");
  }

public static async Task<Microsoft.PowerBI.Api.V2.Models.Report> getReport(PowerBIClient PBIClient){
    var myTask = Task.Run(() => 
        PBIClient.Reports.GetReport(REPORT_ID)
    );
    var x = await myTask;
    return x;
}

Now I have no idea why the function is finishing successfully without logging anything, even if somehow the response returns nothing the function might log Response isn't it?

These are the logging results:

2020-02-07T20:20:53.354 [Info] Function started (Id=20ccb5b0-abf2-4957-aeb6-b9f26747c1f0)
2020-02-07T20:20:53.604 [Info] C# Timer trigger function executed at: 2/7/2020 8:20:53 PM
2020-02-07T20:20:53.684 [Info] Function completed (Success, Id=20ccb5b0-abf2-4957-aeb6-b9f26747c1f0, Duration=317ms)

See? It seems that the function is not respecting the await, but I don't know what I'm doing wrong. Thanks,

fmigg
  • 21
  • 1
  • 5

1 Answers1

3

As others have said Avoid Async Void. Try changing the first line from

public async static void Run(TimerInfo myTimer, TraceWriter log)

to

public async Task Run(TimerInfo myTimer, TraceWriter log)

I'm also seeing TraceWritter in there, what version of Azure functions are you using? The more modern ones use ILogger. Might want to upgrade?

Troy Witthoeft
  • 2,498
  • 2
  • 28
  • 37
  • Thanks a lot! That worked, do you mean the runtime version? I'm using `Runtime version: 1.0.12967.0 (~1)` and I it seems I can't change it... – fmigg Feb 11 '20 at 22:02