Can't load a manifest resource with GetManifes

2019-01-11 12:30发布

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

8条回答
Summer. ? 凉城
2楼-- · 2019-01-11 12:35

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.

查看更多
虎瘦雄心在
3楼-- · 2019-01-11 12:38

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...

查看更多
Emotional °昔
4楼-- · 2019-01-11 12:44

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.

查看更多
放我归山
5楼-- · 2019-01-11 12:47

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"

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-11 12:47

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 :)

查看更多
萌系小妹纸
7楼-- · 2019-01-11 12:54

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)

Build action property set to embedded resource

查看更多
登录 后发表回答