Skip to content

Commit c8ef356

Browse files
committed
Update for .NET 8
1 parent 9bd0aee commit c8ef356

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed
-41 KB
Loading
26 KB
Loading

docs/containers/tutorial-multicontainer.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ Add a project to the same solution and call it *MyWebAPI*. Select **API** as the
6666
::: moniker-end
6767

6868
:::moniker range=">=vs-2022"
69-
1. Add a project to the same solution and call it *WebAPI*. Select **API** as the project type, and clear the checkbox for **Configure for HTTPS**. In this design, we're only using SSL for communication with the client, not for communication from between containers in the same web application. Only `WebFrontEnd` needs HTTPS and the code in the examples assumes that you have cleared that checkbox. In general, the .NET developer certificates used by Visual Studio are only supported for external-to-container requests, not for container-to-container requests.
69+
1. Add a project to the same solution and call it *MyWebAPI*. Select **API** as the project type, and clear the checkbox for **Configure for HTTPS**.
70+
71+
> [!NOTE]
72+
> In this design, we're only using SSL for communication with the client, not for communication from between containers in the same web application. Only `WebFrontEnd` needs HTTPS and the code in the examples assumes that you have cleared that checkbox. In general, the .NET developer certificates used by Visual Studio are only supported for external-to-container requests, not for container-to-container requests.
7073
7174
![Screenshot of creating the Web API project.](media/tutorial-multicontainer/vs-2022/create-web-api-project.png)
7275

@@ -289,12 +292,24 @@ Congratulations, you're running a Docker Compose application with a custom Docke
289292
```csharp
290293
public async Task OnGet()
291294
{
295+
// Call *mywebapi*, and display its response in the page
292296
using (var client = new System.Net.Http.HttpClient())
293297
{
294-
// Call *mywebapi*, and display its response in the page
295298
var request = new System.Net.Http.HttpRequestMessage();
296-
// webapi is the container name
297-
request.RequestUri = new Uri("http://webapi/Counter");
299+
300+
// A delay is a quick and dirty way to work around the fact that
301+
// the mywebapi service might not be immediately ready on startup.
302+
// See the text for some ideas on how you can improve this.
303+
await System.Threading.Tasks.Task.Delay(10000);
304+
305+
// mywebapi is the service name, as listed in docker-compose.yml.
306+
// Docker Compose creates a default network with the services
307+
// listed in docker-compose.yml exposed as host names.
308+
// The port 8080 is exposed in the WebAPI Dockerfile.
309+
// If your WebAPI is exposed on port 80 (the default for HTTP, used
310+
// with earlier versions of the generated Dockerfile), change
311+
// or delete the port number here.
312+
request.RequestUri = new Uri("http://mywebapi:8080/Counter");
298313
var response = await client.SendAsync(request);
299314
string counter = await response.Content.ReadAsStringAsync();
300315
ViewData["Message"] = $"Counter value from cache :{counter}";
@@ -305,6 +320,12 @@ Congratulations, you're running a Docker Compose application with a custom Docke
305320
> [!NOTE]
306321
> In real-world code, you shouldn't dispose `HttpClient` after every request. For best practices, see [Use HttpClientFactory to implement resilient HTTP requests](/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests).
307322

323+
The Uri given references a service name defined in the *docker-compose.yml* file. Docker Compose sets up a default network for communication between containers using the listed service names as hosts.
324+
325+
The code shown here works with .NET 8 and later, which sets up a user account in the Dockerfile without administrator privileges, and exposes port 8080 because the HTTP default port 80 is not accessible without elevated privilege.
326+
327+
The delay is used here because this example code could run immediately on application launch, before the MyWebAPI service is ready to receive calls. Docker Compose provides more robust ways to handle timing issues between dependent containers that can occur during startup. For example, you can use `depends_on` to control the order in which containers are started, and you can specify a health check or other conditions that must be met before a service is considered ready, before starting the next service. See [Control startup order in Docker Compose](https://docs.docker.com/compose/how-tos/startup-order).
328+
308329
1. In the `Index.cshtml` file, add a line to display `ViewData["Message"]` so that the file looks like the following code:
309330

310331
```cshtml
@@ -329,7 +350,11 @@ Congratulations, you're running a Docker Compose application with a custom Docke
329350

330351
1. Choose **Docker Compose**.
331352

332-
1. Choose your Target OS, for example, Linux.
353+
1. **Visual Studio 17.12 and later** Choose the scaffolding options for the WebFrontEnd project.
354+
355+
![Screenshot showing Container Scaffolding Options dialog for the WebFrontEnd project.](media/tutorial-multicontainer/vs-2022/webfrontend-container-options.png)
356+
357+
**Visual Studio 17.11 and earlier** Choose your Target OS, for example, Linux.
333358

334359
![Screenshot of choosing the Target OS.](media/tutorial-multicontainer/docker-tutorial-docker-support-options.PNG)
335360

@@ -348,8 +373,6 @@ Congratulations, you're running a Docker Compose application with a custom Docke
348373
dockerfile: WebFrontEnd/Dockerfile
349374
```
350375

351-
The `version` specified in the first line is the [Docker Compose file version](https://docs.docker.com/compose/compose-file/#version-top-level-element). You normally shouldn't change it, since it's used by the tools to understand how to interpret the file.
352-
353376
The `.dockerignore` file contains file types and extensions that you don't want Docker to include in the container. These files are generally associated with the development environment and source control, not part of the app or service you're developing.
354377

355378
Look at the **Container Tools** section of the output pane for details of the commands being run. You can see the command-line tool `docker-compose` is used to configure and create the runtime containers.

0 commit comments

Comments
 (0)