public async Task WaitForConnectionAsync()

in src/ServiceProfiler.EventPipe.Otel/Microsoft.ApplicationInsights.Profiler.Shared/Services/IPC/DuplexNamedPipeService.cs [49:91]


    public async Task WaitForConnectionAsync(string pipeName, CancellationToken cancellationToken)
    {
        _logger.LogTrace("{methodName} on namedpipe: {pipeName}", nameof(WaitForConnectionAsync), pipeName);
        if (string.IsNullOrEmpty(pipeName))
        {
            throw new ArgumentException($"'{nameof(pipeName)}' cannot be null or empty.", nameof(pipeName));
        }

        try
        {
            using CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource(_options.ConnectionTimeout);
            using CancellationTokenSource linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutCancellationTokenSource.Token, cancellationToken);
            cancellationToken = linkedCancellationTokenSource.Token;

            await _threadSafeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
            switch (_currentMode)
            {
                case NamedPipeRole.Client:
                    throw new InvalidOperationException("Can't wait for connection on a client.");
                case NamedPipeRole.Server:
                    throw new InvalidOperationException("Can't setup a server for a second time.");
                case NamedPipeRole.NotSpecified:
                default:
                    // Good to proceed forward
                    break;
            }

            _currentMode = NamedPipeRole.Server;
            PipeName = pipeName;
            _logger.LogDebug("Named pipe stream initialization done. Role: {role}, PipeName: {pipeName}", _currentMode, PipeName);

            NamedPipeServerStream serverStream = new NamedPipeServerStream(PipeName, PipeDirection.InOut, maxNumberOfServerInstances: 1, transmissionMode: PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
            _pipeStream = serverStream;

            _logger.LogTrace("NamedPipe {} waiting for connection...", _currentMode);
            await serverStream.WaitForConnectionAsync(cancellationToken).ConfigureAwait(false);
            _logger.LogTrace("NamedPipe {} closed.", _currentMode);
        }
        finally
        {
            _threadSafeLock.Release();
        }
    }