Skip to content

Instantly share code, notes, and snippets.

@richardszalay
Created June 14, 2019 02:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardszalay/59664cd302e66511618f51eaaa77db26 to your computer and use it in GitHub Desktop.
Save richardszalay/59664cd302e66511618f51eaaa77db26 to your computer and use it in GitHub Desktop.
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