< Summary - Kestrun — Combined Coverage

Information
Class: Public.Server.Add-KrEndpoint
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Add-KrEndpoint.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 33
Coverable lines: 33
Total lines: 144
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 10/13/2025 - 16:52:37 Line coverage: 0% (0/31) Total lines: 138 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e12/12/2025 - 17:27:19 Line coverage: 0% (0/33) Total lines: 142 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd12/18/2025 - 21:41:58 Line coverage: 0% (0/33) Total lines: 144 Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Add-KrEndpoint.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Creates a new Kestrun server instance with specified options and listeners.
 4    .DESCRIPTION
 5        This function initializes a new Kestrun server instance, allowing configuration of various options and listeners
 6    .PARAMETER Server
 7        The Kestrun server instance to configure. This parameter is Mandatory and must be a valid server object.
 8    .PARAMETER Port
 9        The port on which the server will listen for incoming requests. The default is 0, which means a random available
 10    .PARAMETER IPAddress
 11        The IP address on which the server will listen. Defaults to [System.Net.IPAddress]::Any, which means it will lis
 12    .PARAMETER HostName
 13        The hostname for the listener. This parameter is Mandatory if using the 'HostName' parameter set.
 14    .PARAMETER Uri
 15        The full URI for the listener. This parameter is Mandatory if using the 'Uri' parameter set.
 16    .PARAMETER AddressFamily
 17        An array of address families to filter resolved addresses (e.g., IPv4-only). This parameter is optional.
 18    .PARAMETER CertPath
 19        The path to the SSL certificate file. This parameter is Mandatory if using HTTPS.
 20    .PARAMETER CertPassword
 21        The password for the SSL certificate, if applicable. This parameter is optional.
 22    .PARAMETER SelfSignedCert
 23        If specified, a self-signed certificate will be generated and used for HTTPS. This parameter is optional.
 24    .PARAMETER X509Certificate
 25        An X509Certificate2 object representing the SSL certificate. This parameter is Mandatory if using HTTPS
 26    .PARAMETER Protocols
 27        The HTTP protocols to use (e.g., Http1, Http2). Defaults to Http1 for HTTP listeners and Http1OrHttp2 for HTTPS 
 28    .PARAMETER UseConnectionLogging
 29        If specified, enables connection logging for the listener. This is useful for debugging and monitoring purposes.
 30    .PARAMETER PassThru
 31        If specified, the cmdlet will return the modified server instance after adding the listener.
 32    .EXAMPLE
 33        New-KrServer -Name 'MyKestrunServer'
 34        Creates a new Kestrun server instance with the specified name.
 35    .NOTES
 36        This function is designed to be used after the server has been configured with routes and listeners.
 37#>
 38function Add-KrEndpoint {
 39    [KestrunRuntimeApi('Definition')]
 40    [CmdletBinding(defaultParameterSetName = 'NoCert')]
 41    [OutputType([Kestrun.Hosting.KestrunHost])]
 42    param(
 43        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 44        [Kestrun.Hosting.KestrunHost]$Server,
 45        [Parameter()]
 46        [int]$Port = 0,
 47        [Parameter()]
 48        [System.Net.IPAddress]$IPAddress,
 49        [Parameter()]
 50        [string]$HostName,
 51        [Parameter()]
 52        [System.Uri]$Uri,
 53        [Parameter()]
 54        [System.Net.Sockets.AddressFamily[]]$AddressFamily,
 55
 56        [Parameter(mandatory = $true, ParameterSetName = 'CertFile')]
 57        [string]$CertPath,
 58
 59        [Parameter(mandatory = $false, ParameterSetName = 'CertFile')]
 60        [SecureString]$CertPassword = $null,
 61
 62        [Parameter(ParameterSetName = 'SelfSignedCert')]
 63        [alias('SelfSigned')]
 64        [alias('SelfSignedCertificate')]
 65        [switch]$SelfSignedCert,
 66
 67        [Parameter(Mandatory = $true, ParameterSetName = 'x509Certificate')]
 68        [System.Security.Cryptography.X509Certificates.X509Certificate2]$X509Certificate = $null,
 69
 70        [Parameter(ParameterSetName = 'x509Certificate')]
 71        [Parameter(ParameterSetName = 'CertFile')]
 72        [Parameter(ParameterSetName = 'SelfSignedCert')]
 73        [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]$Protocols,
 74
 75        [Parameter()]
 76        [switch]$UseConnectionLogging,
 77
 78        [Parameter()]
 79        [switch]$PassThru
 80    )
 81    begin {
 82        # Ensure the server instance is resolved
 083        $Server = Resolve-KestrunServer -Server $Server
 084        if ($Server.IsConfigured) {
 085            throw "Cannot add endpoint to a server that is already configured. Please create a new server instance."
 86        }
 87        # Validate mutually exclusive parameters
 088        if ($null -ne $IPAddress) {
 089            if (-not [string]::IsNullOrEmpty($HostName)) {
 090                throw "Cannot specify both IPAddress and HostName. Please choose one."
 91            }
 092            if ($null -ne $Uri) {
 093                throw "Cannot specify both IPAddress and Uri. Please choose one."
 94            }
 095            if ($AddressFamily -and -not ($AddressFamily -contains $IPAddress.AddressFamily)) {
 096                throw "The specified IPAddress does not match the provided AddressFamily filter."
 97            }
 98        } else {
 99
 0100            if ($null -ne $Uri -and (-not ([string]::IsNullOrEmpty($HostName)))) {
 0101                throw "Cannot specify both HostName and Uri. Please choose one."
 102            }
 0103            if ($null -eq $Uri -and [string]::IsNullOrEmpty($HostName)) {
 0104                $IPAddress = [System.Net.IPAddress]::Loopback
 105            }
 106        }
 107    }
 108    process {
 109
 110        # Validate parameters based on the parameter set
 0111        if ($null -eq $Protocols) {
 0112            if ($PSCmdlet.ParameterSetName -eq 'NoCert') {
 0113                $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1
 114            } else {
 0115                $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1AndHttp2
 116            }
 117        }
 0118        if ($PSCmdlet.ParameterSetName -eq 'CertFile') {
 0119            if (-not (Test-Path $CertPath)) {
 0120                throw "Certificate file not found: $CertPath"
 121            }
 0122            $X509Certificate = Import-KrCertificate -FilePath $CertPath -Password $CertPassword
 0123        } elseif ($SelfSignedCert.IsPresent) {
 0124            $X509Certificate = New-KrSelfSignedCertificate -DnsNames localhost, 127.0.0.1 -ValidDays 30
 125        }
 126
 127
 0128        if (-not ([string]::IsNullOrEmpty($HostName))) {
 0129            $Server.ConfigureListener($HostName, $Port, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent, $
 0130        } elseif ($null -ne $Uri) {
 0131            $Server.ConfigureListener($Uri, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent, $AddressFamil
 0132        } elseif ($null -ne $IPAddress) {
 0133            $Server.ConfigureListener($Port, $IPAddress, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent) 
 134        } else {
 0135            throw "Invalid parameter set: $($PSCmdlet.ParameterSetName). Please specify either HostName, Uri, or IPAddre
 136        }
 137
 0138        if ($PassThru.IsPresent) {
 139            # Return the modified server instance
 0140            return $Server
 141        }
 142    }
 143}
 144

Methods/Properties

Add-KrEndpoint()