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.83k stars 372 forks source link

Error publish Static WebApp System.MissingMethodException #1617

Closed f4n0 closed 8 months ago

f4n0 commented 8 months ago

🐛 Bug Report

I've published a static web app, but I get this error:

fail: Microsoft.AspNetCore.Components.Web.ErrorBoundary[0]
      System.MissingMethodException: Arg_NoDefCTor, Microsoft.FluentUI.AspNetCore.Components.Icons+Regular+Size20+Balloon
         at System.RuntimeType.CreateInstanceMono(Boolean , Boolean )
         at System.RuntimeType.CreateInstanceDefaultCtor(Boolean , Boolean )
         at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
         at System.Activator.CreateInstance(Type type, Boolean nonPublic)
         at System.Activator.CreateInstance(Type type)
         at BlazorGenerator.Utils.BlazorGenExtensions.ToFluentIcon(Type icon)
         at BlazorGenerator.Components.Menu.NavMenu.<BuildRenderTree>b__10_1(RenderTreeBuilder __builder2)
         at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32 , RenderFragment )
         at Microsoft.FluentUI.AspNetCore.Components.FluentNavMenu.<BuildRenderTree>b__45_1(RenderTreeBuilder __builder2)
         at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32 , RenderFragment )
         at Microsoft.AspNetCore.Components.CascadingValue`1[[Microsoft.FluentUI.AspNetCore.Components.FluentNavMenu, Microsoft.FluentUI.AspNetCore.Components, Version=4.4.1.24037, Culture=neutral, PublicKeyToken=null]].Render(RenderTreeBuilder builder)
         at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder , RenderFragment , Exception& )
System.MissingMethodException: Arg_NoDefCTor, Microsoft.FluentUI.AspNetCore.Components.Icons+Regular+Size20+Balloon

💻 Repro or Code Sample

🤔 Expected Behavior

😯 Current Behavior

💁 Possible Solution

🔦 Context

🌍 Your Environment

f4n0 commented 8 months ago

More context: The error is generated from my code:

    public static Icon? ToFluentIcon(this Type icon)
    {
      return Activator.CreateInstance(icon) as Icon;
    }

And helps me passing types instead of instances (I'm using attribute decorators)

dvoituron commented 8 months ago

By default, when you publish this type of project, Blazor deletes all parts of code not detected as "used". When using reflection, you must specify that this icon/code is used.

See https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options

f4n0 commented 8 months ago

I've also added <PublishTrimmed>false</PublishTrimmed> and


    <TrimmerRootAssembly Include="Microsoft.FluentUI.AspNetCore.Components" />
    <TrimmerRootAssembly Include="Microsoft.FluentUI.AspNetCore.Components.Icons" />
but it keeps failing
vnbaaij commented 8 months ago

You say it fails on your (added) code. So you of need to add your own namespace as well

f4n0 commented 8 months ago

also added

f4n0 commented 8 months ago

image and the ones that fails, is a "default" one

and referenced like this:

<FluentNavLink Href="@elem.Route" Match="NavLinkMatch.All" Icon="@(elem.Icon.ToFluentIcon())" IconColor="Color.Accent">@elem.Title</FluentNavLink>

vnbaaij commented 8 months ago

Maybe you need to add something like this to your code:

[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(_{your type name}_))]
vnbaaij commented 8 months ago

We are using Azure Static Web Apps for hosting the demo site ourselves and the icons etc are working there. See the demo client and demo shared projects here in the repo for the configuration we used. Closing this as it is not actionable from our side.