Skip to content

Dependency injection fails to create IMessageHandler when the constructor contains additional parameters #166

@bslatner

Description

@bslatner

Describe the bug

Starting with a project created with:

dotnet new serverless.Messaging

I registered an IAmazonDynamoDB client with the IServiceCollection passed to ConfigureServices in Startup.cs. I changed the constructor definition in the provided GreetingMessageHandler to receive a registered service as a parameter and store it in an instance field.

I run the app and publish a message to the queue. The log indicates that Functions.Handler() gets called and that it, in turn, invokes _LambdaMessaging.ProcessLambdaEventWithBatchResponseAsync() without error. However, the HandleAsync method of GreetingMessageHandler never gets called.

Some investigation and additional logging shows that GreetingMessageHandler never even gets constructed. Function.Handler() winds up getting called over and over again because the message never leaves the queue.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

I would expect the framework to properly construct the IMessageHandler instance as long as the DI framework can resolve all the parameters passed to the constructor.

I would also expect that when ProcessLambdaEventWithBatchResponseAsync is invoked -- and possibly even before that -- if the message handler for the type of the message passed to it cannot be constructed, an exception should be thrown or, at the very least, an entry added to the log with information about why.

Current Behavior

Messages sent to the queue never get processed. The registered Lambda function that handles them gets invoked, but the IMessageHandler for that message type is never constructed or invoked.

Reproduction Steps

Create a new project using dotnet new serverless.Messaging

Open Startup.cs and on the line before services.AddAWSMessageBus add some instance to the DI container. In my case, I called:

services.AddAWSService<IAmazonDynamoDB>(); services.AddSingleton<IDataAccess>(x => { var ddb = x.GetRequiredService<IAmazonDynamoDB>(); return new DynamoDbDataAccess(ddb, Environment.GetEnvironmentVariable("TABLE_NAME")); });

Add a parameter for the service you added to the constructor GreetingMessageHandler . In my case, I added IDataAccess dataAccess.

Publish the stack and invoke the message sender function with a valid message.

Possible Solution

No response

Additional Information/Context

No response

AWS.Messaging (or related) package versions

Include="AWS.Messaging.Lambda" Version="0.10.0"

Targeted .NET Platform

.NET 8

Operating System and version

Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmEffort estimation: mediump2This is a standard priority issuequeued

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions