NegotiateStream.LeaveInnerStreamOpen Property

public bool LeaveInnerStreamOpen { get; set; }

Gets or sets a value that indicates whether the inner stream is left open when the NegotiateStream is closed.

This property is useful when you want to reuse the inner stream after the NegotiateStream is no longer needed. For example, if you created the inner stream and want to manage its lifecycle separately, setting this property to true will prevent NegotiateStream from disposing of it.

Syntax

public bool LeaveInnerStreamOpen { get; set; }

Property Value

System.Boolean

true to leave the inner stream open after the NegotiateStream is closed; otherwise, false.

Remarks

When the NegotiateStream is disposed of, it calls the Dispose method on the inner stream by default. If you set LeaveInnerStreamOpen to true, the Dispose method of the inner stream will not be called when the NegotiateStream is disposed. This allows you to maintain control over the inner stream's lifetime.

Consider the following scenario:

The default value for this property is false.

Examples

The following code example demonstrates how to set the LeaveInnerStreamOpen property to true to prevent the inner stream from being disposed.

using System;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text;

public class NegotiateStreamExample
{
    public static void Main(string[] args)
    {
        TcpClient client = null;
        NetworkStream networkStream = null;
        NegotiateStream negotiateStream = null;

        try
        {
            // Assume a server is running and a client is connected
            // For demonstration, we'll simulate this setup.
            // In a real scenario, you would establish a connection here.

            // Simulate a connected TcpClient and its underlying stream
            // In a real application, this would come from a successful TcpClient.Connect() or AcceptTcpClient()
            Console.WriteLine("Simulating a client connection...");
            client = new TcpClient();
            // client.Connect("server_address", port); // Uncomment and configure for real connection
            networkStream = new NetworkStream(new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)); // Placeholder for real stream

            Console.WriteLine("Creating NegotiateStream...");
            // Assume authentication details are handled
            negotiateStream = new NegotiateStream(networkStream, true); // Start with LeaveInnerStreamOpen = true

            // Set LeaveInnerStreamOpen to true explicitly for clarity
            negotiateStream.LeaveInnerStreamOpen = true;

            // Authenticate (simplified for example)
            // negotiateStream.AuthenticateAsClientAsync(..., CancellationToken.None).GetAwaiter().GetResult();
            Console.WriteLine("Authentication successful (simulated).");

            // Perform secure operations...
            Console.WriteLine("Performing secure communication (simulated).");
            byte[] buffer = Encoding.UTF8.GetBytes("Hello Secure World!");
            // negotiateStream.Write(buffer, 0, buffer.Length);

            // Now, close the NegotiateStream but leave the inner stream open
            Console.WriteLine("Closing NegotiateStream, leaving inner stream open.");
            negotiateStream.Close(); // This will NOT dispose of the networkStream because LeaveInnerStreamOpen is true.

            // You can now use the networkStream for other purposes if needed
            Console.WriteLine("Inner NetworkStream is still available for use.");
            // Example: Send unencrypted data on the inner stream
            // networkStream.Write(Encoding.UTF8.GetBytes("Sending unencrypted message."), 0, Encoding.UTF8.GetBytes("Sending unencrypted message.").Length);
            // networkStream.Flush();

        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
        finally
        {
            // IMPORTANT: If LeaveInnerStreamOpen was true, YOU are responsible for disposing of the inner stream.
            Console.WriteLine("Disposing of the inner NetworkStream manually.");
            networkStream?.Dispose(); // Manually dispose the inner stream

            // The TcpClient itself might also need disposal if it holds resources.
            // client?.Dispose(); // Uncomment if TcpClient was genuinely created and connected.
        }
    }
}

Requirements

Assembly File
System.Net.Security.dll

Thread Safety

Public static members of this type are thread-safe. Any instance members are not guaranteed to be thread-safe.

See Also

NegotiateStream Class

System.Net.Security Namespace