开发者

Can't load a manifest resource with GetManifestResourceStream()

开发者 https://www.devze.com 2023-01-04 02:59 出处:网络
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:

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 na开发者_运维技巧mespace 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


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"


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.


In my case,

When you try to access the file via GetManifestResourceStream(). You will get an error due to invalid path of the file, and stream will be null.

Solution:

Right click on the file which you have added in to solution and Click on Properties.

Select the Build Action as Embedded Resource. (Instead of Content - by default)

Can't load a manifest resource with GetManifestResourceStream()


By default, visual studio does not embed xsd file therefore you must ensure "Build Action" property of xsd file is set to "Embedded Resource" to make it works


just add your resources under form1.resx -->add existing items

double click on the resources you added under Resources folder.go to properties and select "Embedded Resources" instead of none.

Then try debugging the line:

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

check the resources you added are in the array. then copy the resource name exactly from this array and try putting the name on your code..it works fine!!


You can get the Resource Stream by passing the Resource Names which is as follows below...

  1. Get the resource name e.g..

    Assembly objAssembly = Assembly.GetExecutingAssembly();

    string[] strResourceNames = objAssembly.GetManifestResourceNames();

  2. Pass the Resource Names to ...

    Stream strm = objAssembly.GetManifestResourceStream(strResourceNames);

Now you have Stream you can do whatever you want...


In my case, it was something completely different:

My UWP App compiled correctly in Debug and Release configuration but GetManifestResourceStream returned Null only Release configuration.

The issue was, that in the UWP Build Configuration file (and only there) the setting "Compile with .NET Native tool chain" was enabled. After disabling, GetManifestResourceStream worked as expected.


I had an issue where I was embedding a whole heap of .xsd files in many different assemblies; everything was working (GetManifestResourceNames was returning the files I'd expect to see) except for one. The one which wasn't was called:

Something.LA.xsd

I wasn't dealing with specific cultures and the .LA bit at the end of the filename was being picked up by the compiler as this file being for the LA culture - the filename in the manifest was going in as Something.xsd (under culture LA) - hence me not being able to find it (it ended up in a satellite assembly). I dodged the issue by renaming the file - presumably it is possible to explicitly state the culture of a given embedded resource.

Actually, a quick google reveals: How can I prevent embedded resource file culture being set based on its filename

According to this answer, you've got to do hacky things - so perhaps renaming the file wasn't so bad after all :)

0

精彩评论

暂无评论...
验证码 换一张
取 消