microsoft / appcenter-sdk-dotnet

Development repository for the App Center SDK for .NET platforms, including Xamarin
Other
310 stars 141 forks source link

AppCenter thinks the application is WPF but it is actually a WinForms application when using a WPF component. #1745

Closed Jsil8 closed 8 months ago

Jsil8 commented 1 year ago

Description

When using a WPF component in a WinForms application, AppCenter runs WPF specific code while it shouldn't and crashes.

The problem is in the WindowsHelper static constructor. If the "PresentationFramework" assembly is loaded, the IsRunningAsWpf is set to true and this will result in a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot perform runtime binding on a null reference'.

image

Repro Steps

  1. Create a new Windows Forms App in Visual Studio
  2. Add NuGet packages: Microsoft.AppCenter.Crashes and DiffPlex.Wpf (or just another random wpf control).
  3. Add the wpf control to the Form1 using the designer.
  4. Add the following code to the Form1:
    protected override void OnShown(EventArgs e)
    {
      base.OnShown(e);
      AppCenter.Start("appSecret=xxx", typeof(Crashes));
    }

Please see this repository to reproduce the problem: https://github.com/Jsil8/appcenter_winforms_bug

Details

  1. What is your app platform (Xamarin.Android or Xamarin.iOS or UWP)? Windows Forms
  2. If using Xamarin.Forms or if using portable/shared code to call our SDK APIs, are you using shared project, PCL code or .NET standard code for the application? Which .NET standard version or which PCL profile? .NET 7.0
  3. Which SDK version are you using? 5.0.2
  4. Which OS version did you experience the issue on? Windows 11 22H2 build 22621.1848
  5. What device version did you see this error on? Were you using an emulator or a physical device? PC
  6. What third party libraries are you using? In this case, DiffPlex.Wpf, but any other WPF control can be used to reproduce the problem.
  7. Please enable verbose logging for your app using AppCenter.LogLevel = LogLevel.Verbose before your call to AppCenter.Start(...) and include the logs here: 'WinFormsApp3.exe' (CoreCLR: clrhost): Loaded 'Anonymously Hosted DynamicMethods Assembly'. Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in System.Linq.Expressions.dll An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Linq.Expressions.dll but was not handled in user code Cannot perform runtime binding on a null reference
  8. Relevant exception info: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException HResult=0x80131500 Message=Cannot perform runtime binding on a null reference Source=System.Linq.Expressions StackTrace:

    at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at Microsoft.AppCenter.Utils.ApplicationLifecycleHelperDesktop..ctor() at Microsoft.AppCenter.ApplicationLifecycleHelper.get_Instance() at Microsoft.AppCenter.AppCenter..ctor() at Microsoft.AppCenter.AppCenter.get_Instance() at Microsoft.AppCenter.AppCenter.PlatformStart(String appSecret, Type[] services) at Microsoft.AppCenter.AppCenter.Start(String appSecret, Type[] services) at WinFormsApp3.Form1.OnShown(EventArgs e) in D:\Temp\WinFormsApp3\WinFormsApp3\Form1.cs:line 18 at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

MikhailSuendukov commented 1 year ago

Hi @Jsil8 ,I was able to reproduce your error, thank you very much for the detailed information. I created a work item about this error on our board.

emako commented 1 year ago

I encounter the same exception when I want to call AppCenter.Start(...) in Program.cs and before create App.xaml class.

This exception occurred in AppCenter 5.0.X, and I did not encounter this issue when using AppCenter 4.5.X with the same code.

I create the WPF Desktop Application but not WinForm.

Exception info


Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500
Message=无法对 null 引用执行运行时绑定
Source=<无法计算异常源>
StackTrace:
<无法计算异常堆栈跟踪>

> My sample code
```c#
internal class Program
{
    [STAThread]
    internal static void Main()
    {
        AppCenter.Start(...);
        RunApp(...);
    }
}
MikhailSuendukov commented 1 year ago

Hi @emako, and thanks for reaching out to us. It would be great if you could clarify the following points regarding your case:

  1. What is your app platform (Xamarin.Android or Xamarin.iOS or UWP)?
  2. If using Xamarin.Forms or if using portable/shared code to call our SDK APIs, are you using shared project, PCL code or .NET standard code for the application? Which .NET standard version or which PCL profile?
  3. Which SDK version are you using?
  4. Which OS version did you experience the issue on?
  5. What device version did you see this error on? Were you using an emulator or a physical device?
  6. What third party libraries are you using?
  7. Please enable verbose logging for your app using AppCenter.LogLevel = LogLevel.Verbose before your call to AppCenter.Start(...) and include the logs here.
  8. Relevant exception info.
DmitriyKirakosyan commented 8 months ago

Closing as the fix was released.