【问题标题】:Use Microsoft.Azure.Storage in Azure Function csx在 Azure Function csx 中使用 Microsoft.Azure.Storage
【发布时间】:2019-10-15 09:11:21
【问题描述】:

不知何故,我无法在 Azure Function v2 中使用 csx 包 Microsoft.Azure.Storage.blob

在 extension.proj 我有以下内容:

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

在 csx 文件中我有:

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

我有错误:

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?)

完整代码在 GitHub 上:https://github.com/ptrstpp950/cognitive-service-azure-function

【问题讨论】:

  • 我一直在这里告诉人们...
  • @silent 我不能。我必须让它们在研讨会的浏览器中可编辑。我切换到 NodeJS。在这种情况下,它就像一个魅力:)
  • 知道了。是的,最近也有同样的情况
  • @PiotrStapp 我的解决方案可以解决您的问题吗?还有其他疑问吗?

标签: c# azure azure-functions csx


【解决方案1】:

1.您确定您使用的是extension.proj吗?

根据您的代码,我知道您正在门户网站上写作。所以你应该在门户上创建function.proj 而不是extension.proj

2。我看到你在 .proj 文件中写了&lt;PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.0" /&gt;。所以你应该使用#r "Microsoft.WindowsAzure.Storage" 而不是 using Microsoft.WindowsAzure.Storage

下面是我的function.proj 的代码,我这边一切正常。更多细节,看这个Offical doc。(所有的解决方案都是基于你使用function 2.x。如果你使用function 1.x。不一样。)

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

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

我的 .crx 文件的代码:

#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");
}

【讨论】:

  • 为什么是#r Microsoft.WindowsAzure.Storage 而不是Microsoft.Azure.Storage.Blob
  • @PiotrStapp 对于框架程序集,使用 #r "AssemblyName" 指令添加引用。先添加引用再使用。或许您需要详细说明使用 .crx 文件:docs.microsoft.com/en-us/azure/azure-functions/…
  • 我的描述可能有问题,我的意思是你必须使用#r“xxx”来引用它,而不是直接使用using。这与 Visual Studio 中的不同。
【解决方案2】:

你应该在使用前导入包:

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.Extensions.Logging;

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多