qbo4.Infrastructure.psm1

Write-Verbose "Setting up AssemblyResolve handler for qbo4.Infrastructure"

# Only attach the handler once
if (-not $script:AssemblyHandlerAttached) {

    $script:ResolvedAssemblies = @{}

    $script:AssemblyResolveHandler = {
        param($sender, $args)

        $requestedName = $args.Name.Split(',')[0]
        if ($script:ResolvedAssemblies.ContainsKey($requestedName)) {
            return $script:ResolvedAssemblies[$requestedName]
        }

        try {
            $assemblyPath = Join-Path -Path $PSScriptRoot -ChildPath "$requestedName.dll"
            if (Test-Path $assemblyPath) {
                $assembly = [System.Reflection.Assembly]::LoadFrom($assemblyPath)
                $script:ResolvedAssemblies[$requestedName] = $assembly
                Write-Verbose "Manually loaded assembly: $requestedName from $assemblyPath"
                return $assembly
            } else {
                Write-Verbose "Assembly $requestedName not found at $assemblyPath"
            }
        } catch {
            Write-Warning "Failed to resolve assembly '$requestedName': $_"
        }

        return $null
    }

    [AppDomain]::CurrentDomain.add_AssemblyResolve($script:AssemblyResolveHandler)
    $script:AssemblyHandlerAttached = $true
}

Write-Verbose "Loading qbo4.Infrastructure cmdlets and functions from $($PSScriptRoot)"
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath qbo4.Infrastructure.Powershell.dll)
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath qbo4.WebServer.ps1)
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath qbo4.PublishModule.ps1)
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath qbo4.Nuget.ps1)

Write-Verbose "Initializing qboConfig"
Initialize-qboConfig 

Export-ModuleMember -Cmdlet '*'