Azure功能应用程序在本地工作但在实时时抛出异常

时间:2017-12-09 09:24:50

标签: function rest azure azure-functions

我正在使用Azure功能,并在Visual Studio中编写了我的第一个应用程序。该功能应该执行以下操作:

  • 每5分钟执行以下步骤:
  • 为最近播放的比赛调用外部API(防暴游戏API)。
  • 从每个匹配项的外部API中获取更多信息。
  • 在我的DTO(表格实体)中转换该信息。
  • 将匹配的商店存储在我的表存储中。

所有这些步骤都完成了3次,因为我为3个不同的玩家做了这些。 这是我的代码,没有实际的玩家名称和密钥:

public static class MatchHistoryUpdater
{
    [FunctionName("MatchHistoryUpdater")]
    public static async Task Run(
        [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer)
    {
        var storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=********;AccountKey=*******==;EndpointSuffix=core.windows.net";
        var account = CloudStorageAccount.Parse(storageConnectionString);
        var serviceClient = account.CreateCloudTableClient();

        var riotClient = new RiotClient(new RiotClientSettings
        {
            ApiKey = "********",
        });

        var accountIds = new List<long> { *****, *****, *****};
        var summonerNames = new List<string> { "*****", "*****", "*****" };

        // valid names, all lower case, no whitespaces, etc.
        var tableNames = new List<string> { "*****", "*****", "*****" };

        var championList = await riotClient.GetStaticChampionsAsync(platformId: PlatformId.EUW1);

        for (var i = 0; i< accountIds.Count; i++)
        {
            var recentMatches = await riotClient.GetMatchListByAccountIdAsync(accountIds[i], platformId: PlatformId.EUW1);
            if (recentMatches.Matches.Count == 0) continue;

            var table = serviceClient.GetTableReference(tableNames[i]);

            foreach(var match in recentMatches.Matches)
            {
                var retrieveOperation = TableOperation.Retrieve<MatchResult>(match.GameId.ToString(), match.Timestamp.ToString());
                var retrievedMatch = table.Execute(retrieveOperation);
                if (retrievedMatch != null) continue;

                var newMatch = await riotClient.GetMatchAsync(match.GameId, PlatformId.EUW1);

                var participantIdentity = newMatch.ParticipantIdentities.Single(x => x.Player.SummonerName == summonerNames[i]);
                var participantId = participantIdentity.ParticipantId;
                var participant = newMatch.Participants.Single(x => x.ParticipantId == participantId);

                var result = "Remake";
                if (participant.Stats.Win) result = "Victory";
                if (!participant.Stats.Win) result = "Defeat";

                var champion = championList.Data.Single(x => x.Value.Id.Equals(participant.ChampionId)).Key;

                var matchResult = new MatchResult
                {
                    PartitionKey = newMatch.GameId.ToString(),
                    RowKey = newMatch.GameCreation.ToString(),
                    Champion = champion,
                    Kills = participant.Stats.Kills,
                    Deaths = participant.Stats.Deaths,
                    Assists = participant.Stats.Assists,
                    Result = result,
                    GameDuration = newMatch.GameDuration.ToString(),
                    TotalDamageDealtToChampions = participant.Stats.TotalDamageDealt,
                    TotalDamageTaken = participant.Stats.TotalDamageTaken,
                    GoldEarned = participant.Stats.GoldEarned,
                    CreepScore = participant.Stats.TotalMinionsKilled + participant.Stats.NeutralMinionsKilled
                };

                var insertOperation = TableOperation.InsertOrReplace(matchResult);
                table.Execute(insertOperation);
            }
        }
    }
}

我在本地启动功能应用,功能正常。它成功了,之后我可以查看Azure存储资源管理器,看到所有表都已更新。

所以现在我将我的应用程序发布到Azure。这也有效。但是,当我查看我的函数调用日志时,它给出了以下错误消息:

Exception while executing function: MatchHistoryUpdater
Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: MatchHistoryUpdater ---> Microsoft.WindowsAzure.Storage.StorageException : The remote server returned an error: (400) Bad Request. ---> System.Net.WebException : The remote server returned an error: (400) Bad Request.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd,IRetryPolicy policy,OperationContext operationContext) at c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs : 677 
   End of inner exception
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd,IRetryPolicy policy,OperationContext operationContext) at c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs : 604
   at Microsoft.WindowsAzure.Storage.Table.TableOperation.Execute(CloudTableClient client,CloudTable table,TableRequestOptions requestOptions,OperationContext operationContext) at c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Table\TableOperation.cs : 41
   at Microsoft.WindowsAzure.Storage.Table.CloudTable.Execute(TableOperation operation,TableRequestOptions requestOptions,OperationContext operationContext) at c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Table\CloudTable.cs : 51
   at async LoLChest.MatchHistoryUpdater.Run(TimerInfo myTimer)
   at async Microsoft.Azure.WebJobs.Host.Executors.VoidTaskMethodInvoker`2.InvokeAsync[TReflected,TReturnType](TReflected instance,Object[] arguments)
   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync[TReflected,TReturnValue](Object instance,Object[] arguments)
   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunc…

我之所以认为这可能是因为由于某种速率限制而检索实体的行并对这些行进行了评论:

var retrieveOperation = TableOperation.Retrieve<MatchResult>(match.GameId.ToString(), match.Timestamp.ToString());
                    var retrievedMatch = table.Execute(retrieveOperation);
                    if (retrievedMatch != null) continue;

但无论如何:我得到同样的错误。

所以现在我被卡住了。有没有人知道为什么它的局部表现可能与天蓝色不同?

提前致谢!

0 个答案:

没有答案