Search code examples
c#amazon-web-servicesaws-lambda

Unable to find handler in C# project


Newbie to C# and Lambda. I'm using Jetbrains rider trying to debug the below piece of code.

using System;
using System.IO;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Util;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace CROLambda
{
    
    public class Function
    {

        private static AmazonS3Client _sourceClient;
        private static AmazonS3Client _destinationClient;
        
        public static void Main()
        {
            Console.WriteLine("Done");
        }
    }
}

I'm trying to run/debug the above code through Rider using the below config. I get an error stating that it cannot find handler "Main" in the project. Any idea? enter image description here

Below is the aws-lambda-tools-defaults.json content.

{
  "profile": "",
  "region": "",
  "configuration": "Release",
  "template": "template.yaml",
  "function-handler": "CROLambda::CROLambda.Function::Main"
}

Solution

  • The function here isn't intended to be the entry point for an application (i.e. no static Main method needed). That's taken care of by AWS Lambda's hosting mechanism.

    You just need a standard instance method. For example:

    public void FunctionHandler()
    {
    }
    

    or

    public async Task FunctionHandler()
    {
    }
    

    There's also different context types you can accept, and then corresponding types you should return. Docs.