Closed fscavo closed 2 years ago
@fscavo First thing I'd have to ask is, if the actor have a zero constructor and you don't have any EventWorkflowOptions
services registered in the dependency injection service provider, why are you using DependencyResolver
and not a normal Props? I.e. why bother using dependency injection if there are no dependency to inject?
Second this bug is located in dotnet itself. Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.Match(object[] args)
returns the very first constructor it inspect when you pass an empty array as args. We could possibly try and fix this internally in our code, but it is a big can of worm because it has a lot of edge cases. @Aaronontheweb what do you think?
Marking this as closed - issue is upstream inside Microsoft.Extensions.DependencyInjection
I'd like a little more investigation into this issue to see if it's our use of Microsoft.Extensions.DependencyInjection that is at-fault here.
Have another data point - user with the following constructor ran into the same failure:
public DistributedPubSubToEventStreamActor(IDistributedPubSub distributedPubSub, string[] subscribeToEvents)
Looks like in this case IDistributedPubSub
is passed in via DI but the events are passed in as CTOR args via the usual route inside our DI.
It was a different issue. This specific issue can be tested with this simple console app:
class Program
{
static void Main(string[] args)
{
var provider = new ServiceCollection().BuildServiceProvider();
ActivatorUtilities.CreateInstance(provider, typeof(GoodClass), Array.Empty<object>());
Console.WriteLine($"{nameof(GoodClass)} created");
ActivatorUtilities.CreateInstance(provider, typeof(FailClass), Array.Empty<object>());
Console.WriteLine($"{nameof(FailClass)} created");
}
}
public class GoodClass
{
public GoodClass()
{}
public GoodClass(string arg1)
{}
}
public class FailClass
{
public FailClass(string arg1)
{}
public FailClass()
{}
}
Output:
PS > dotnet run
GoodClass created
Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate 'Repro.FailClass'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Repro.Program.Main(String[] args) in D:\Repro\Program.cs:line 14
Note the very specific exception message:
System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate 'Repro.FailClass'.
What if the array is populated?
Version Information Version of Akka.NET? 1.4.32 Which Akka.NET Modules? Akka.Cluster Akka.Cluster.Sharding
I have an actor with the following constructors defined in this order:
And this is how I create the actor:
DependencyResolver tries to invoke the first constructor (with parameters), though args is empty. If I change the order of constructors definition to be like the following, it works:
This is the exception that throws during actor creation: