引用多个 c# 项目时 Firebase 函数构建错误

Firebase functions build error when referencing multiple c# projects

我能够毫无问题地部署我的 firebase c# 函数,但是,当我引用另一个 c# 项目以便我可以使用另一个对象时,我收到错误消息说项目不存在。

因此能够毫无问题地部署以下内容:

namespace CloudFunctions
{
    public class Login : IHttpFunction
    {
        public async Task HandleAsync(HttpContext context) {
            await context.Response.WriteAsync("Hello World!");
        }
    }
}

这个 class 住在一个名为 CloudFunctions 的项目中。我添加了对名为服务的项目的项目引用,以便我可以调用登录服务,但出现以下错误:

The referenced project '../Services/Services.csproj' does not exist

这就是我的部署方式:

gcloud functions deploy login --entry-point CloudFunctions.Login --runtime dotnet3 --trigger-http --allow-unauthenticated

我无法想象我们需要将所有内容都放在一个项目中才能进行部署?

您需要使所有项目都可用于 buildpack(即从父目录部署),但还要使用 GOOGLE_BUILDABLE 构建时环境变量指定包含入口点的项目。

来自deployment documentation in the Functions Framework GitHub repo

Real world functions are often part of a larger application which will usually contain common code for data types and common business logic. If your function depends on another project via a local project reference (a <ProjectReference> element in your .csproj file), the source code for that project must also be included when deploying to Google Cloud Functions. Additionally, you need to specify which project contains the function you wish to deploy.

In a typical directory structure where all projects sit side-by-side within one top-level directory, this will mean you need to deploy from that top-level directory instead of from the function's project directory. You also need to use the --set-build-env-vars command line flag to specify the GOOGLE_BUILDABLE build-time environment variable. This tells the Google Cloud Functions deployment process which project to build and deploy. Note that the GOOGLE_BUILDABLE environment variable value is case-sensitive, and should match the directory and file names used.

When deploying a function with multiple projects, it's important to make sure you have a suitable .gcloudignore file, so that you only upload the code that you want to. In particular, you should almost always include bin/ and obj/ in the .gcloudignore file so that you don't upload your locally-built binaries.

来自 examples 目录的示例部署命令行:

gcloud functions deploy multi-project \
  --runtime dotnet3 \
  --trigger-http \
  --entry-point=Google.Cloud.Functions.Examples.MultiProjectFunction.Function \
  --set-build-env-vars=GOOGLE_BUILDABLE=Google.Cloud.Functions.Examples.MultiProjectFunction