From 7b43b30567a53cc826afb8607c32f1fe6075b46e Mon Sep 17 00:00:00 2001 From: "nero.wang" Date: Thu, 18 May 2023 16:59:00 +0800 Subject: [PATCH] Add registering actor from Type in runtime Signed-off-by: nero.wang --- .../Runtime/ActorRegistrationCollection.cs | 21 ++++++++++++++++++- .../Runtime/ActorRuntimeTests.cs | 17 +++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Dapr.Actors/Runtime/ActorRegistrationCollection.cs b/src/Dapr.Actors/Runtime/ActorRegistrationCollection.cs index 69c01646d..f55dd6f01 100644 --- a/src/Dapr.Actors/Runtime/ActorRegistrationCollection.cs +++ b/src/Dapr.Actors/Runtime/ActorRegistrationCollection.cs @@ -78,7 +78,26 @@ public void RegisterActor(string actorTypeName, Action(string actorTypeName, ActorRuntimeOptions typeOptions, Action configure = null) where TActor : Actor { - var actorTypeInfo = ActorTypeInformation.Get(typeof(TActor), actorTypeName); + RegisterActor(typeof(TActor), actorTypeName, typeOptions, configure); + } + + /// + /// Registers an actor type in the collection. + /// + /// Type of actor. + /// The name of the actor type represented by the actor. + /// An optional that defines values for this type alone. + /// An optional delegate used to configure the actor registration. + /// The value of will have precedence over the default actor type name derived from the actor implementation type or any type name set via . + /// The specified actionType is not a subclass of Actor. + public void RegisterActor(Type actionType, string actorTypeName, ActorRuntimeOptions typeOptions, Action configure = null) + { + if (!actionType.IsSubclassOf(typeof(Actor))) + { + throw new ArgumentException("ActionType must be a subclass of Actor.", nameof(actionType)); + } + + var actorTypeInfo = ActorTypeInformation.Get(actionType, actorTypeName); var registration = new ActorRegistration(actorTypeInfo, typeOptions); configure?.Invoke(registration); this.Add(registration); diff --git a/test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs b/test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs index 52ae4aa7b..102a93453 100644 --- a/test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs +++ b/test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs @@ -41,6 +41,19 @@ private interface ITestActor : IActor { } + [Fact] + public void TestRegisterActorFromTypeInRuntime() + { + var actorType = typeof(TestActor); + + var options = new ActorRuntimeOptions(); + options.Actors.RegisterActor(actorType, null, null); + var runtime = new ActorRuntime(options, loggerFactory, activatorFactory, proxyFactory); + + Assert.Contains(actorType.Name, runtime.RegisteredActors.Select(a => a.Type.ActorTypeName), StringComparer.InvariantCulture); + Assert.Throws(() => options.Actors.RegisterActor(typeof(FakeActor), null, null)); + } + [Fact] public void TestInferredActorType() { @@ -316,6 +329,10 @@ public async Task TestActorSettingsWithPerActorConfigurations() Assert.Equal(32, element.GetInt32()); } + private sealed class FakeActor + { + } + private sealed class TestActor : Actor, ITestActor { public TestActor(ActorHost host)