0

I have a Timer Functions (beta version/v2) with that uses the Graph Api and a MySql connection. Everything was working while I had an Http Trigger but now that I've decided to use a Timer Function I'm getting the the error below.

What am I missing? Please let me know. @Jerry Liu, you might know the answer ;)

Error received:

2018-08-10T13:36:17 Welcome, you are now connected to log-streaming service. 2018-08-10T13:37:17 No new trace in the past 1 min(s). 2018-08-10T13:37:23.958 [Information] Script for function 'TimerTriggerMeetingRoom' changed. Reloading. 2018-08-10T13:37:30.447 [Error] run.csx(40,6): error CS1997: Since 'Run(TimerInfo, string, ILogger)' is an async method that returns 'Task', a return keyword must not be followed by an object expression. Did you intend to return 'Task'? 2018-08-10T13:37:30.548 [Information] Compilation failed.

This is my "run.csx" code:
#r "Newtonsoft.Json"
#r "System.Configuration"
#r "System.Data"


using System.Net; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Text;
using System;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

using System.Configuration;

using System.Threading.Tasks;

public static async Task Run(TimerInfo myTimer, string graphToken, ILogger log)
{
    var currentTime = DateTime.UtcNow;
    var ramsey = new List<Ramsey>();

    log.LogInformation("C# HTTP trigger function processed a request.");

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", graphToken);
    var json = await client.GetStringAsync("https://graph.microsoft.com/v1.0/me/");


    log.LogInformation("C# HTTP trigger function processed a request.");
    JObject jResults = JObject.Parse(json);
    //log.LogInformation(jResults.ToString());
    var result= jResults["value"];
    log.LogInformation("value: "+result.ToString());

     return new HttpResponseMessage(HttpStatusCode.OK) 
    {
        Content = new StringContent(json, Encoding.UTF8, "application/json") 
    };
}


public class Ramsey
{
    public string subject { get; set; }
}

"Function.json":

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */15 * * * *"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "token",
      "name": "graphToken",
      "resource": "https://graph.microsoft.com",
      "identity": "UserFromRequest",
      "direction": "in"
    }
  ]
}

This is instead my "functions.proj" file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Mobile.Client" Version="2.0.0"/>
    <PackageReference Include="MySql.Data" Version="7.0.7-m61"/>
  </ItemGroup>
</Project>
Filippo
  • 55
  • 6

1 Answers1

1

It looks like this is your issue: [Error] run.csx(99,5): error CS1997: Since 'Run(TimerInfo, string, ILogger)' is an async method that returns 'Task', a return keyword must not be followed by an object expression. Did you intend to return 'Task'?

Note that in the stack trace of exceptions, there's a "script compilation error" and the error above that more specifically calls out the error in your function code.

I would change your method signature to be public static async void Run(...), and remove returning a new HttpResponseMessage (presumably on line 99). A simple return; would be better for a timer triggered function, as I can't see an HTTP output making sense in this context.

Marie Hoeger
  • 1,261
  • 9
  • 11
  • Sorry! In rush I copied the old code with the http trigger. Now it's the right one ;) – Filippo Aug 09 '18 at 20:56
  • Hi Marie, Many thanks for your reply. No, it doesn't work. I get now this error: 2018-08-10T08:52:34.019 [Error] Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.TimerTriggerMeetingRoom'. Microsoft.Azure.WebJobs.Host: Unable to resolve binding parameter 'headers'. Binding expressions must map to either a value provided by the trigger or a property of the value the trigger is bound to, or must be a system binding expression (e.g. sys.randguid, sys.utcnow, etc.). – Filippo Aug 10 '18 at 08:53
  • I've just wrote a basic version of the problem so everyone can try it in order to solve the my problem and let me know what I'm doing wrong – Filippo Aug 10 '18 at 13:41
  • Hi, the problem hasn’t been solved. I have still the same problem. If someone could try the code above and tell me what I am doing wrong it would be great. Thank you in advance – Filippo Aug 17 '18 at 06:47
  • The [HTTP output binding](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook#output) is to respond to the HTTP request sender, and doesn't make sense/won't work with a timer trigger. If you remove the Http output in `function.json` as well as in your code, it should compile. – Marie Hoeger Aug 17 '18 at 18:51
  • Hi Marie, Many thanks for your reply. After the summer break, today, I've had the time to update the code removing the HTTP binding. However, just adding the "string graphToken" among the parameter I get error. Without everything works. How can I implement the using of the Graph Api without getting an error? Could you write me a basic working example please? Thank you in advance – Filippo Aug 26 '18 at 18:33