dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
34.91k stars 9.86k forks source link

Adding external dll dependency in Blazor WASM Shared project causing exception when deployed in IIS ! #47551

Open harshbhadoria011 opened 1 year ago

harshbhadoria011 commented 1 year ago

Hi,

I've created a Blazor WASM project in .NET6. In my use case, I need to select a file and that file will be processed by an external dll, it will process the data and return some output.

This external dll I've added in Shared project and from Client project in index.rajor, I'm calling respective methods of this dll. This external dll is .net 6 compiled dll.

In VS2022 on my developent machine, when I ran, it worked smoothly. But when I published and deployed in IIS on other server, I've got issues (pasted below).

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Invalid type TestApp.Shared.TestService for instance field TestApp.Client.Pages.Index:<Svc>k__BackingField System.TypeLoadException: Invalid type TestApp.Shared.TestService for instance field TestApp.Client.Pages.Index:<Svc>k__BackingField at System.RuntimeTypeHandle.CanCastTo(RuntimeType , RuntimeType ) at System.RuntimeType.IsAssignableFrom(Type ) at Microsoft.AspNetCore.Components.RouteTableFactory.<GetRouteableComponents>g__GetRouteableComponents|4_0(List 1 , Assembly ) at Microsoft.AspNetCore.Components.RouteTableFactory.GetRouteableComponents(RouteKey ) at Microsoft.AspNetCore.Components.RouteTableFactory.Create(RouteKey ) at Microsoft.AspNetCore.Components.Routing.Router.RefreshRouteTable() at Microsoft.AspNetCore.Components.Routing.Router.Refresh(Boolean isNavigationIntercepted) at Microsoft.AspNetCore.Components.Routing.Router.RunOnNavigateAsync(String path, Boolean isNavigationIntercepted) at Microsoft.AspNetCore.Components.Routing.Router.<>c__DisplayClass62_0.<RunOnNavigateAsync>b__1(RenderTreeBuilder builder) at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder , RenderFragment , Exception& )

One More Question: When i was trying to add this external dll dependency in my Blazor WASM Client project, I got error like "File not found". So, Shared project is the correct place to add other dependency?

To Reproduce Create a Blazor WASM project. Add any external dll dependency in the Shared project. Create an instance in Client's Program.cs -> builder.Service.AddScoped Inject this service in index.razor -> @inject ABCService svc

Further Technical Details SDK used is .NET 6 IDE VS2022

mkArtakMSFT commented 1 year ago

Thanks for contacting us. Looks like the assembly you're referencing is being trimmed, impacting the APIs that you rely on. Consider annotating it to avoid those methods to not be trimmed, following our documentation at: https://learn.microsoft.com/aspnet/core/blazor/host-and-deploy/configure-trimmer?view=aspnetcore-7.0

To confirm this is indeed the case, you can try to simply disable the trimmer for your project and see if that works, by setting the following property in your project file of the Shared project:

<PublishTrimmed>false</PublishTrimmed>
ghost commented 1 year ago

Hi @harshbhadoria011. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

harshbhadoria011 commented 1 year ago

Hi @mkArtakMSFT , I tried disabling trimming but it didn't work. For now, instead of dll, I used complete source code of that dll and it worked. Still I'm trying to figure out why dll is not able to resolve at blazor assembly runtime.

javiercn commented 1 year ago

@harshbhadoria011 How are you referencing the dll?

It seems that there is a problem in how the dll you are referring to is being consumed, are you using lazy loading? If so, the dll needs to be loaded before the type is even referenced by another type, otherwise you get a typeload exception (as you saw).

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 7 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.