我刚刚升级到Rc2,而以前的工作不再适用。我在一个单独的项目中有几个resx文件,我使用自定义类来访问数据。现在运行时出现以下错误:
MissingManifestResourceException:找不到适合指定文化或中性文化的任何资源。确保" GarageWeb.Core.CoreResources.resources"被正确嵌入或链接到汇编" GarageWeb.Core"在编译时,或者所有所需的附属程序集都是可加载和完全签名的。
编辑:我简化了这一步并创建了一个控制台应用程序,除了重现错误所需的内容之外,该应用程序除去了所有内容:https://github.com/GarageWeb/ResourceTest
以下是访问资源的类:
public class ResourceService : IResourceService
{
private readonly ILoggingService _loggingService;
private readonly ICoreGlobalResourceService _coreGlobalResources;
private readonly ISiteGlobalResourceService _siteGlobalResources;
public ResourceService(ILoggingService loggingService, ICoreGlobalResourceService coreGlobalResourceService, ISiteGlobalResourceService siteGlobalResources)
{
_loggingService = loggingService;
_coreGlobalResources = coreGlobalResourceService;
_siteGlobalResources = siteGlobalResources;
}
public string GetGlobalText(string resourceKey, bool includeBrackets = true)
{
var localizedString = _coreGlobalResources.ResourceManager.GetString(resourceKey);
if (string.IsNullOrEmpty(localizedString))
{
localizedString = _siteGlobalResources.ResourceManager.GetString(resourceKey);
}
if (string.IsNullOrEmpty(localizedString) && includeBrackets)
{
_loggingService.LogInvalidResource(resourceKey);
}
if (includeBrackets)
{
return localizedString ?? "[" + resourceKey + "]";
}
return localizedString ?? resourceKey;
}
public string BuildMessageFromResource(string resourceKey, string placeHolderResourceKey1,
bool includeBrackets = true)
{
var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets),
GetGlobalText(placeHolderResourceKey1, includeBrackets));
return errorString;
}
public string BuildMessageFromResourceAndArray(string resourceKey, string[] arrayOfValues,
bool includeBrackets = true)
{
var placeHolderValue = "";
for (var i = 0; i < arrayOfValues.Length; i++)
{
if (i + 1 == arrayOfValues.Length)
{
placeHolderValue += GetGlobalText(arrayOfValues[i], includeBrackets);
}
else
{
placeHolderValue += GetGlobalText(arrayOfValues[i], includeBrackets) + ", ";
}
}
var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets),
placeHolderValue);
return errorString;
}
public string BuildMessageFromResourceAndTwoArrays(string resourceKey, string[] firstArrayOfValues,
string[] secondArrayOfValues,
bool includeBrackets = true)
{
var placeHolderOneValue = "";
var placeHolderTwoValue = "";
for (var i = 0; i < firstArrayOfValues.Length; i++)
{
if (i + 1 == firstArrayOfValues.Length)
{
placeHolderOneValue += GetGlobalText(firstArrayOfValues[i], includeBrackets);
}
else
{
placeHolderOneValue += GetGlobalText(firstArrayOfValues[i], includeBrackets) + ", ";
}
}
for (var i = 0; i < secondArrayOfValues.Length; i++)
{
if (i + 1 == secondArrayOfValues.Length)
{
placeHolderTwoValue += GetGlobalText(secondArrayOfValues[i], includeBrackets);
}
else
{
placeHolderTwoValue += GetGlobalText(secondArrayOfValues[i], includeBrackets) + ", ";
}
}
var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets),
placeHolderOneValue, placeHolderTwoValue);
return errorString;
}
public string BuildMessageFromResource(string resourceKey, string placeHolderResourceKey1,
string placeHolderResourceKey2, bool includeBrackets = true)
{
var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets),
GetGlobalText(placeHolderResourceKey1, includeBrackets),
GetGlobalText(placeHolderResourceKey2, includeBrackets));
return errorString;
}
public string BuildMessageFromResource(string resourceKey, string placeHolderResourceKey1,
string placeHolderResourceKey2, string placeHolderResourceKey3,
bool includeBrackets = true)
{
var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets),
GetGlobalText(placeHolderResourceKey1, includeBrackets),
GetGlobalText(placeHolderResourceKey2, includeBrackets),
GetGlobalText(placeHolderResourceKey3, includeBrackets));
return errorString;
}
}
它在这里失败:var localizedString = _coreGlobalResources.ResourceManager.GetString(resourceKey);
有什么想法吗?有没有新的方法来嵌入这些资源?
答案 0 :(得分:6)
因此,如果我将.resx文件移动到项目的根目录而不是子文件夹中,它会按预期工作。我已尝试从子文件夹嵌入的各种方式,它不再有效。现在我将使用此解决方法,但我怀疑这是RC2中的一个错误。
答案 1 :(得分:3)
就像你提到的那样,我也相信这是一个错误。我想这么久,这个bug就位于自动生成的Designer工具中。 由于奇怪的原因,以及为什么您的解决方法有效,该工具假定您将所有resx文件放在应用程序的根目录中。
以下是该工具输出的示例:
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Cosmos.ViewModels.Default", typeof(Default).GetTypeInfo().Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
其中,Cosmos.ViewModels
是程序集名称,但.resx文件都收集在(对于我们当前的解决方案)Resources.da
命名空间中,提供了 {{的完全限定名称空间1}} 强>
考虑到这一点,您可以将文件保留在您希望的位置,然后将硬编码的Cosmos.ViewModels.Resources.da
字符串更改为 Cosmos.ViewModels.Default
。
当然,工具可能会意外地再次移除您的更改。
我希望微软能解决这个问题。我在github上创建了一个问题,但我认为问题实际上应该在核心cli中。
MissingManifestResourceException from included assembly although present #1534
答案 2 :(得分:0)
我正在使用MVC Core 1.1.0,我可以在子文件夹中添加资源文件。当您尝试更改没有子文件夹的资源文件的命名空间时,它会抛出错误。因此资源文件命名空间的生成与生成时相同。