I have an asp.net core project with the following properties:
I'm trying to use asp.net localization, but the resourcemanager cannot locate the language resources. The docs provide some answer:
If the root namespace of an assembly is different than the assembly name:
- Localization does not work by default.
- Localization fails due to the way resources are searched for within the assembly. RootNamespace is a build-time value which is not available to the executing process. If the RootNamespace is different from the AssemblyName, include the following in AssemblyInfo.cs (with parameter values replaced with the actual values):
using System.Reflection; using Microsoft.Extensions.Localization; [assembly: ResourceLocation("Resource Folder Name")] [assembly: RootNamespace("App Root Namespace")]
The problem is that in .net core 5 there is no longer an assemblyInfo.cs file but its being autogenerated. This means i'd need to add RootNamespace
to the .csproj file somehow.
How can i do this?
EDIT I've tried adding:
<ItemGroup>
<AssemblyMetadata Include="ResourceLocation" Value="Resources" />
<AssemblyMetadata Include="RootNamespace" Value="Cmp.WebApi" />
</ItemGroup>
To the .csproj file but that did not work - it generated the following assemblyInfo in obj
[assembly: System.Reflection.AssemblyMetadata("ResourceLocation", "Resources")]
[assembly: System.Reflection.AssemblyMetadata("RootNamespace", "OPG.WebApi")]
Also note that this works fine if the RootNameSpace is the same as the assemblyname.
EDIT 2
I've also tried:
<PropertyGroup>
<AssemblyResourceLocation>Resources</AssemblyResourceLocation>
<AssemblyRootNamespace>Cmp.WebApi</AssemblyRootNamespace>
</PropertyGroup>
But that did not work aswell, which seems logical considering that i don't see those properties in the msbuild task:
One option (and the only i found) is to add a rather empty AssemblyInfo.cs
.
using System.Runtime.InteropServices;
using Microsoft.Extensions.Localization;
// In SDK-style projects such as this one, several assembly attributes that were historically
// defined in this file are now automatically added during build and populated with
// values defined in project properties. For details of which attributes are included
// and how to customise this process see: https://aka.ms/assembly-info-properties
// Setting ComVisible to false makes the types in this assembly not visible to COM
// components. If you need to access a type in this assembly from COM, set the ComVisible
// attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM.
[assembly: Guid("677ac300-e567-46dd-9281-cafe8d644224")]
[assembly: ResourceLocation("Resources")]
[assembly: RootNamespace("Cmp.WebApi")]
Which you can simply do by selecting the properties
folder, right clicking to open the context menu and then choose add > new item...
, and then choosing the AssemblyInfo.cs template:
This was in a asp.net core 5 project.