0
votes

Hi I am trying out simple Azure Function with REST Api. My env is MSVC 2019 and created a simple mmApi Fucntion app

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using AzureFuncApp.Model;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage;

namespace AzureFuncApp
{
    public static class mmApi
    {
        //static List<Board> boards = new List<Board>();

        [FunctionName("CreateBoard")]
        public static async Task<IActionResult> CreateBoard(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "board")] HttpRequest req,
            [Table("boards", Connection ="AzureWebJobsStorage")] IAsyncCollector<BoardTableEntity> boardTable,
            ILogger log)
        {
            log.LogInformation("Creating a new board.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var input = JsonConvert.DeserializeObject<BoardCreateModel>(requestBody);
            var board = new Board() { PlayerCount = input.PlayerCount };
            board.Players.Add(input.Player);
            await boardTable.AddAsync(board.ToTableEntity());
            return new OkObjectResult(board);
        }

It is this line that I have added to Table Storage [Table("boards", Connection ="AzureWebJobsStorage")] IAsyncCollector

Is throwing the below error

Function (AzureFuncAppmmApi/CreateBoard) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'CreateBoard'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'boardTable' to type IAsyncCollector`1. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

What I am not following is where do I register builder.AddAzureStorage() when I don't have the startup file in my project.

Attached is my project details, any help and pointer is appriciated

enter image description here

1
The static list storage //static List<Board> boards = new List<Board>(); just works fineUp-In-Air
I would try binding to CloudTable instead of IAsyncCollector<> to see whether that works. Also the Microsoft.Azure.WebJobs.Extensions.Storage version that works in my projects is 3.0.5. Can you try whether downgrading to that works?tubakaya
Also try removing nuget references to Microsoft.Azure.WebJobs and Microsoft.Azure.WebJobs.Core. From my experience referencing extra packages caused many troubles.tubakaya
[12/14/2019 10:19:24 PM] The 'GetBoards' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'GetBoards'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'boardTable' to type CloudTable. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.). I do have a GET api using Cloud I still get the errorUp-In-Air
I also removed Microsoft.Azure.WebJobs and Microsoft.Azure.WebJobs.Core, still no luckUp-In-Air

1 Answers

1
votes

I found the answer, Microsoft.NET.Sdk.Functions updates to v3.0.2, manually added the below refrences in the csprog file and it works fine. @tubakaya your suggestion helped

    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.23" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.1" />   </ItemGroup>```