From 34a057f34e8cf1c5e2d9667cc41e9bb58fc686a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 8 Feb 2023 07:16:51 +0100 Subject: [PATCH] Improve GAC registration in PowerShell module (#2171) --- OpenTelemetry.DotNet.Auto.psm1 | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/OpenTelemetry.DotNet.Auto.psm1 b/OpenTelemetry.DotNet.Auto.psm1 index 90653f8b62..2184b54395 100644 --- a/OpenTelemetry.DotNet.Auto.psm1 +++ b/OpenTelemetry.DotNet.Auto.psm1 @@ -199,6 +199,15 @@ function Get-OpenTelemetry-Archive([string] $Version, [string] $LocalPath) { return Download-OpenTelemetry $Version $tempDir } +function Test-AssemblyNotForGAC([string] $Name) { + switch ($Name) { + "netstandard.dll" { return $true } + "grpc_csharp_ext.x64.dll" { return $true } + "grpc_csharp_ext.x86.dll" { return $true } + } + return $false +} + <# .SYNOPSIS Installs OpenTelemetry .NET Automatic Instrumentation. @@ -240,13 +249,19 @@ function Install-OpenTelemetryCore() { [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null $publish = New-Object System.EnterpriseServices.Internal.Publish $dlls = Get-ChildItem -Path $installDir\netfx\ -Filter *.dll -File - for ($i = 0; $i -le $dlls.Count; $i++) { + for ($i = 0; $i -lt $dlls.Count; $i++) { $percentageComplete = $i / $dlls.Count * 100 Write-Progress -Activity "Registering .NET Framweworks dlls in GAC" ` -Status "Module $($i+1) out of $($dlls.Count). Installing $($dlls[$i].Name):" ` -PercentComplete $percentageComplete + + if (Test-AssemblyNotForGAC $dlls[$i].Name) { + continue + } + $publish.GacInstall($dlls[$i].FullName) } + Write-Progress -Activity "Registering .NET Framweworks dlls in GAC" -Status "Ready" -Completed } catch { $message = $_ @@ -275,9 +290,19 @@ function Uninstall-OpenTelemetryCore() { [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null $publish = New-Object System.EnterpriseServices.Internal.Publish $dlls = Get-ChildItem -Path $installDir\netfx\ -Filter *.dll -File - foreach ($dll in $dlls) { - $publish.GacRemove($dll.FullName) + for ($i = 0; $i -lt $dlls.Count; $i++) { + $percentageComplete = $i / $dlls.Count * 100 + Write-Progress -Activity "Unregistering .NET Framweworks dlls from GAC" ` + -Status "Module $($i+1) out of $($dlls.Count). Uninstalling $($dlls[$i].Name):" ` + -PercentComplete $percentageComplete + + if (Test-AssemblyNotForGAC $dlls[$i].Name) { + continue + } + + $publish.GacRemove($dlls[$i].FullName) } + Write-Progress -Activity "Unregistering .NET Framweworks dlls from GAC" -Status "Ready" -Completed Remove-Item -LiteralPath $installDir -Force -Recurse