【问题标题】:Reference file from Azure function 2.0 with .net core来自带有 .net 核心的 Azure 函数 2.0 的参考文件
【发布时间】:2018-07-24 17:27:13
【问题描述】:

我正在尝试创建一个从 .mmdb 文件 GeoLite2 Country DB 读取的 Azure 函数

我在函数旁边添加了 geolite2 文件。但是我找不到以编程方式引用文件路径的方法,以便它在我的本地机器上保持不变以及部署/发布。

string GeoLocationDbPath = "D:<path_to_project>\Functions\GeoLocation-Country.mmdb"
var reader = new DatabaseReader($"{GeoLocationDbPath}");

我偶然发现了这篇文章How to add assembly references to an Azure Function App

我希望有更好的方法来引用本地和部署的文件。

有什么想法吗?


我看过的其他链接:

How to add and reference a external file in Azure Function

How to add a reference to an Azure Function C# project?

Retrieving information about the currently running function

Azure functions – Read file and use SendGrid to send an email

【问题讨论】:

    标签: azure asp.net-core azure-functions filereference geolite2


    【解决方案1】:

    您可以通过将ExecutionContext 注入您的函数来获取文件夹的路径:

    public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context)
    {
        var funcPath = context.FunctionDirectory; // e.g. d:\home\site\wwwroot\HttpTrigger1
        var appPath = context.FunctionAppDirectory; // e.g. d:\home\site\wwwroot
        // ...
    }
    

    【讨论】:

    • 是的,但是 .mmdb 文件在服务器上不存在。我相信我需要做一个额外的构建步骤才能将其复制到正确的目录??
    • 在 Kudu 中,我可以查看该目录 D:\home\site\wwwroot\&lt;myfunction&gt;ls function.json 我的 .mmdb 文件不存在
    • @Sarah 您可以将文件添加到 Visual Studio 项目中,将其标记为内容并复制到输出
    • 啊,好吧,我找到了一个有效的内部方法。所以我没有检查“复制到输出”是否有效。希望这对偶然发现此问题的其他人有所帮助:)
    【解决方案2】:

    我得出的结论是,引用 Azure 函数的本地文件不是一个好方法。我阅读了Azure functions best practices,Azure 函数本来就是无状态的。此外,部署时文件夹结构也会发生变化。

    如果我要继续,我会将 .mmdb 文件上传到 Azure blob 容器并使用 CloudStorageAccount 访问该文件。

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 1970-01-01
      • 2023-04-02
      • 2018-02-15
      • 1970-01-01
      • 2018-06-26
      • 2018-10-05
      • 2017-06-25
      • 2018-09-10
      相关资源
      最近更新 更多