我正在开发一个包含Asp.Net Web应用程序和Windows服务应用程序的项目。这两个可以使用我创建的共同项目来包含资源文件。我想把资源文件放在程序集的一边,所以当我想要更改它们时,它们可以编辑而无需编译所有解决方案。我搜索一个方法,在这里找到一个;
ASP.NET Resourcemanager to read local .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");
}
}