5

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?

2
  • Shooting blindly... Does the 3rd party have AddRazorSupportForMvc in the project file? ref: learn.microsoft.com/en-us/aspnet/core/migration/… Commented Aug 3, 2020 at 9:37
  • 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? Commented Aug 3, 2020 at 10:13

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.