Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

c# - Assembly resources FileNotFoundException in project upgraded to VS 2010 framework 4.0

I have upgraded a C# WinForms application from Visual C# 2008 (framework 3.5) to 2010 (framework 4.0) and I'm getting runtime errors.

The application loads assemblies dynamically at runtime using Assembly.Load(filename). It is designed this way because depending on the users configuration it should load different implementations of the assembly located in different folders.

Everything works fine, except when the loaded dll has got embedded resources (bitmaps or xsd-datasets) then I get FileNotFoundException:

{"Could not find file 'FF.Fi_Stat.SKA.resources'.":null}

The assembly is called FF.Fi_Stat.SKA.dll. I do not understand this message because there are no external resources (only embedded ones) and no file with that name is generated by VS in the output directory. Any ideas?

--

More details:

This is how I load the assembly:

Assembly a = Assembly.LoadFile(assemblyFileName);

The actual loading of the assembly works, it is when I try to create an instance of a class inside the assembly that the exception occurs:

Type t = a.GetType("nameofclass");
Activator.CreateInstance(t);  //fails here

Here is the stack trace:

Exception has been thrown by the target of an invocation.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   ...

The inner exception stack trace:

   at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.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.GetString(String name, CultureInfo culture)
   at System.Resources.ResourceManager.GetString(String name)
   at FF.Fi_Stat.SKA.RegForm.InitializeComponent()
   at FF.Fi_Stat.SKA.RegForm..ctor()

I now realize that the dll's that don't work has this line in InitializeComponent in common:

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RegForm));

However as I mentioned this was no problem with framework 3.5.

--

SOLUTION FOUND

I found out that the problem was related to how the dotnet framework tried to find localized resources and after I added the following line to AssemblyInfo.cs in my assemblies it started working again.

[assembly: NeutralResourcesLanguageAttribute("sv-SE", UltimateResourceFallbackLocation.MainAssembly)]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SOLUTION FOUND

I found out that the problem was related to how the dotnet framework tried to find localized resources and after I added the following line to AssemblyInfo.cs in my assemblies it started working again.

[assembly: NeutralResourcesLanguageAttribute("sv-SE", UltimateResourceFallbackLocation.MainAssembly)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...