我正在开发一个包含 Asp.Net Web 应用程序和 Windows 服务应用程序的项目。这两个可以使用我创建的用于包含资源文件的共同项目。我想将资源文件放在程序集之外,因此当我想更改它们时,它们可以在不编译所有解决方案的情况下进行编辑。我搜索一种方法,并在这里找到一种方法;
ASP.NET Resourcemanager 读取本地 .resx
它在 Web 应用程序上运行良好,但过了一会儿我看到,Windows 服务应用程序无法读取该资源,我得到了
System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture (or the neutral culture) on disk.
baseName: abc locationInfo: <null> fileName: abc.resx
at System.Resources.FileBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream)
at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture)
at yyyy.yyyyyy.yyyyyyy.GetGlobalResourceObject(String classKey, String resourceKey, Boolean getCommon)
at xxx.xxx.xxx.Entities.xxxxxxxx.get_LocalizedName()
at yyyyy.Adapters.ArchiverDtoAdapter.CreateArchiverInjectionsDto(InjectionProcedure procedure, INamingService namingService)
at yyyyy.Adapters.ArchiverDtoAdapter.ConvertInjectionProcedureDto(InjectionProcedure procedure, INamingService namingService)
at yyyy.Factories.ArchiverDtoFactory.<>c__DisplayClass1.<CreateInjectionProcedureSendRequestDto>b__0(IUnitOfWork unitOfWork)
at Core.Data.NHibernate.Factory.UnitOfWorkFactoryBase.Create(Action`1 action, IUnitOfWork unitOfWork)
at Core.Data.NHibernate.Factory.UnitOfWorkFactory.Create(Action`1 action)
我不明白这个问题,因为我的项目可以正常工作,而另一个则不能。另一个奇怪的行为是,当我在我的机器上启动服务并进行调试以查看发生了什么时,它可以工作。
更新:这是我引用的代码
public class ResxResourceManager : ResourceManager
{
public ResxResourceManager(string baseName, string resourceDir)
{
ObjectFactory.ResolveDependencies(this);
Type[] paramTypes = new Type[] { typeof(string), typeof(string), typeof(Type) };
object[] paramValues = new object[] { baseName, resourceDir, typeof(ResXResourceSet) };
Type baseType = GetType().BaseType;
ConstructorInfo ci = baseType.GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
null, paramTypes, null);
ci.Invoke(this, paramValues);
}
protected override string GetResourceFileName(CultureInfo culture)
{
string resourceFileName = base.GetResourceFileName(culture);
return resourceFileName.Replace(".resources", ".resx");
}
}