# PortAllocator

- Kind: `class`
- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Version: `13.3.0`
- Namespace: `Aspire.Hosting.ApplicationModel`
- Target framework: `net8.0`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/PortAllocator.cs)
- Implements: [IPortAllocator](/reference/api/csharp/aspire.hosting/iportallocator.md)

Allocates and manages a range of ports for use in an application context.

## Definition

```csharp
namespace Aspire.Hosting.ApplicationModel;

public sealed class PortAllocator
    : Aspire.Hosting.ApplicationModel.IPortAllocator
{
    // ...
}
```

## Remarks

This class starts allocating ports from a specified initial port (default is 8000) and ensures that allocated ports do not overlap with ports already marked as used.

## Constructors

- [PortAllocator(int)](/reference/api/csharp/aspire.hosting/portallocator/constructors.md#constructor-int) -- Allocates and manages a range of ports for use in an application context.

## Methods

- [AddUsedPort(int)](/reference/api/csharp/aspire.hosting/portallocator/methods.md#addusedport-int) -- Marks a port as used to prevent it from being allocated.
- [AllocatePort](/reference/api/csharp/aspire.hosting/portallocator/methods.md#allocateport) : `int` -- Allocates a port that is not currently in use.
