Skip to content

Commit f6cd6a0

Browse files
docs: clarify hosting API on accessible ports for Microcks container (#62)
Users reported difficulties with this setup. Added guidance to address it. Signed-off-by: SebastienDegodez <[email protected]>
1 parent 1e24d4a commit f6cd6a0

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

README.md

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ You just have to specify the container image you'd like to use. This library req
5959

6060
```csharp
6161
MicrocksContainer container = new MicrocksBuilder()
62-
.WithImage("quay.io/microcks/microcks-uber:1.11.0")
63-
.Build();
62+
.WithImage("quay.io/microcks/microcks-uber:1.11.0")
63+
.Build();
6464
await container.StartAsync();
6565
```
6666

@@ -73,9 +73,9 @@ You can do it before starting the container using simple paths:
7373

7474
```csharp
7575
MicrocksContainer container = new MicrocksBuilder()
76-
.WithMainArtifacts("apipastries-openapi.yaml")
77-
.WithSecondaryArtifacts("apipastries-postman-collection.json")
78-
.Build();
76+
.WithMainArtifacts("apipastries-openapi.yaml")
77+
.WithSecondaryArtifacts("apipastries-postman-collection.json")
78+
.Build();
7979
await container.StartAsync();
8080
```
8181

@@ -90,8 +90,8 @@ You can also import full [repository snapshots](https://microcks.io/documentatio
9090

9191
```csharp
9292
MicrocksContainer container = new MicrocksBuilder()
93-
.WithSnapshots("microcks-repository.json")
94-
.Build();
93+
.WithSnapshots("microcks-repository.json")
94+
.Build();
9595
await container.StartAsync();
9696
```
9797

@@ -119,8 +119,8 @@ private int port;
119119
public async Task InitializeAsync()
120120
{
121121
container = new MicrocksBuilder()
122-
.WithExposedPort(port)
123-
.Build();
122+
.WithExposedPort(port)
123+
.Build();
124124
await container.StartAsync();
125125
}
126126

@@ -142,6 +142,67 @@ public async Task testOpenAPIContract()
142142

143143
The `TestResult` gives you access to all details regarding success of failure on different test cases.
144144

145+
**Important:** You must host the API on a port that is accessible from the Microcks container. If your tests are using WebApplicationFactory, the API is hosted on an in-memory server and does not expose a port.
146+
147+
One way to do this is to specify a URL to the WebApplication. However, this requires not to use the minimal hosting model (Program.cs without Main and without Startup.cs).
148+
149+
Refactor your `Program.cs` to use Main and create a new class ApplicationBuilder for example, and copy the content of your `Program.cs` into it.
150+
151+
See below an example of a minimal hosting model with a `Main` method:
152+
```csharp
153+
public class Program
154+
{
155+
public static void Main(string[] args)
156+
{
157+
var app = ApplicationBuilder.Create(args);
158+
app.Run();
159+
}
160+
}
161+
```
162+
163+
Then, in your `ApplicationBuilder` class:
164+
165+
```csharp
166+
public static WebApplication Create(params string[] args)
167+
{
168+
var builder = WebApplication.CreateBuilder(args);
169+
170+
builder.Services.AddControllers();
171+
// Example: Configure OpenTelemetry, HTTP clients, controllers, Swagger, etc.
172+
// ...existing code...
173+
174+
var app = builder.Build();
175+
176+
app.MapControllers();
177+
178+
// Example: Enable Swagger in development, HTTPS redirection, authorization, etc.
179+
// ...existing code...
180+
181+
return app;
182+
}
183+
```
184+
Finally, in your test class, you can use the `ApplicationBuilder` to create a new instance of your application and specify the URL:
185+
186+
```csharp
187+
using var app = ApplicationBuilder.Create();
188+
app.Urls.Add("http://127.0.0.1:0");
189+
await app.StartAsync();
190+
// Get the port assigned by Kestrel
191+
var port = app.Services.GetRequiredService<IServer>()
192+
.Features
193+
.Get<IServerAddressesFeature>()
194+
.Addresses
195+
.First()
196+
.Split(':')
197+
.Last();
198+
199+
// Expose the port to the Microcks container
200+
IEnumerable<ushort> ports = [(ushort)port];
201+
await TestcontainersSettings.ExposeHostPortsAsync(ports)
202+
.ConfigureAwait(false);
203+
```
204+
205+
145206
### Advanced features with MicrocksContainersEnsemble
146207

147208
The `MicrocksContainer` referenced above supports essential features of Microcks provided by the main Microcks container.

0 commit comments

Comments
 (0)