0

I have a .NET 6 application. I start a Docker container, execute an http call to the container via HttpClient, then stop the container.

The code to stop the container does not run unless I comment out the PostAsync call.

//Start Container
var started = await dockerClient.Containers.StartContainerAsync(
    containerId,
    new ContainerStartParameters()
    );


//Call To CodeExecution API
var result = await client.PostAsync("http://localhost:5000/CodeExecution", data);
string resultContent = result.Content.ReadAsStringAsync().Result;
runCodeResultModel = JsonConvert.DeserializeObject<RunCodeResultModel>(resultContent);


//Stop Container
await dockerClient.Containers.StopContainerAsync(
    containerId,
    new ContainerStopParameters
    {
        WaitBeforeKillSeconds = 15,
    },
    CancellationToken.None);
Sam R
  • 31
  • 3
  • 10
  • Is it async all the way up the call chain? – ProgrammingLlama Jul 25 '22 at 07:59
  • Looks like there was an exception while sending the request. I forgot to put a breakpoint in my catch block. – Sam R Jul 25 '22 at 08:04
  • 4
    Also suggest to change the line to `string resultContent = await result.Content.ReadAsStringAsync();` instead of invoking `.Result` property. – user1672994 Jul 25 '22 at 08:06
  • Does this answer your question? [Why does this async action hang when I try and access the Result property of my Task?](https://stackoverflow.com/questions/14526377/why-does-this-async-action-hang-when-i-try-and-access-the-result-property-of-my) – Charlieface Jul 25 '22 at 08:11
  • It's because the docker container isn't ready when I send the request. – Sam R Jul 25 '22 at 08:16
  • If you put a breakpoint on `PostAsync` or in catch block then what is the response or error you are getting? – user1672994 Jul 25 '22 at 08:53

0 Answers0