Skip to content

Commit

Permalink
Improve GAC registration in PowerShell module (#2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared authored Feb 8, 2023
1 parent 6e3a1ec commit 34a057f
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions OpenTelemetry.DotNet.Auto.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = $_
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 34a057f

Please sign in to comment.