[Bug]: BC 28.5 on-prem — Invoke-NAVApplicationDatabaseConversion fails with TypeInitializationException in OpenTelemetry (regression vs 28.3)
Describe the issue
On Business Central 28.5 on-premises (build 28.0.54016.0), every Management cmdlet that constructs a NavEnvironment fails immediately when executed from powershell.exe.
Invoke-NAVApplicationDatabaseConversion: The type initializer for
'OpenTelemetry.Instrumentation.Http.Implementation.HttpHandlerDiagnosticListener' threw an exception.
Event Viewer:
TypeLoadException: Could not load type 'System.Diagnostics.ActivitySourceOptions'
from assembly 'System.Diagnostics.DiagnosticSource, Version=8.0.0.0, ...'
Stack:
OpenTelemetry.Trace.ActivitySourceFactory.Create
-> OpenTelemetry.Instrumentation.Http.Implementation.HttpHandlerDiagnosticListener..cctor()
-> OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions.AddHttpClientInstrumentation
-> Microsoft.BusinessCentral.Telemetry.OpenTelemetry.OpenTelemetrySpanLogger..ctor
-> Microsoft.Dynamics.Nav.Diagnostic.NavOpenTelemetryLogger..ctor
-> Microsoft.Dynamics.Nav.Runtime.NavEnvironment..ctor
-> InvokeNAVApplicationDatabaseConversion.InternalProcessRecord()
Root cause analysis
This is a regression introduced in 28.5. Comparing two servers with otherwise identical configuration (Windows Server 2022, .NET Framework 4.8 release 528449, Windows PowerShell 5.1, same side-by-side BC installs):
28.3 — 28.0.52201.0 (works) |
28.5 — 28.0.54016.0 (fails) |
|
|---|---|---|
System.Diagnostics.DiagnosticSource |
10.0.0.0 | 10.0.0.0 |
OpenTelemetry.Instrumentation.Http |
1.9.0.41 | 1.16.0.1135 |
OpenTelemetry.Instrumentation.Http was upgraded from 1.9.0.41 to 1.16.0.1135. Version 1.16 references System.Diagnostics.DiagnosticSource 8.0.0.0 and requires the ActivitySource API surface from 9.x/10.x in the static constructor of HttpHandlerDiagnosticListener. Version 1.9 did not touch that code path, so the mismatch was harmless.
Correct binding redirects do exist:
Service\Microsoft.Dynamics.Nav.Server.exe.config—System.Diagnostics.DiagnosticSource0.0.0.0-10.0.0.0->10.0.0.0Service\Management\Microsoft.Dynamics.Nav.Management.dll.config— same redirects
The NST service works because it is an .exe and reads its own .exe.config. However, .NET Framework never reads *.dll.config — configuration is resolved only from <process>.exe.config. When Management cmdlets are loaded into powershell.exe, no redirect applies, the CLR resolves DiagnosticSource 8.0.0.0, and the type initializer throws.
Impact: all on-premises upgrade automation on 28.5 is blocked — database conversion, and any other cmdlet that builds a NavEnvironment.
Expected behavior
Invoke-NAVApplicationDatabaseConversion (and other Management cmdlets creating a NavEnvironment) should run successfully from powershell.exe after importing NavAdminTool.ps1, exactly as they do on BC 28.3 and earlier.
Steps to reproduce
- Install Business Central 28.5 on-premises (build
28.0.54016.0) on Windows Server 2022, .NET Framework 4.8. - Open a clean
powershell.exe -NoProfilesession (Windows PowerShell 5.1, Desktop edition). - Import the admin tool:
. 'C:\Program Files\Microsoft Dynamics 365 Business Central\280\Service\NavAdminTool.ps1' - Run a database conversion against a database from an earlier version (e.g. 27.3):
Invoke-NAVApplicationDatabaseConversion ` -DatabaseServer 'SQLSERVER' ` -DatabaseName 'BC Test' ` -Force - The cmdlet fails with
TypeInitializationExceptionbefore touching SQL.
Repeating the same steps on BC 28.3 (28.0.52201.0) succeeds.
Additional context
Environment
- Windows Server 2022 Standard / Datacenter
- .NET Framework 4.8, release
528449 - Windows PowerShell 5.1 (Desktop) — reproduced on builds
5.1.20348.4294and confirmed working on5.1.20348.5499with 28.3 - Side-by-side BC installs present on both servers; ruled out as a factor (both machines have 5 versions installed,
PATH/PSModulePathcontain no BC entries, GAC contains noSystem.Diagnostics.DiagnosticSource)
Things that were ruled out
- Licence — the cmdlet fails before licence validation
- GAC pollution — no
DiagnosticSourcein GAC on either machine PATH/PSModulePath— no BC paths present- Az / AzureRM modules — not installed
- Preloading the correct assembly via
Assembly.LoadFromplus anAppDomain.AssemblyResolvehook does not help, because the OpenTelemetry reference is resolved in the default load context, which does not see the LoadFrom context
Workarounds
- Copy the
<assemblyBinding>section fromMicrosoft.Dynamics.Nav.Management.dll.configinto%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe.config, then run the cmdlet with the working directory set to...\280\Service. This is global and affects every PowerShell session on the machine, so it must be reverted afterwards. - Downgrade the platform to 28.3.
- Perform the upgrade inside a Docker container built from a 28.3 artifact.
Suggested fix
Any of the following would resolve it:
- Ship a
powershell.exe.configfragment (or documented merge step) together with the Administration Tool component - Apply the binding redirects programmatically from
NavAdminTool.ps1, e.g. via a dedicatedAppDomainwith the correctAppDomainSetup.ConfigurationFile - Pin
OpenTelemetry.Instrumentation.Httpto a version compatible with the shippedSystem.Diagnostics.DiagnosticSource 10.0.0.0, or ship a matchingDiagnosticSourceassembly version - Make
NavEnvironmenttolerate OpenTelemetry initialisation failures instead of propagating them as a fatalTypeInitializationException
I will provide a fix for a bug
- [ ] I will provide a fix for a bug
:robot: AI triage completed. Product group, please validate the triage report (Microsoft internal access only, use Ctrl+Click to open in new tab).
Was this triage helpful? React with :thumbsup: or :thumbsdown: on this comment to provide feedback.
I have the same problem. Upgrading from BC 26.16 to BC 28.5.
Invoke-NAVApplicationDatabaseConversion gives me this error:
PS > Invoke-NAVApplicationDatabaseConversion -DatabaseServer $DatabaseServer -DatabaseName $ApplicationDatabase
WARNING: UnhandledErrorMessage
Invoke-NAVApplicationDatabaseConversion : The type initializer for 'OpenTelemetry.Instrumentation.Http.Implementation.HttpHandlerDiagnosticListener' threw an exception.
At line:1 char:1
+ Invoke-NAVApplicationDatabaseConversion -DatabaseServer $DatabaseServ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-NAVApplicationDatabaseConversion], TypeInitializationException
+ FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.Dynamics.Nav.Management.Cmdlets.InvokeNAVApplicationDatabaseConversion
Hi i have the same issue im doing CU upgrade from ( 27.CU 5 to BC27 CU 11)
Invoke-NAVApplicationDatabaseConversion -DatabaseServer $DatabaseServer -DatabaseName $ApplicationDatabase
WARNING: UnhandledErrorMessage Invoke-NAVApplicationDatabaseConversion: The type initializer for 'OpenTelemetry.Instrumentation.Http.Implementation.HttpHandlerDiagnosticListener' threw an exception. PS C:\Windows\System32>
@xtiche this workaround doens't work for me.
Copy the <assemblyBinding> section from Microsoft.Dynamics.Nav.Management.dll.config into %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe.config, then run the cmdlet with the working directory set to ...\280\Service. This is global and affects every PowerShell session on the machine, so it must be reverted afterwards.
Confirming this issue.
Found a solution: I updated my Powershell from 7.4.1 to the last version 7.6.6.
This solved my problem.
Is this really working? From what version did you do the upgrade to 28.5 ?
Is this really working? From what version did you do the upgrade to 28.5 ?
Upgrading from BC 26.16 to BC 28.5.
Do I need to use all the 3 steps from 'Workarounds' section?
- Copy the
section from Microsoft.Dynamics.Nav.Management.dll.config into %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe.config, then run the cmdlet with the working directory set to ...\280\Service. This is global and affects every PowerShell session on the machine, so it must be reverted afterwards. - Downgrade the platform to 28.3.
- Perform the upgrade inside a Docker container built from a 28.3 artifact.
Do I need to use all the 3 steps from 'Workarounds' section?
- Copy the section from Microsoft.Dynamics.Nav.Management.dll.config into %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe.config, then run the cmdlet with the working directory set to ...\280\Service. This is global and affects every PowerShell session on the machine, so it must be reverted afterwards.
- Downgrade the platform to 28.3.
- Perform the upgrade inside a Docker container built from a 28.3 artifact.
Not needed for me, just update PS to 7.6.6
Found a solution: I updated my Powershell from 7.4.1 to the last version 7.6.6.
This solved my problem.
Using PowerShell 7.6.6 worked for me too
Updating the PS version to 7.6.6 resolved the problem .
to do it in VSC (with PS 7.6.6), just switch the Powershell extension into Pre-Release, otherwise there is error: WARNING: UnhandledErrorMessage
System.MissingMethodException: Method not found: 'Microsoft.Extensions.Logging.ILoggerFactory Microsoft.Extensions.Logging.LoggerFactory.Create(System.Action1<Microsoft.Extensions.Logging.ILoggingBuilder>)'. at Microsoft.BusinessCentral.Telemetry.OpenTelemetry.OpenTelemetryLogger1..ctor(Dictionary2 contextColumns, String logFileFolderOnlyUseDuringDevelopment, Boolean enableLoggingToEventLog, LogLevel minimumLogLevel) at Microsoft.BusinessCentral.Telemetry.OpenTelemetry.OpenTelemetryLogger1..ctor(Dictionary2 contextColumns, String logFileFolderOnlyUseDuringDevelopment, Boolean enableLoggingToEventLog, LogLevel minimumLogLevel) at Microsoft.Dynamics.Nav.Diagnostic.NavOpenTelemetryLogger..ctor(Verbosity traceLevel, Dictionary2 contextColumns, String logFileFolderOnlyUseDuringDevelopment)
at Microsoft.Dynamics.Nav.Runtime.NavEnvironment..ctor(NavEnvironmentFlags flags)
at Microsoft.Dynamics.Nav.Runtime.NavEnvironment.InstantiateStandaloneNavEnvironment(Boolean enableNavApps, Boolean removeAllAppsMode)
at Microsoft.Dynamics.Nav.Management.Cmdlets.InvokeNAVApplicationDatabaseConversion.InternalProcessRecord()
at Microsoft.Dynamics.Nav.Management.NavCommand.ProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
Check in VSC: [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.GetName().Name -eq "Microsoft.Extensions.Logging"} | Select FullName Result needs to look like this: Microsoft.Extensions.Logging, Version=10.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60