Azure Function problems

Steve G 1 Reputation point
2021-05-21T19:53:21.693+00:00

not sure this is the right forum but here goes..
i'm tearing my hair out trying to get an Azure Function to work. this is my first try at a function but i didn't think it was gonna be this difficult.
my initial attempt i did from within VS Code. it actually ran but i was using the default code for a function app that was pre-written by the editor. i was psyched about it working but it still wasn't doing what i wanted it to do. here's the initial 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 Newtonsoft.Json;

namespace Company.Function
{
    public static class HttpTriggerCSharp1
    {
        [FunctionName("HttpTriggerCSharp1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

what i did notice about this was that by using the HttpTrigger, i could pass stuff in via the query string which is perfect for what i want to do.
however, this is where i run into problems. i need to access an Azure storage table in order to retrieve a match on the value passed in but i'm not having much luck trying to connect to my storage table. it seems the code i'm using to try and do that needs a bunch more references/usings in the code. even after thinking i have the right ones in there, i'm still getting all sorts of 'object not defined - are you missing..' errors in the Code compiler. i'm also not sure which version of the TableAttribute to use as the second param to the Run method.
another message i get which i don't understand is something like 'cannot use namespace in this context'. really? what the heck is the above code using? code that worked. so i' m totally confused. here's my latest code which is still having problems..

r "Newtonsoft.Json"

r "Microsoft.WindowsAzure.Storage"

r "Microsoft.Azure.WebJobs"

r "Microsoft.Azure.WebJobs.Extensions"

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Description;
using System;
using System.Threading.Tasks;

namespace FunctionAppSearchFunc2
{
public static class finalSearchFunc2
{
[FunctionName("finalSearchFunc2")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[Table("finalTableStorage1")] CloudTable cloudTable,
ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        string name = req.Query["name"];

        TableQuery<ProductEntity> rangeQuery = new TableQuery<ProductEntity>().Where(
                 TableQuery.GenerateFilterCondition("ProductName", QueryComparisons.Equal,
                     name));

        // Execute the query and loop through the results
        foreach (ProductEntity entity in 
            await cloudTable.ExecuteQuerySegmentedAsync(rangeQuery, null))
        {
            log.LogInformation(
                $"{entity.PartitionKey}\t{entity.RowKey}\t{entity.Timestamp}\t{entity.ProductName}");
        }
    }
}
public class ProductEntity : TableEntity
{
    public ProductEntity() { }
    public ProductEntity(int pid, bool stk, string prodname, string loc, int qty) 
    {
        ProductId = pid;
        Stock = stk;
        ProductName = prodname;
        Location = loc;
        Quantity = qty;
        PartitionKey = prodname;
        RowKey = pid.ToString(); 
    }
    public int ProductId { get; set; }
    public bool Stock { get; set; }
    public string ProductName { get; set; }
    public string Location { get; set; }
    public int Quantity { get; set; }
}

}

here's some example of the errors in the portal after porting my code up..

2021-05-21T15:55:42.986 [Error] run.csx(13,1): error CS7021: Cannot declare namespace in script code
2021-05-21T15:55:43.107 [Error] run.csx(38,34): error CS0246: The type or namespace name 'TableEntity' could not be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.163 [Error] run.csx(20,14): error CS0246: The type or namespace name 'TableAttribute' could not be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.209 [Error] run.csx(20,14): error CS0246: The type or namespace name 'Table' could not be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.256 [Error] run.csx(20,43): error CS0246: The type or namespace name 'CloudTable' could not be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.301 [Error] run.csx(48,13): error CS0103: The name 'PartitionKey' does not exist in the current context
2021-05-21T15:55:43.349 [Error] run.csx(49,13): error CS0103: The name 'RowKey' does not exist in the current context
2021-05-21T15:55:43.396 [Error] run.csx(25,13): error CS0246: The type or namespace name 'TableQuery<>' could not be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.445 [Error] run.csx(25,56): error CS0246: The type or namespace name 'TableQuery<>' could not be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.489 [Error] run.csx(26,22): error CS0103: The name 'TableQuery' does not exist in the current context
2021-05-21T15:55:43.560 [Error] run.csx(26,72): error CS0103: The name 'QueryComparisons' does not exist in the current context
2021-05-21T15:55:43.616 [Error] run.csx(34,31): error CS1061: 'ProductEntity' does not contain a definition for 'PartitionKey' and no accessible extension method 'PartitionKey' accepting a first argument of type 'ProductEntity' could be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.659 [Error] run.csx(34,54): error CS1061: 'ProductEntity' does not contain a definition for 'RowKey' and no accessible extension method 'RowKey' accepting a first argument of type 'ProductEntity' could be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.707 [Error] run.csx(34,71): error CS1061: 'ProductEntity' does not contain a definition for 'Timestamp' and no accessible extension method 'Timestamp' accepting a first argument of type 'ProductEntity' could be found (are you missing a using directive or an assembly reference?)
2021-05-21T15:55:43.754 [Error] error AF003: Missing a trigger argument named 'req'.
2021-05-21T15:55:43.779 [Warning] warning AF004: Missing binding argument named 'finalTableStorage1'. Mismatched binding argument names may lead to function indexing errors.
2021-05-21T15:55:43.779 [Information] Compilation failed.

please help..

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
152 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,016 questions
{count} votes

1 answer

Sort by: Most helpful
  1. sadomovalex 3,621 Reputation points
    2021-06-01T14:16:47.813+00:00

    if you are new one with AF I strongly suggest to use Visual Studio for working with them instead of VS code. In VS mentioned problems are resolved on compile time and you will be able to test it locally (with http://localhost:5000). Also it has built-in integration with Azure and will allow to publish compiled code of AF to Azure directly from VS.

    Mentioned problems (The type or namespace name 'TableEntity', etc) are caused by missing reference of Microsoft.Azure.Cosmos.Table.dll (it may be added with nuget).

    0 comments No comments