1. Describe the bug
Tests that pass in the AL Test Tool (page 130451) fail when they are run with the developer tools' test execution, i.e. the VS Code Test Explorer or altool runtests (no AL test runner). The failure is a platform exception while a page is disposed:
Unexpected CLR exception thrown.: System.NullReferenceException: Object reference not set to an instance of an object. in NavClientHandle.Dispose.
It occurs as soon as Camera.IsAvailable() has been called – directly, or indirectly by opening a card page whose picture FactBox calls it in OnOpenPage ("Item Picture" on the Item Card, "Customer Picture" on the Customer Card). As a consequence, every test that opens one of these card pages as a TestPage cannot be run from the Test Explorer or with altool runtests.
2. To Reproduce
- Create an app that depends on Microsoft apps only and add the codeunit below.
- Publish it to a BC 28.5 on-premises container that contains Microsoft apps only.
- Run codeunit 50100 with
altool runtests 50100 --server https://<container> --serverinstance BC --port 7049 --tenant default --environmenttype OnPrem --authentication UserPassword --company "CRONUS International Ltd." (or from the VS Code Test Explorer).
- Run the same codeunit in the AL Test Tool (page 130451) in the web client.
codeunit 50100 "Camera Dispose Repro"
{
Subtype = Test;
TestPermissions = Disabled;
[Test]
procedure "01_Control_OpenAndClosePaymentTerms"()
var
PaymentTerms: TestPage "Payment Terms";
begin
PaymentTerms.OpenView();
PaymentTerms.Close();
end;
[Test]
procedure "02_CameraIsAvailableWithoutAnyPage"()
var
Camera: Codeunit Camera;
begin
if Camera.IsAvailable() then;
end;
[Test]
procedure "03_OpenAndCloseItemCard"()
var
ItemCard: TestPage "Item Card";
begin
ItemCard.OpenView();
ItemCard.Close();
end;
[Test]
procedure "04_OpenAndCloseCustomerCard"()
var
CustomerCard: TestPage "Customer Card";
begin
CustomerCard.OpenView();
CustomerCard.Close();
end;
}
3. Expected behavior
Same result as in the AL Test Tool: all four tests pass. For example, Camera.IsAvailable() could return false (or not create the client-side object) when no client is attached, so that the page and codeunit can be disposed.
4. Actual behavior
| Test |
altool runtests (no test runner) |
AL Test Tool (page 130451) |
| 01_Control_OpenAndClosePaymentTerms |
pass |
pass |
| 02_CameraIsAvailableWithoutAnyPage |
FAIL |
pass |
| 03_OpenAndCloseItemCard |
FAIL |
pass |
| 04_OpenAndCloseCustomerCard |
FAIL |
pass |
Error of 02 (03 and 04 end in the same frames, reached via the picture FactBox of the card):
Unexpected CLR exception thrown.: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Dynamics.Nav.Runtime.NavClientHandle.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.NavInteropHandleReferenceCount.Dispose()
at Microsoft.Dynamics.Nav.Runtime.NavDotNet.Clear()
at Microsoft.Dynamics.Nav.Runtime.NavDotNet.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.NavForm.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.TreeHandler.TreeObjectReferenceHandler.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.NavFormHandle.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.TreeHandler.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.BusinessApplication.Codeunit50100._02_CameraIsAvailableWithoutAnyPage()
Analysis from the source (Base Application / System Application 28.5):
- "Item Picture"
OnOpenPage: CameraAvailable := Camera.IsAvailable(); (same pattern in "Customer Picture").
- "Camera Impl."
IsAvailable() only checks GuiAllowed() – which is true in test sessions – and then calls page "Camera".
- Page "Camera" holds
[RunOnClient] [WithEvents] CameraProvider: DotNet CameraProvider;.
Without an attached client there seems to be no valid client handle when this RunOnClient object is disposed. There is no way for a test to intercept before the DotNet call (OnIsCameraAvailable is raised afterwards).
5. Versions:
- AL Language: 18.0.2732683
- Visual Studio Code: 1.139.1
- Business Central: on-premises Docker container, platform 28.0.54423.0, Base Application 28.5.54151.54491 (company CRONUS International Ltd.); only Microsoft apps installed besides the repro app
- List of Visual Studio Code extensions that you have installed: not relevant for the repro above – it was run with
altool runtests outside VS Code. The same exception occurs for our own test codeunits in the VS Code Test Explorer.
- Operating System:
Final Checklist
1. Describe the bug
Tests that pass in the AL Test Tool (page 130451) fail when they are run with the developer tools' test execution, i.e. the VS Code Test Explorer or
altool runtests(no AL test runner). The failure is a platform exception while a page is disposed:Unexpected CLR exception thrown.: System.NullReferenceException: Object reference not set to an instance of an object.inNavClientHandle.Dispose.It occurs as soon as
Camera.IsAvailable()has been called – directly, or indirectly by opening a card page whose picture FactBox calls it inOnOpenPage("Item Picture" on the Item Card, "Customer Picture" on the Customer Card). As a consequence, every test that opens one of these card pages as a TestPage cannot be run from the Test Explorer or withaltool runtests.2. To Reproduce
altool runtests 50100 --server https://<container> --serverinstance BC --port 7049 --tenant default --environmenttype OnPrem --authentication UserPassword --company "CRONUS International Ltd."(or from the VS Code Test Explorer).3. Expected behavior
Same result as in the AL Test Tool: all four tests pass. For example,
Camera.IsAvailable()could return false (or not create the client-side object) when no client is attached, so that the page and codeunit can be disposed.4. Actual behavior
altool runtests(no test runner)Error of 02 (03 and 04 end in the same frames, reached via the picture FactBox of the card):
Analysis from the source (Base Application / System Application 28.5):
OnOpenPage:CameraAvailable := Camera.IsAvailable();(same pattern in "Customer Picture").IsAvailable()only checksGuiAllowed()– which is true in test sessions – and then calls page "Camera".[RunOnClient] [WithEvents] CameraProvider: DotNet CameraProvider;.Without an attached client there seems to be no valid client handle when this RunOnClient object is disposed. There is no way for a test to intercept before the DotNet call (
OnIsCameraAvailableis raised afterwards).5. Versions:
altool runtestsoutside VS Code. The same exception occurs for our own test codeunits in the VS Code Test Explorer.Final Checklist
altool runtests, no VS Code extensions involved)