Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enable/Disable JIT optimizations for assemblies so they can be debugged with dnSpy
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Enable-IISAssemblyDebugging
{
param(
[string]$Path = ".",
[string]$Filter = "*.dll"
)
$Path = Resolve-Path $Path
$binPath = Join-Path $Path "bin"
$webConfig = Join-Path $Path "Web.config"
if (-not (Test-Path $binPath)) {
throw "Path does not contain a bin\ folder"
}
if (-not (Test-Path $webConfig)) {
throw "Path does not contain a Web.config file"
}
$assemblies = Get-ChildItem -Path $binPath -Filter $Filter -File
$assemblies | Foreach-Object {
$assemblyIniPath = Join-Path $_.DirectoryName "$($_.BaseName).ini"
Set-Content -Path $assemblyIniPath -Value "[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0"
} | Out-Null
$doc = New-Object System.Xml.XmlDocument
$doc.Load([string]$webConfig)
$hostingEnvironmentEl = $doc.SelectSingleNode("/configuration/system.web/hostingEnvironment")
if ($hostingEnvironmentEl -eq $null) {
$hostingEnvironmentEl = $doc.CreateElement("hostingEnvironment")
$systemWebEl = $doc.SelectSingleNode("/configuration/system.web")
if ($systemWebEl -eq $null) {
$systemWebEl = $doc.CreateElement("system.web")
$doc.DocumentElement.AppendChild($systemWebEl)
}
$systemWebEl.AppendChild($hostingEnvironmentEl) | Out-Null
}
$hostingEnvironmentEl.SetAttribute("shadowCopyBinAssemblies", "false")
$doc.Save($webConfig)
}
function Disable-IISAssemblyDebugging
{
param(
[string]$Path = ".",
[string]$Filter = "*.dll"
)
$Path = Resolve-Path $Path
$binPath = Join-Path $Path "bin"
$webConfig = Join-Path $Path "Web.config"
if (-not (Test-Path $binPath)) {
throw "Path does not contain a bin\ folder"
}
if (-not (Test-Path $webConfig)) {
throw "Path does not contain a Web.config file"
}
$assemblies = Get-ChildItem -Path $binPath -Filter $Filter -File
$assemblies |
Foreach-Object { Join-Path $_.DirectoryName "$($_.BaseName).ini" } |
Where-Object { Test-Path $_ } |
ForEach-Object { Remove-Item $_ } |
Out-Null
$doc = New-Object System.Xml.XmlDocument
$doc.Load([string]$webConfig)
$hostingEnvironmentEl = $doc.SelectSingleNode("/configuration/system.web/hostingEnvironment")
if ($hostingEnvironmentEl -ne $null) {
$hostingEnvironmentEl.RemoveAttribute("shadowCopyBinAssemblies")
if ($hostingEnvironmentEl.Attributes.Count -eq 0) {
$hostingEnvironmentEl.ParentNode.RemoveChild($hostingEnvironmentEl) | Out-Null
}
}
$doc.Save($webConfig)
}
Export-ModuleMember Disable-IISAssemblyDebugging
Export-ModuleMember Enable-IISAssemblyDebugging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.