1
votes

I'm currently working on a Blazor WebAssembly Asp.NET hosted in .NET Standard 2.1.

I try to load resources files from a separate .NET Standard 2.1. C# Class library.

Unfortunately I always get the following error:

"Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Could not load type of field 'MyNamespace.Footer:k__BackingField' (0) due to: Could not load file or assembly 'MyNamespace.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies."

  1. My .resx files are set to Access Modifier: Public and look like this in the properties tab:

enter image description here

My project structure looks like this:

  • MyNamespace.BlazorApp.Client
  • MyNamespace.BlazorApp.Server
  • MyNamespace.BlazorApp.Resources

I load the resources like this:

  1. MyNamespace.BlazorApp.Client:

In my Program.cs Main Method I set the current culture like this:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");

and I add the Localization service:

services.AddLocalization();

Further on in a .razor page where I want to use the culture setting I inject the Localization service:

@inject IStringLocalizer<Translations> Translations
  1. MyNamespace.BlazorApp.Server

Here I simply load the project reference to my resources project. I don't use the .resx in this project.

Do you know how to load resources from an external .NET Standard 2.1. C# Class library into a Blazor Wasm project?

1
Shooting blindly... Does the 3rd party have AddRazorSupportForMvc in the project file? ref: docs.microsoft.com/en-us/aspnet/core/migration/…mynkow
Still quite a few details are missing: Where is Translations defined (ns & asm), what is the folderstructure? What is the exact name of the resx files?Henk Holterman

1 Answers

2
votes

The error comes from a name clash, the project can be compiled but not loaded.
I know a way to get it working, I'm not sure is this is the only or best way.

  1. Rename your resx project to MyNamespace.BlazorApp.Translations
  2. Make sure there is a public class Translation in the root (no 's')
  3. Make a Folder called Resources in the Translations project
  4. Add Translation.resx, Translation.es.resx etc to that folder
  5. Change the config to builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

Resources is a default name at several places, not a good idea to name your own project the same. The ResourcesPath seems to be required, I don't know what the defaults are.