Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't load a manifest resource with GetManifestResourceStream()

I've created a custom configuration section using XSD. In order to parse the config file that follows this new schema, I load the resource (my .xsd file) with this:

public partial class MonitoringConfiguration     {         public const string ConfigXsd = "MonitoringAPI.Configuration.MonitoringConfiguration.xsd";         public const string ConfigSchema = "urn:MonitoringConfiguration-1.0";          private static XmlSchemaSet xmlSchemaSet;          static MonitoringConfiguration()         {             xmlSchemaSet = new XmlSchemaSet();             Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);             XmlReader schemaReader = XmlReader.Create(xsdStream);             xmlSchemaSet.Add(ConfigSchema, schemaReader);         }      } 

By the way my resource is: MonitoringConfiguration.xsd. And the namespace of the other partial class (that represents the code behind of the .xsd file) is MonitoringAPI.Configuration.

The problem is situated here:

 Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd); 

The xsdStream is null, so I guess the resource can't be found! But why?

Thank you

like image 957
Amokrane Chentir Avatar asked Jun 18 '10 10:06

Amokrane Chentir


2 Answers

The name of the resource is always:

<Base namespace>.<RelativePathInProject>.<FileName>

So if your resource is located in "Resources/Xsd/", and your default project namespace is "MonitoringAPI.Configuration", the resource name is:

"MonitoringAPI.Configuration.Resources.Xsd.MonitoringConfiguration.xsd"

Also make sure the build action for your resource is set to "Embedded Resource"

like image 178
Philippe Leybaert Avatar answered Sep 19 '22 04:09

Philippe Leybaert


Easy and correct way to get the actual name of your embedded resource:

string[] resourceNames =     Assembly.GetExecutingAssembly().GetManifestResourceNames(); 

Then simply check resourceNames array, and you will know for sure what to pass to GetManifestResourceStream method.

like image 23
user1958681 Avatar answered Sep 21 '22 04:09

user1958681