dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.63k stars 1.61k forks source link

Error handling is different for compiled bindings #22078

Open DarkStarDS9 opened 2 weeks ago

DarkStarDS9 commented 2 weeks ago

Description

When the getter of a property throws a NullReferenceException, this will be caught and silently ignored for compiled bindings, there won't even be a diagnostic-log via BindingDiagnostics.SendBindingFailure.

Binding expressions however execute the getter without any try/catch and will therefore throw.

If this difference in error-handling is on purpose, I'd ask to document this on the "Compiled bindings" page and add some logging via BindingDiagnostics.SendBindingFailure.

I'd prefer to have the behavior aligned either way, but if it is decided that it should not throw, I would really like to see some logging so that this can be caught in a UnitTest, for example.

Steps to Reproduce

No response

Link to public reproduction project repository

https://github.com/DarkStarDS9/MauiIssues

Version with bug

8.0.21 SR4.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

DarkStarDS9 commented 1 week ago

I've updated the description to point to my repo. If you run the UnitTests in Debug configuration, they will fail because a NullReferenceException is thrown. If you run the UnitTests in Release configuration they will fail because the exception will be swallowed and no BindingError will be logged.

kevinxufei commented 1 week ago

Verified this issue with Visual Studio 17.10 Preview 5 (8.0.21). Can not repro issue with sample project.

DarkStarDS9 commented 1 week ago

I have updated the repo to .21 (sorry, didn't check this before - kinda assumed that dotnet new will create a project using the most recent version). Strangely I also had to adjust the test and add yet another mock... but anyway, the issue is still there.

BTW, I am not running this in Visual Studio but from the command-line:

dotnet test -c Debug [xUnit.net 00:00:00.28] UnitTests.Issue22078Tests.SetBindingContext [FAIL] Failed UnitTests.Issue22078Tests.SetBindingContext [189 ms] Error Message: System.NullReferenceException : Object reference not set to an instance of an object. Stack Trace: at MauiIssues.Issue22078ViewModel.get_ObjectType()

vs

dotnet test -c Release [xUnit.net 00:00:00.26] UnitTests.Issue22078Tests.SetBindingContext [FAIL] Failed UnitTests.Issue22078Tests.SetBindingContext [176 ms] Error Message: If a binding throws an exception, I kinda expect diagnostic logging :) Stack Trace: at UnitTests.Issue22078Tests.SetBindingContext()

kevinxufei commented 1 week ago

Thanks for your explanation, executing from the command line can reproduce the issue.