Azure Function to Update Table Storage entity - CloudTable.Execute not found

Viewed 563

I'm starting out using Azure and c# and I'm attempting to use Table Storage - and using an Azure Function to update entitys in the table. My code is as follows:

 #r "Microsoft.WindowsAzure.Storage"
#r "Newtonsoft.Json"
using System;
using Newtonsoft.Json; 
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;

public static async Task<HttpResponseMessage> Run(HttpRequest req, CloudTable lookupTable)
{
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    string partitionKey = data.Society;
    string rowKey = data.ConnectionDetails.Environment; 
    string newConnection = data.ConnectionDetails.Connection; 

    TableOperation operation = TableOperation.Retrieve<SocietyConnectionDetails>(partitionKey, rowKey); 
    TableResult result = lookupTable.Execute(operation);
    SocietyConnectionDetails societyConnectionDetails = (SocietyConnectionDetails)result.Result; 

    societyConnectionDetails.Connection = newConnection; 
    operation = TableOperation.Replace(societyConnectionDetails); 
    lookupTable.Execute(operation);
}

public class SocietyConnectionDetails : TableEntity
{
    public string Connection {get; set;}
}

But the errors im getting are as follows:

2020-02-25T10:33:16.956 [Error] run.csx(17,38): error CS1061: 'CloudTable' does not contain a definition for 'Execute' and no accessible extension method 'Execute' accepting a first argument of type 'CloudTable' could be found (are you missing a using directive or an assembly reference?)
2020-02-25T10:33:16.984 [Error] run.csx(22,17): error CS1061: 'CloudTable' does not contain a definition for 'Execute' and no accessible extension method 'Execute' accepting a first argument of type 'CloudTable' could be found (are you missing a using directive or an assembly reference?)
2020-02-25T10:33:17.011 [Error] run.csx(8,47): error CS0161: 'Run(HttpRequest, CloudTable)': not all code paths return a value

I can see that the issue is happening when im attempting to 'Execute' my Table Operations... this might be a relatively straight forward problem but I'm struggling to work out why this wouldn't be working...

Thanks for any help..

2 Answers

I assume you are using the Azure Functions runtime 2, and in this case the problem is related to your references. You should reference the nuget package Microsoft.Azure.WebJobs.Extensions.Storage and ensure it is installed in your function, according to this article in Microsoft's documentation.

I can reproduce your error.

Like this:

enter image description here

Solution is add a function.proj file to your function. enter image description here

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
    </ItemGroup>
</Project>

Then the error should disappear. (If you dont do this. the compilation step will not success.) enter image description here

Related