metalTools.PortDiscovery 26.10.11.1

dotnet add package metalTools.PortDiscovery --version 26.10.11.1
                    
NuGet\Install-Package metalTools.PortDiscovery -Version 26.10.11.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="metalTools.PortDiscovery" Version="26.10.11.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="metalTools.PortDiscovery" Version="26.10.11.1" />
                    
Directory.Packages.props
<PackageReference Include="metalTools.PortDiscovery" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add metalTools.PortDiscovery --version 26.10.11.1
                    
#r "nuget: metalTools.PortDiscovery, 26.10.11.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package metalTools.PortDiscovery@26.10.11.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=metalTools.PortDiscovery&version=26.10.11.1
                    
Install as a Cake Addin
#tool nuget:?package=metalTools.PortDiscovery&version=26.10.11.1
                    
Install as a Cake Tool

metalTools.PortDiscovery

Small .NET 10 helpers for choosing a TCP port within an inclusive range.

TcpPortRange validates the range and returns candidates in randomized order, with an optional preferred port first. PortBinder.Bind tries those candidates using your application's actual bind callback and returns the successfully bound port. It throws InvalidOperationException if every candidate returns false.

using metalTools.PortDiscovery;

int port = PortBinder.Bind(
    new TcpPortRange(51200, 51299),
    tryBind: port => server.TryBind(port),
    preferredPort: 51200);

Here, server.TryBind represents your transport adapter: return true only after the server has bound and retained the port; return false when the candidate is unavailable. Clean up any partial resources before returning false. Unexpected exceptions propagate immediately. The caller owns and disposes the server.

This avoids the race in probing a port, releasing it, and binding later. The helper does not create sockets, reserve ports separately, or discover remote services. Clients still need the selected endpoint through configuration or a discovery mechanism.

You can also use range.GetCandidates(preferredPort: 51200) directly. A preferred port outside the range is ignored. If a constrained range is unnecessary, bind to port zero and let the OS assign a port instead.

See the binding guide for a TCP listener example and an EventPump integration proposal.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on metalTools.PortDiscovery:

Package Downloads
metalTools

Convenience package for LoggerLite, PortDiscovery, StateMachine and ServiceDiscovery.

metalTools.ConnectionManagement.NetMq

NetMQ request ownership, discovery, heartbeat supervision and client session leases for .NET 10.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.10.11.1 135 10/11/2026
26.10.10.1232 164 10/10/2026
26.9.29.13 119 9/29/2026