GoogleCloudPlatform/functions-framework-dotnet

Deployment failed

brian-sysmirror opened this issue · 2 comments

Steps

  1. Create and deploy the default cloud function "function-1" for .net core 3.1 using the browser
  2. Open Visual Studio 2019
  3. Create a class library project using .NET Core 3.1 called SimpleHttpFunction
  4. Add the Function class (see below)
  5. Add a reference to "Google.Cloud.Functions.Framework (1.0.0-beta02) using NuGet
  6. Open a command prompt to the project folder
  7. Run "gloud auth login"
  8. Run "gcloud config set project {project_name_and_id}"
  9. Run "gcloud functions deploy function-1 --entry-point SimpleHttpFunction.Function --runtime dotnet3 --trigger-http --allow-unauthenticated"

Here's the output of the command.

Deploying function (may take a while - up to 2 minutes)...-
For Cloud Build Stackdriver Logs, visit: {removed}
Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.

Function class:

using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;

namespace SimpleHttpFunction
{
    public class Function : IHttpFunction
    {
        private readonly ILogger _logger;

        public Function(ILogger<Function> logger) =>
            _logger = logger;

        public async Task HandleAsync(HttpContext context)
        {
            HttpRequest request = context.Request;
            // Check URL parameters for "message" field
            string message = request.Query["message"];

            // If there's a body, parse it as JSON and check for "message" field.
            using TextReader reader = new StreamReader(request.Body);
            string text = await reader.ReadToEndAsync();
            if (text.Length > 0)
            {
                try
                {
                    JsonElement json = JsonSerializer.Deserialize<JsonElement>(text);
                    if (json.TryGetProperty("message", out JsonElement messageElement) &&
                        messageElement.ValueKind == JsonValueKind.String)
                    {
                        message = messageElement.GetString();
                    }
                }
                catch (JsonException parseException)
                {
                    _logger.LogError(parseException, "Error parsing JSON request");
                }
            }

            await context.Response.WriteAsync(message ?? "Hello World 2!");
        }
    }
}

I can't reproduce this - I've just tried exactly your code and your deployment command line, and it was fine.

Did you have a look at the logs as suggested in the output? If you could include those logs here it would help us to diagnose what's wrong. (If you could include your project file as well, that would help.)

If you run locally with dotnet run does the function start okay?

Closing due to lack of further information. If this is still causing problems, please add another comment with more information and I'll reopen it.