Pallab
Pallab

Reputation: 2325

Getting error in C# for Azure Function app

Can anyone help me fix these errors?

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;

public static async Task<IActionResult> Run
              (HttpRequest req,
               CloudTable objUserProfileTable,
               ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string firstname = null, lastname = null;

    string requestBody = await new 
    StreamReader(req.Body).ReadToEndAsync();

    dynamic inputJson = JsonConvert.DeserializeObject(requestBody);
    firstname = firstname ?? inputJson?.firstname;
    lastname = inputJson?.lastname;

    UserProfile objUserProfile = new UserProfile(firstname, lastname) ;

    TableOperation objTblOperationInsert = 
    TableOperation.Insert(objUserProfile);
    await objUserProfileTable.ExecuteAsync(objTblOperationInsert);

    return (lastname + firstname) != null 
    ? (ActionResult)new OkObjectResult($"Hello, {firstname + " " + lastname}")
        :new BadRequestObjectResult("Please pass a name on the query" + "string or in the request body");
}

class UserProfile : TableEntity
{
    public UserProfile(string firstName, string lastName)
    {
        this.PartitionKey = "p1";
        this.RowKey = Guid.NewGuid().ToString();
        this.FirstName = firstName;
        this.LastName = lastName;
    }

    UserProfile() {}

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

I get these errors:

Error CS0006: Metadata file 'Microsoft.WindowsAzure.Storage' could not be found

Error CS0234: The type or namespace name 'WindowsAzure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Error CS0246: The type or namespace name 'CloudTable' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246: The type or namespace name 'TableEntity' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246: The type or namespace name 'TableOperation' could not be found (are you missing a using directive or an assembly reference?)

Error CS0103: The name 'TableOperation' does not exist in the current context

Error CS1061: 'UserProfile' does not contain a definition for 'PartitionKey' and no accessible extension method 'PartitionKey' accepting a first argument of type 'UserProfile' could be found (are you missing a using directive or an assembly reference?)

Error CS1061: 'UserProfile' does not contain a definition for 'RowKey' and no accessible extension method 'RowKey' accepting a first argument of type 'UserProfile' could be found (are you missing a using directive or an assembly reference?)

Compilation failed.

Upvotes: 1

Views: 2880

Answers (1)

Skin
Skin

Reputation: 11197

I think Microsoft may need to update their documentation.

Microsoft.WindowsAzure.Storage has been deprecated and has been replaced by a few other Nuget packages.

https://www.nuget.org/packages/WindowsAzure.Storage/9.3.3?_src=template

For what you're looking at wanting to do, as far I can work out, you'll need to use Azure.Data.Tables for tables.

https://github.com/Azure/azure-sdk-for-net/blob/Azure.Data.Tables_12.4.0/sdk/tables/Azure.Data.Tables/README.md

The thing is, it's not a package you can pull in using the #r statement.

To reference it directly in the portal, you'll need to create a function.proj file in your function and include it as a reference.

It basically looks like this ...

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Azure.Data.Tables" Version="12.4.0" />
    </ItemGroup>
</Project>

You can then use it in a normal using statement as per any other program you'd create.

How you then incorporate it into your function is the next question given I can see you have it as a part of your signature to your Run method through a binding or something.

If you can't get the binding to work, you'll just need to connect to it using a connection string or something of that nature directly at the code level.

Upvotes: 1

Related Questions