Why does GetManifestResourceStream returns null while the resource name exists when calling GetManifestResourceNames?

C#asp.net

C# Problem Overview


I have a web application project. I generated the DLL and import it in another project. I implemented VirtualPathProvider.

I followed this web site: http://support.microsoft.com/kb/910441/en-us?spid=8940&sid=global, and everything works until I add another site master.

  1. I added site_export.master and changed its Build Action to Embedded Resource.
  2. I changed my page to use the new site master.
  3. GetManifestResourceStream() returns null when I load site_export.master.
  4. I call GetManifestResourceNames() to check if site_export.master exists in the DLL and it does. It's in the list. All of the name spaces match. I didn't list the name space out here.

Why can't GetManifestResourceStream() load my new site_export.master? It loads site.master just fine. I know my DLL is loaded because I can see other files in the DLL.

C# Solutions


Solution 1 - C#

Remember the following issues...

Step 1. Build action set to embedded resource see

C#’s GetManifestResourceStream Gotcha

Step 2. Use namespace.resourcename see GetManifestResourceStream() returns null ?

Actually, this method returns null if a private resource in another assembly is accessed and the caller does not have ReflectionPermission with the ReflectionPermissionFlag.MemberAccess flag.

Solution 2 - C#

Side-hint. To make sure you're in the right assembly and with right name: dump and evaluate all the resources available in your target assembly

string[] names = assembly.GetManifestResourceNames();

(in my case, I misused a namespace from another assembly)

Solution 3 - C#

I did this to make it work:

Step 1: Select the resource (CRDF.xsl in my case) and right click > Properties. Set Build Action to "EmbeddedResource" and Logical Name to name of your choice, e.g. CRDFXSL.

Step 2 : Change your Source code like this:

Assembly _assembly;
  _assembly = Assembly.GetExecutingAssembly();         
  xslStream = _assembly.GetManifestResourceStream("CRDFXSL");
  _xmlReader = XmlReader.Create(xslStream);

Thus everything went smoooooooth..

Solution 4 - C#

Hint and Caution: If the "Assembly name" and "Default namespace" does not match in the project file then also GetManifestResourceStream would return null. GetManifestResourceNames still returns the names of assemblies but during loading the manifest would not work.

Solution 5 - C#

Try this:

Dim ctx As Windows.ApplicationModel.Resources.Core.ResourceContext = New Windows.ApplicationModel.Resources.Core.ResourceContext()
ctx.Languages = {Globalization.CultureInfo.CurrentUICulture.Name}
Dim rmap As Windows.ApplicationModel.Resources.Core.ResourceMap = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap
Dim res = rmap.GetValue("Assets/sample.png", ctx)
Dim resFile = Await res.GetValueAsFileAsync

The Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap list all resources.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMsBugKillerView Question on Stackoverflow
Solution 1 - C#ShahdatView Answer on Stackoverflow
Solution 2 - C#Yury SchkatulaView Answer on Stackoverflow
Solution 3 - C#Deepak ChaudharyView Answer on Stackoverflow
Solution 4 - C#RajivSView Answer on Stackoverflow
Solution 5 - C#Rodrigo T.View Answer on Stackoverflow