Skip to content

Commit 0c85300

Browse files
committed
Fixed #3: Allow additional ActorService constructor parameters to be passed through from RegisterActor registration method.
1 parent a81e910 commit 0c85300

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

src/Autofac.Integration.ServiceFabric/ActorFactoryRegistration.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2424
// OTHER DEALINGS IN THE SOFTWARE.
2525

26+
using System;
2627
using Microsoft.ServiceFabric.Actors.Runtime;
2728

2829
namespace Autofac.Integration.ServiceFabric
2930
{
3031
// ReSharper disable once ClassNeverInstantiated.Global
3132
internal sealed class ActorFactoryRegistration : IActorFactoryRegistration
3233
{
33-
public void RegisterActorFactory<TActor>(ILifetimeScope container) where TActor : ActorBase
34+
public void RegisterActorFactory<TActor>(
35+
ILifetimeScope container,
36+
Func<ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = null,
37+
IActorStateProvider stateProvider = null,
38+
ActorServiceSettings settings = null) where TActor : ActorBase
3439
{
3540
ActorRuntime.RegisterActorAsync<TActor>((context, actorTypeInfo) =>
3641
{
@@ -41,7 +46,7 @@ public void RegisterActorFactory<TActor>(ILifetimeScope container) where TActor
4146
TypedParameter.From(actorService),
4247
TypedParameter.From(actorId));
4348
return actor;
44-
});
49+
}, stateManagerFactory, stateProvider, settings);
4550
}).GetAwaiter().GetResult();
4651
}
4752
}

src/Autofac.Integration.ServiceFabric/AutofacActorExtensions.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@ public static class AutofacActorExtensions
3737
/// Registers an actor service with the container.
3838
/// </summary>
3939
/// <param name="builder">The container builder.</param>
40+
/// <param name="stateManagerFactory">A factory method to create <see cref="T:Microsoft.ServiceFabric.Actors.Runtime.IActorStateManager" /></param>
41+
/// <param name="stateProvider">State provider to store the state for actor objects.</param>
42+
/// <param name="settings">/// Settings to configures behavior of Actor Service.</param>
4043
/// <typeparam name="TActor">The type of the actor to register.</typeparam>
4144
/// <exception cref="ArgumentException">Thrown when <typeparamref name="TActor"/> is not a valid actor type.</exception>
4245
/// <remarks>The actor will be wrapped in a dynamic proxy and must be public and not sealed.</remarks>
43-
public static void RegisterActor<TActor>(this ContainerBuilder builder) where TActor : ActorBase
46+
public static void RegisterActor<TActor>(
47+
this ContainerBuilder builder,
48+
Func<ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = null,
49+
IActorStateProvider stateProvider = null,
50+
ActorServiceSettings settings = null) where TActor : ActorBase
4451
{
4552
if (builder == null)
4653
throw new ArgumentNullException(nameof(builder));
@@ -52,7 +59,9 @@ public static void RegisterActor<TActor>(this ContainerBuilder builder) where TA
5259

5360
builder.RegisterServiceWithInterception<TActor, ActorInterceptor>();
5461

55-
builder.RegisterBuildCallback(c => c.Resolve<IActorFactoryRegistration>().RegisterActorFactory<TActor>(c));
62+
builder.RegisterBuildCallback(
63+
c => c.Resolve<IActorFactoryRegistration>().RegisterActorFactory<TActor>(
64+
c, stateManagerFactory, stateProvider, settings));
5665
}
5766
}
5867
}

src/Autofac.Integration.ServiceFabric/IActorFactoryRegistration.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2424
// OTHER DEALINGS IN THE SOFTWARE.
2525

26+
using System;
2627
using Microsoft.ServiceFabric.Actors.Runtime;
2728

2829
namespace Autofac.Integration.ServiceFabric
2930
{
3031
internal interface IActorFactoryRegistration
3132
{
32-
void RegisterActorFactory<TActor>(ILifetimeScope lifetimeScope) where TActor : ActorBase;
33+
void RegisterActorFactory<TActor>(
34+
ILifetimeScope lifetimeScope,
35+
Func<ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = null,
36+
IActorStateProvider stateProvider = null,
37+
ActorServiceSettings settings = null) where TActor : ActorBase;
3338
}
3439
}

test/Autofac.Integration.ServiceFabric.Test/AutofacActorExtensionsTests.cs

+48-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void RegisterActorAddsFactoryCallback()
6464

6565
var container = builder.Build();
6666

67-
factoryMock.Verify(x => x.RegisterActorFactory<Actor1>(container), Times.Once);
67+
factoryMock.Verify(x => x.RegisterActorFactory<Actor1>(container, null, null, null), Times.Once);
6868
}
6969

7070
[Fact]
@@ -90,7 +90,53 @@ public void RegisterActorCanBeCalledFromModuleLoad()
9090
var container = builder.Build();
9191

9292
container.AssertRegistered<Actor1>();
93-
factoryMock.Verify(x => x.RegisterActorFactory<Actor1>(container), Times.Once);
93+
factoryMock.Verify(x => x.RegisterActorFactory<Actor1>(container, null, null, null), Times.Once);
94+
}
95+
96+
[Fact]
97+
public void RegisterActorCanBeCalledWithStateManagerFactory()
98+
{
99+
var builder = new ContainerBuilder();
100+
// ReSharper disable once ConvertToLocalFunction
101+
Func<ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = (actor, provider) => null;
102+
builder.RegisterActor<Actor1>(stateManagerFactory: stateManagerFactory);
103+
var factoryMock = new Mock<IActorFactoryRegistration>();
104+
builder.RegisterInstance(factoryMock.Object);
105+
106+
var container = builder.Build();
107+
108+
container.AssertRegistered<Actor1>();
109+
factoryMock.Verify(x => x.RegisterActorFactory<Actor1>(container, stateManagerFactory, null, null), Times.Once);
110+
}
111+
112+
[Fact]
113+
public void RegisterActorCanBeCalledWithStateProvider()
114+
{
115+
var builder = new ContainerBuilder();
116+
var stateProvider = new Mock<IActorStateProvider>().Object;
117+
builder.RegisterActor<Actor1>(stateProvider: stateProvider);
118+
var factoryMock = new Mock<IActorFactoryRegistration>();
119+
builder.RegisterInstance(factoryMock.Object);
120+
121+
var container = builder.Build();
122+
123+
container.AssertRegistered<Actor1>();
124+
factoryMock.Verify(x => x.RegisterActorFactory<Actor1>(container, null, stateProvider, null), Times.Once);
125+
}
126+
127+
[Fact]
128+
public void RegisterActorCanBeCalledWithSettings()
129+
{
130+
var builder = new ContainerBuilder();
131+
var settings = new ActorServiceSettings();
132+
builder.RegisterActor<Actor1>(settings: settings);
133+
var factoryMock = new Mock<IActorFactoryRegistration>();
134+
builder.RegisterInstance(factoryMock.Object);
135+
136+
var container = builder.Build();
137+
138+
container.AssertRegistered<Actor1>();
139+
factoryMock.Verify(x => x.RegisterActorFactory<Actor1>(container, null, null, settings), Times.Once);
94140
}
95141

96142
[Fact]

0 commit comments

Comments
 (0)