Embedded resources fail to load when using VB.Net
-------------------------------------------------

         Key: IBATISNET-85
         URL: http://issues.apache.org/jira/browse/IBATISNET-85
     Project: iBatis for .NET
        Type: Bug
    Reporter: Ron Grabowski
 Assigned to: Gilles Bayon 
    Priority: Minor


This line loads the embedded file correclty in C#:

 <providers embedded="Resources.Providers.config, CompanyName.Data" />

but throws the following exception in VB.Net:

[NullReferenceException: Object reference not set to an instance of an object.]
   IBatisNet.Common.Utilities.Resources.GetEmbeddedResourceAsXmlDocument(String 
resource) +129

The issue is that C# requires the full namespace to the embedded resource be 
used with GetManifestResourceStream while in VB.Net only the file name is 
passed in:
 
 http://www.vbbox.com/blog/2004/05/net-resource-management-stringtable.html

 // C#
 Assembly assembly = Assembly.GetExecutingAssembly();
 Stream s = assembly.GetManifestResourceStream("MyApplication.XMLFile1.xml");

 ' VB.Net
 Dim asm As Assembly = Assembly.GetExecutingAssembly()
 Dim s As Stream = asm.GetManifestResourceStream("XMLFile1.xml")

Here's some debug output I added to GetEmbeddedResourceAsXmlDocument (if you 
open Resources.cs this will make more sense):

 fileInfo.AssemblyName: [CompanyName.Data]
 fileInfo.ResourceFileName: [CompanyName.Data.Resources.Providers.config]
 assembly.FullName: [CompanyName.Data, Version=0.1.0.0, Culture=neutral, 
PublicKeyToken=null]
 assembly.GetManifestResourceNames().Length: [1]
 assembly.GetManifestResourceNames(): [Providers.config]
 Could not load embedded resource from assembly

A fix is to replace this line:

 Stream stream = assembly.GetManifestResourceStream(fileInfo.ResourceFileName);

with this:

 Stream stream = assembly.GetManifestResourceStream(fileInfo.ResourceFileName);
 if (stream == null)
 {
 stream = assembly.GetManifestResourceStream(fileInfo.FileName);
 }

There is other code in the GetEmbeddedResourceAsXmlDocument method that calls 
GetManifestResourceStream(fileInfo.FileName) but I think that code is called 
only when the assembly name is not specified by the user. Using this notation:

 <providers embedded="Resources.Providers.config" />

won't solve the problem because the for loop may load the resource from an 
incorrect assembly.

With the suggested code change, I think this means that its not possible to 
ship an assembly with embedded resources in this structure and guanrentee that 
the correct resource will be loaded under VB.Net:

 Resources\SqlMaps\SqlServer\User.xml
 Resources\SqlMaps\OleDb\User.xml
 Resources\SqlMaps\Oracle\User.xml

Perhaps we should caution users who are developing in for deployment in another 
language and recommend they use distinct filenames:

 ???
 Resources\SqlMaps\SqlServer-User.xml
 Resources\SqlMaps\OleDb-User.xml
 Resources\SqlMaps\Oracle-User.xml

-1 for VB.Net

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira

Reply via email to