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
<PackageReference Include="metalTools.PortDiscovery" Version="26.10.11.1" />
<PackageVersion Include="metalTools.PortDiscovery" Version="26.10.11.1" />
<PackageReference Include="metalTools.PortDiscovery" />
paket add metalTools.PortDiscovery --version 26.10.11.1
#r "nuget: metalTools.PortDiscovery, 26.10.11.1"
#:package metalTools.PortDiscovery@26.10.11.1
#addin nuget:?package=metalTools.PortDiscovery&version=26.10.11.1
#tool nuget:?package=metalTools.PortDiscovery&version=26.10.11.1
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 | Versions 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. |
-
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 |