microsoft / fluentui-blazor

Microsoft Fluent UI Blazor components library. For use with ASP.NET Core Blazor applications
https://www.fluentui-blazor.net
MIT License
3.23k stars 294 forks source link

fix: System.ArgumentNullException in MessageService.ShowMessageBar #1986

Closed verdie-g closed 2 weeks ago

verdie-g commented 2 weeks ago

🐛 Bug Report

MessageService.ShowMessageBar can throw a System.ArgumentNullException.

fail: Microsoft.AspNetCore.Components.Web.ErrorBoundary[0]
      System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
       ---> System.ArgumentNullException: Value cannot be null. (Parameter 'Please use the constructor including parameters.')
         at Microsoft.FluentUI.AspNetCore.Components.Icon..ctor() in /_/src/Core/Components/Icons/Icon.cs:line 17
         at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Constructor(Object obj, IntPtr* args)
         at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
         --- End of inner exception stack trace ---
         at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
         at System.RuntimeType.CreateInstanceMono(Boolean nonPublic, Boolean wrapExceptions)
         at System.RuntimeType.CreateInstanceOfT()
         at System.Activator.CreateInstance[Icon]()
         at Microsoft.FluentUI.AspNetCore.Components.FluentIcon`1[[Microsoft.FluentUI.AspNetCore.Components.Icon, Microsoft.FluentUI.AspNetCore.Components, Version=4.6.1.24101, Culture=neutral, PublicKeyToken=null]].OnParametersSet() in /_/src/Core/Components/Icons/FluentIcon.razor.cs:line 93
         at Microsoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()
         at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

💻 Repro or Code Sample

@inject IMessageService MessageService

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

<FluentButton OnClick="@Hello">Hello</FluentButton>

<FluentMessageBarProvider Section="Test" />

@code {

    private void Hello()
    {
        MessageService.ShowMessageBar(options =>
        {
            options.Title = "Hello";
            options.Section = "Test";
        });
    }

}

🌍 Your Environment

c0g1t8 commented 2 weeks ago

@verdie-g

Did you add the icons package as per https://www.fluentui-blazor.net/IconsAndEmoji?

dotnet add package FluentUI.AspNetCore.Components.Icons
vnbaaij commented 2 weeks ago

Is this happening in a published application? If so, it is probably the trimmer being to aggressive. Try with adding to your project file: <ItemGroup> <TrimmerRootAssembly Include="Microsoft.FluentUI.AspNetCore.Components" /> </ItemGroup>

verdie-g commented 2 weeks ago

I could reproduce in a minimal application:

  1. dotnet new fluentblazorwasm -n FluentBlazorTest
  2. replace the Home.razor with the snippet of my first message
  3. run the app in debug mode

Did you add the icons package as per https://www.fluentui-blazor.net/IconsAndEmoji?

I confirm that this package is included in the template

Is this happening in a published application?

and that I've not published the app but just ran it from my IDE.

vnbaaij commented 2 weeks ago

~Ok, with these settings your home page is using SSR. Maybe that is it.~ Never mind, it's wasm...

Will take a look later

vnbaaij commented 2 weeks ago

Ok, the issue is that you have not specified an Intent of the MessageBar in the options.

It will go through this code

 return Content.Intent switch
                {
                    MessageIntent.Info => new CoreIcons.Filled.Size20.Info(),
                    MessageIntent.Warning => new CoreIcons.Filled.Size20.Warning(),
                    MessageIntent.Error => new CoreIcons.Filled.Size20.DismissCircle(),
                    MessageIntent.Success => new CoreIcons.Filled.Size20.CheckmarkCircle(),
                    _ => null,
                };

In your case it returns null and that is what gives you the error.

So 'workaround' is for now to supply an Intent (of type Messageintent) in the options.

I'll implement a default (MessageIntent.Info) for this for the next trelease.