错误的Azure函数计时器图

时间:2018-08-09 16:40:12

标签: azure azure-functions azure-functions-runtime

我有一个使用Graph Api和MySql连接的计时器函数(测试版/ v2)。当我有一个Http触发器时,一切正常,但是现在我决定使用计时器函数,出现以下错误。

我想念什么?请告诉我。 @Jerry Liu,您可能知道答案;)

收到错误:

2018-08-10T13:36:17欢迎,您现在已连接到日志流服务。 2018-08-10T13:37:17在过去1分钟没有新踪影。 2018-08-10T13:37:23.958 [Information]函数'TimerTriggerMeetingRoom'的脚本已更改。正在重新加载。 2018-08-10T13:37:30.447 [错误] run.csx(40,6):错误CS1997:由于'Run(TimerInfo,string,ILogger)'是返回'Task'的异步方法,因此return关键字一定不能后跟一个对象表达式。您是否打算返回“任务”? 2018-08-10T13:37:30.548 [Information]编译失败。

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"
    }
  ]
}

这是我的“ functions.proj”文件:

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

1 个答案:

答案 0 :(得分:1)

这似乎是您的问题: [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'?

请注意,在堆栈异常跟踪中,有一个“脚本编译错误”,而上面的错误更具体地指出了函数代码中的错误。

我将您的方法签名更改为public static async void Run(...),并删除返回的新HttpResponseMessage(大概在第99行)。简单的return;对于计时器触发的函数会更好,因为在这种情况下我看不到HTTP输出有意义。