0

I am writing a code that will connect to an azure storage table from Azure function.This should return a HTTP response after checking a certain variable in a table rowkey.

There is an error run.csx(2,1): error CS0006: Metadata file 'Microsoft.WindowsAzure.Storage' could not be found

run.csx(8,17): error CS0234: The type or namespace name 'WindowsAzure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

run.csx(27,5): error CS0246: The type or namespace name 'CloudStorageAccount' could not be found (are you missing a using directive or an assembly reference?)

I am not sure how I can install the assembly reference from Azure portal function. I am not using a VS code to code as this is already an existing infrastucture built from Portal.

#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;
using Microsoft.WindowsAzure.Storage.Table;

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

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

    if (string.IsNullOrEmpty(xx))
    {
        return new BadRequestObjectResult("Please enter an xx.");
    }

    string account = "XXW";
    string accountKey = "YYW";
    string tableName = "TWY";
    string connectionString = $"DefaultEndpointsProtocol=https;AccountName={account};AccountKey={accountKey};EndpointSuffix=core.windows.net";

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    CloudTable table = tableClient.GetTableReference(tableName);

    TableOperation operation = TableOperation.Retrieve(xx, xx);
    TableResult result = await table.ExecuteAsync(operation);

    string responseMessage = (result.Result != null) ?
        $"xx: {xx} exists in the table." :
        $"xx: {xx} does not exist in the table.";

    return new OkObjectResult(responseMessage);
}
  • My error got resolved after I installed this package in my Visual studio -https://i.imgur.com/jmY5z4D.png https://i.imgur.com/TUwFHGv.png https://i.imgur.com/HtUE2iu.png – SiddheshDesai Jun 01 '23 at 03:41
  • Hi, is there a way to not use visual studio? – carlplusplus Jun 01 '23 at 03:45
  • Yes, Another way is to run these commands- dotnet add package Microsoft.Azure.Storage.Blob --version 11.2.3 and another methods mentioned there like Package manager, Package reference Cake etc reference- https://www.nuget.org/packages/Microsoft.Azure.Storage.Blob/ for Aspnet refer here- https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Core/2.2.5?_src=template dot net add or another methods would work – SiddheshDesai Jun 01 '23 at 03:49
  • If you want to add nuget package directly from Azure Portal refer the answers in this SO thread - https://stackoverflow.com/questions/57088774/including-a-nuget-package-in-an-azure-function-written-in-the-portal and the answers here- https://stackoverflow.com/questions/36411536/how-can-i-use-nuget-packages-in-my-azure-functions Also refer this blog post on the same- https://h-savran.blogspot.com/2019/08/adding-nuget-packages-to-azure.html – SiddheshDesai Jun 01 '23 at 03:57
  • If you're using windows Function you can directly push your function.proj file in Kudu tool by drag another way is to use App service editor as mentioned in answer here by Fabio Cavalcante - https://stackoverflow.com/questions/36411536/how-can-i-use-nuget-packages-in-my-azure-functions – SiddheshDesai Jun 01 '23 at 03:58

1 Answers1

0

In order to add Nuget package from Azure Portal in your Function app, Refer the steps below:-

Create Functions.csproj file like below:-

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.10.0" />
    <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>
</Project>

I have referred Method 1 and 2 from this SO thread answer by Fabio Cavalcante

Method 1) App Service Editor:-

Go to your Function App > Development Tools > app Service Editor

enter image description here

Link:-

[wwwroot - App Service Editor (scm.azurewebsites.net)](https://functionappname.scm.azurewebsites.net/dev/wwwroot/)

enter image description here

enter image description here

Method 2) Advance Tools > Kudu Console:-

Go to your Function app > Development Tools > Advance Tools> Go to open Kudu Tool in your browser.

enter image description here

Go to Tools > Zip Push Deploy below page will appear:-

enter image description here

Click on + beside /wwwroot and create New file

enter image description here

Create a file with .csproj extension, click on Edit pencil icon and copy the xml code that includes package and Save like below:-

enter image description here

You can click on Debug Console > Cmd> D:\home\site\wwwroot\function-name and drag and drop your Functions.csproj file like below:-

Link:-

[Diagnostic Console (scm.azurewebsites.net)](https://functionappname.scm.azurewebsites.net/DebugConsole)

enter image description here

enter image description here

enter image description here

Method 3)

In your Function app > go to Code + Test and upload the Functions.csproj file above by clicking on upload like below:-

enter image description here

One more workaround is to use FTP to transfer your csproj file in Azure Functions as given in this answer by Fabio Cavalcante

Reference:-

Adding Nuget packages to Azure Functions by using Azure Portal | Hasan Savran (h-savran.blogspot.com)

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11