Use Microsoft.Azure.Storage in Azure Function csx

2.1k views Asked by At

Somehow I cannot use package Microsoft.Azure.Storage.blob in Azure Function v2 using csx.

In extension.proj I have following:

<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.0" />

In csx file I have:

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;

And I have error:

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

Full code is on GitHub: https://github.com/ptrstpp950/cognitive-service-azure-function

2

There are 2 answers

0
Thiago Custodio On

You should import the package before using it:

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.Extensions.Logging;
3
Cindy Pau On

1.Are you sure of the extension.proj you are using?

From your code I know you are writing on the portal. So you should create function.proj instead of extension.proj on portal.

2. I see you write <PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.0" /> in .proj file. So you should use #r "Microsoft.WindowsAzure.Storage" instead of using Microsoft.WindowsAzure.Storage

Below is the code of my function.proj, things works fine on my side. For more details, have a look of this Offical doc.(All of the solution is based on you are using function 2.x. If you are using function 1.x. It is not the same.)

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

    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.0" />
    </ItemGroup>
</Project>

enter image description here

Code of my .crx file:

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

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.Storage.Blob;

public static async Task<IActionResult> Run(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;

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