@@ -54,11 +54,55 @@ public ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
5454 return WithEnvironment ( AcceptLicenseAgreementEnvVar , licenseAgreement ) ;
5555 }
5656
57+ /// <summary>
58+ /// Sets the dependent MSSQL container for the Azure Service Bus Emulator.
59+ /// </summary>
60+ /// <remarks>
61+ /// This method allows an existing MSSQL container to be attached to the Azure Service
62+ /// Bus Emulator. The containers must be on the same network to enable communication
63+ /// between them.
64+ /// </remarks>
65+ /// <param name="network">The network to connect the container to.</param>
66+ /// <param name="container">The MSSQL container.</param>
67+ /// <param name="networkAlias">The MSSQL container network alias.</param>
68+ /// <param name="password">The MSSQL container password.</param>
69+ /// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
70+ public ServiceBusBuilder WithMsSqlContainer (
71+ INetwork network ,
72+ MsSqlContainer container ,
73+ string networkAlias ,
74+ string password = MsSqlBuilder . DefaultPassword )
75+ {
76+ return Merge ( DockerResourceConfiguration , new ServiceBusConfiguration ( databaseContainer : container ) )
77+ . DependsOn ( container )
78+ . WithNetwork ( network )
79+ . WithNetworkAliases ( ServiceBusNetworkAlias )
80+ . WithEnvironment ( "SQL_SERVER" , networkAlias )
81+ . WithEnvironment ( "MSSQL_SA_PASSWORD" , password ) ;
82+ }
83+
5784 /// <inheritdoc />
5885 public override ServiceBusContainer Build ( )
5986 {
6087 Validate ( ) ;
61- return new ServiceBusContainer ( DockerResourceConfiguration ) ;
88+
89+ if ( DockerResourceConfiguration . DatabaseContainer != null )
90+ {
91+ return new ServiceBusContainer ( DockerResourceConfiguration ) ;
92+ }
93+
94+ // If the user has not provided an existing MSSQL container instance,
95+ // we configure one.
96+ var network = new NetworkBuilder ( )
97+ . Build ( ) ;
98+
99+ var container = new MsSqlBuilder ( )
100+ . WithNetwork ( network )
101+ . WithNetworkAliases ( DatabaseNetworkAlias )
102+ . Build ( ) ;
103+
104+ var serviceBusContainer = WithMsSqlContainer ( network , container , DatabaseNetworkAlias ) ;
105+ return new ServiceBusContainer ( serviceBusContainer . DockerResourceConfiguration ) ;
62106 }
63107
64108 /// <inheritdoc />
@@ -80,10 +124,7 @@ protected override ServiceBusBuilder Init()
80124 {
81125 return base . Init ( )
82126 . WithImage ( ServiceBusImage )
83- . WithNetwork ( new NetworkBuilder ( ) . Build ( ) )
84- . WithNetworkAliases ( ServiceBusNetworkAlias )
85127 . WithPortBinding ( ServiceBusPort , true )
86- . WithMsSqlContainer ( )
87128 . WithWaitStrategy ( Wait . ForUnixContainer ( )
88129 . UntilMessageIsLogged ( "Emulator Service is Successfully Up!" )
89130 . AddCustomWaitStrategy ( new WaitTwoSeconds ( ) ) ) ;
@@ -107,25 +148,9 @@ protected override ServiceBusBuilder Merge(ServiceBusConfiguration oldValue, Ser
107148 return new ServiceBusBuilder ( new ServiceBusConfiguration ( oldValue , newValue ) ) ;
108149 }
109150
110- /// <summary>
111- /// Configures the dependent MSSQL container.
112- /// </summary>
113- /// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
114- private ServiceBusBuilder WithMsSqlContainer ( )
115- {
116- var msSqlContainer = new MsSqlBuilder ( )
117- . WithNetwork ( DockerResourceConfiguration . Networks . Single ( ) )
118- . WithNetworkAliases ( DatabaseNetworkAlias )
119- . Build ( ) ;
120-
121- return Merge ( DockerResourceConfiguration , new ServiceBusConfiguration ( databaseContainer : msSqlContainer ) )
122- . WithEnvironment ( "MSSQL_SA_PASSWORD" , MsSqlBuilder . DefaultPassword )
123- . WithEnvironment ( "SQL_SERVER" , DatabaseNetworkAlias ) ;
124- }
125-
126151 /// <inheritdoc cref="IWaitUntil" />
127152 /// <remarks>
128- /// This is a workaround to ensure that the wait strategy does not indicate
153+ /// This is a workaround to ensure that the wait strategy does not indicate
129154 /// readiness too early:
130155 /// https://github.com/Azure/azure-service-bus-emulator-installer/issues/35#issuecomment-2497164533.
131156 /// </remarks>
0 commit comments