BCApps icon indicating copy to clipboard operation
BCApps copied to clipboard

[Bug]: BC 28.5 on-prem — Invoke-NAVApplicationDatabaseConversion fails with TypeInitializationException in OpenTelemetry (regression vs 28.3)

Open xtiche opened this issue 2 months ago • 13 comments

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.DiagnosticSource 0.0.0.0-10.0.0.0 -> 10.0.0.0
  • Service\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

  1. Install Business Central 28.5 on-premises (build 28.0.54016.0) on Windows Server 2022, .NET Framework 4.8.
  2. Open a clean powershell.exe -NoProfile session (Windows PowerShell 5.1, Desktop edition).
  3. Import the admin tool:
    . 'C:\Program Files\Microsoft Dynamics 365 Business Central\280\Service\NavAdminTool.ps1'
    
  4. Run a database conversion against a database from an earlier version (e.g. 27.3):
    Invoke-NAVApplicationDatabaseConversion `
        -DatabaseServer 'SQLSERVER' `
        -DatabaseName   'BC Test' `
        -Force
    
  5. The cmdlet fails with TypeInitializationException before 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.4294 and confirmed working on 5.1.20348.5499 with 28.3
  • Side-by-side BC installs present on both servers; ruled out as a factor (both machines have 5 versions installed, PATH/PSModulePath contain no BC entries, GAC contains no System.Diagnostics.DiagnosticSource)

Things that were ruled out

  • Licence — the cmdlet fails before licence validation
  • GAC pollution — no DiagnosticSource in GAC on either machine
  • PATH / PSModulePath — no BC paths present
  • Az / AzureRM modules — not installed
  • Preloading the correct assembly via Assembly.LoadFrom plus an AppDomain.AssemblyResolve hook does not help, because the OpenTelemetry reference is resolved in the default load context, which does not see the LoadFrom context

Workarounds

  1. 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.
  2. Downgrade the platform to 28.3.
  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.config fragment (or documented merge step) together with the Administration Tool component
  • Apply the binding redirects programmatically from NavAdminTool.ps1, e.g. via a dedicated AppDomain with the correct AppDomainSetup.ConfigurationFile
  • Pin OpenTelemetry.Instrumentation.Http to a version compatible with the shipped System.Diagnostics.DiagnosticSource 10.0.0.0, or ship a matching DiagnosticSource assembly version
  • Make NavEnvironment tolerate OpenTelemetry initialisation failures instead of propagating them as a fatal TypeInitializationException

I will provide a fix for a bug

  • [ ] I will provide a fix for a bug

xtiche avatar Sep 08 '26 13:09 xtiche

: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.

github-actions[bot] avatar Sep 08 '26 13:09 github-actions[bot]

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

pom-wandries avatar Sep 15 '26 11:09 pom-wandries

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>

GITHUBLSR avatar Sep 15 '26 14:09 GITHUBLSR

@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.

pom-wandries avatar Sep 16 '26 08:09 pom-wandries

Confirming this issue.

kennetlindberg avatar Sep 21 '26 11:09 kennetlindberg

Found a solution: I updated my Powershell from 7.4.1 to the last version 7.6.6.

This solved my problem.

pom-wandries avatar Sep 21 '26 12:09 pom-wandries

Is this really working? From what version did you do the upgrade to 28.5 ?

robi93p avatar Sep 23 '26 12:09 robi93p

Is this really working? From what version did you do the upgrade to 28.5 ?

Upgrading from BC 26.16 to BC 28.5.

pom-wandries avatar Sep 23 '26 12:09 pom-wandries

Do I need to use all the 3 steps from 'Workarounds' section?

  1. 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.
  2. Downgrade the platform to 28.3.
  3. Perform the upgrade inside a Docker container built from a 28.3 artifact.

robi93p avatar Sep 23 '26 12:09 robi93p

Do I need to use all the 3 steps from 'Workarounds' section?

  1. 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.
  2. Downgrade the platform to 28.3.
  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

pom-wandries avatar Sep 23 '26 12:09 pom-wandries

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

jvincke avatar Oct 01 '26 09:10 jvincke

Updating the PS version to 7.6.6 resolved the problem .

ManiKumar-1988 avatar Oct 06 '26 10:10 ManiKumar-1988

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

RobertJelen avatar Oct 21 '26 08:10 RobertJelen