miguelito
miguelito

Reputation: 121

Azure Function App Can't bind Table to type 'Microsoft.WindowsAzure.Storage.Table.CloudTable'

I'm working on this Azure Function App where I just need to use the method GET to retrieve all the users from Azure Storage Table. When I run my application I got the error:

Can't bind Table to type 'Microsoft.WindowsAzure.Storage.Table.CloudTable'.

This is my code:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Linq;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.AspNetCore.Builder;

namespace FunctionEncrypt
{
    public class GetUser
    {
        [FunctionName("GetAll")]
        public static async Task<IActionResult>
            GetAll(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "users")] HttpRequest req,
            [Table("user", Connection = "AzureWebJobsStorage")] CloudTable cloudTable,
            ILogger log)
        {
            log.LogInformation("Getting All users");

            TableQuery<UserTable>
            query = new TableQuery<UserTable>();
            var segment = await cloudTable.ExecuteQuerySegmentedAsync(query, null);
            var data = segment.Select(UserExtensions.ToUser);

            return new OkObjectResult(data);
        }
    }
}

And this is my Refences: enter image description here

I have researched about this error, and every single place I looked for, says to replace the "using Microsoft.WindowsAzure.Storage.Table;" for "using Microsoft.Azure.Cosmos.Table;", but even after I change the references, I still get the same error message. I also checked the versions and they are correct. When I changed for cosmos references, I change my package reference as well. I removed because I had issues before with multiple references.

Any ideas what could be wrong?

Upvotes: 4

Views: 2136

Answers (1)

Markus Meyer
Markus Meyer

Reputation: 3967

For net6.0 v4 please use only the following package:
Microsoft.Azure.WebJobs.Extensions.Tables

This means, you can uninstall Microsoft.Azure.WebJobs.Extensions.Storage if you are not using Table or Queue binding.

Finally, you have to clean up your code.
Delete using:
using Microsoft.WindowsAzure.Storage.Table;

This means also, that CloudTable is no longer available.
You have to use the TableClient or bind with TableEntity

Please find examples here:
https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.Tables

using System.Linq;
using System.Threading.Tasks;
using Azure.Data.Tables;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace FunctionEncrypt
{
  public class GetUser
  {
    [FunctionName("GetAll")]
    public static async Task<IActionResult> GetAll(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "users")] HttpRequest req,
        [Table("user", Connection = "AzureWebJobsStorage")] TableClient client,
        ILogger log)
    {
        log.LogInformation("Getting All users");
        var result = client.Query<Azure.Data.Tables.TableEntity>().ToList();
        return new OkObjectResult(result);
    }
  }
}

Upvotes: 5

Related Questions