Search code examples
c#firebasegcloudserverless

Firebase functions build error when referencing multiple c# projects


I was able to deploy my firebase c# function with no issues, however, when i referenced another c# project so that i could utilize another object i get error saying project doesn't exist.

So was able to deploy following with no problem:

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

This class lives in a project called CloudFunctions. I added a project reference to a project called Services so that i could call the login service and i get the following error:

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

This is how i am deploying:

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

I can't imagine we would be required to have everything in one project in order to deploy?


Solution

  • You need to make all of the projects available to the buildpack (i.e. deploy from the parent directory) but specify the project that contains the entry point as well, using the GOOGLE_BUILDABLE build-time environment variable.

    From the 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.

    Sample deployment command line from the examples directory:

    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