# PortAllocator Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [PortAllocator](/reference/api/csharp/aspire.hosting/portallocator.md)
- Kind: `Methods`
- Members: `2`

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

## AddUsedPort(int)

- Name: `AddUsedPort(int)`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/PortAllocator.cs#L35-L36)

Marks a port as used to prevent it from being allocated.

```csharp
public sealed class PortAllocator
{
    public void AddUsedPort(
        int port)
    {
        // ...
    }
}
```

## Parameters

- `port` (`int`)
  The port number to mark as used.

## AllocatePort

- Name: `AllocatePort`
- Returns: `int`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/PortAllocator.cs#L21-L28)

Allocates a port that is not currently in use.

```csharp
public sealed class PortAllocator
{
    public int AllocatePort()
    {
        // ...
    }
}
```

## Returns

`int` -- An available port number.
