@@ -18,7 +18,15 @@ public class When_custom_provider_registered : NServiceBusAcceptanceTest
1818 public async Task Should_be_used ( )
1919 {
2020 Context context = await Scenario . Define < Context > ( )
21- . WithEndpoint < EndpointWithCustomProvider > ( b => b . When ( session => session . SendLocal ( new StartSaga1 { DataId = Guid . NewGuid ( ) } ) ) )
21+ . WithEndpoint < EndpointWithCustomProvider > ( b =>
22+ {
23+ b . Services ( c =>
24+ c . AddSingleton < IMongoClientProvider > ( b => new CustomProvider ( b . GetRequiredService < Context > ( ) ) ) , afterStart : true ) ;
25+ b . When ( session => session . SendLocal ( new StartSaga1
26+ {
27+ DataId = Guid . NewGuid ( )
28+ } ) ) ;
29+ } )
2230 . Done ( c => c . SagaReceivedMessage )
2331 . Run ( ) ;
2432
@@ -34,11 +42,7 @@ public class Context : ScenarioContext
3442 public class EndpointWithCustomProvider : EndpointConfigurationBuilder
3543 {
3644 public EndpointWithCustomProvider ( ) =>
37- EndpointSetup < DefaultServer > ( config =>
38- {
39- config . RegisterComponents ( c =>
40- c . AddSingleton < IMongoClientProvider > ( b => new CustomProvider ( b . GetRequiredService < Context > ( ) ) ) ) ;
41- } ) ;
45+ EndpointSetup < DefaultServer > ( ) ;
4246
4347 public class JustASaga ( Context testContext ) : Saga < JustASagaData > , IAmStartedByMessages < StartSaga1 >
4448 {
@@ -53,30 +57,30 @@ public Task Handle(StartSaga1 message, IMessageHandlerContext context)
5357 protected override void ConfigureHowToFindSaga ( SagaPropertyMapper < JustASagaData > mapper ) => mapper . MapSaga ( s => s . DataId ) . ToMessage < StartSaga1 > ( m => m . DataId ) ;
5458 }
5559
56- public class CustomProvider ( Context testContext ) : IMongoClientProvider
60+ public class JustASagaData : ContainSagaData
61+ {
62+ public virtual Guid DataId { get ; set ; }
63+ }
64+ }
65+
66+ public class CustomProvider ( Context testContext ) : IMongoClientProvider
67+ {
68+ [ field: AllowNull , MaybeNull ]
69+ public IMongoClient Client
5770 {
58- [ field: AllowNull , MaybeNull ]
59- public IMongoClient Client
71+ get
6072 {
61- get
73+ if ( field is not null )
6274 {
63- if ( field is not null )
64- {
65- return field ;
66- }
67-
68- var containerConnectionString = Environment . GetEnvironmentVariable ( "NServiceBusStorageMongoDB_ConnectionString" ) ;
69-
70- field = string . IsNullOrWhiteSpace ( containerConnectionString ) ? new MongoClient ( ) : new MongoClient ( containerConnectionString ) ;
71- testContext . ProviderWasCalled = true ;
7275 return field ;
7376 }
74- }
75- }
7677
77- public class JustASagaData : ContainSagaData
78- {
79- public virtual Guid DataId { get ; set ; }
78+ var containerConnectionString = Environment . GetEnvironmentVariable ( "NServiceBusStorageMongoDB_ConnectionString" ) ;
79+
80+ field = string . IsNullOrWhiteSpace ( containerConnectionString ) ? new MongoClient ( ) : new MongoClient ( containerConnectionString ) ;
81+ testContext . ProviderWasCalled = true ;
82+ return field ;
83+ }
8084 }
8185 }
8286
0 commit comments