Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assembly.Load(): Fails / Unable To Load Resource Via T4 Template #20

Open
GoogleCodeExporter opened this issue Mar 30, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

Using SVN version of Subversion. Custom repository library embeds the 
SqlMap.config file into the assembly as a resource. The following is the 
initialization code that is used to setup the various custom repositories 
(shared base class):

    protected static void Initialize()
    {
      ConfigurationSetting configurationSetting = new ConfigurationSetting();
      configurationSetting.Properties.Add("nullableInt", "int?");

      string resource = String.Format("assembly://{0}/Configuration/SqlMap.config", Assembly.GetAssembly(typeof(BaseRepository)).GetName().Name);
      try {
        IConfigurationEngine engine = new DefaultConfigurationEngine(configurationSetting);
        engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

        IMapperFactory mapperFactory = engine.BuildMapperFactory();
        _sessionFactory = engine.ModelStore.SessionFactory;
        _dataMapper = ((IDataMapperAccessor) mapperFactory).DataMapper;
      }
      catch (Exception ex) {
        Exception e = ex;
        while (e != null) {
          Console.WriteLine(e.Message);
          Console.WriteLine(e.StackTrace);
          e = e.InnerException;
        }
        throw;
      }

However, when attempting to use the specified custom data repository in a T4 
template, a System.IO.FileNotFoundException is generated with the name of the 
assembly containing the repository.

When tracing the problem, I discovered that if I pass in the fully qualified 
assembly name (Name=mydata,Version=1.0,etc) to the Assembly.Load function, it 
would work. My first thought was to just update the assembly:// reference with 
the full assembly name, however the new Uri() fails validation when creating 
the Uri. Instead, I updated the AssemblyResource() constructor to do a try 
catch, first attempting to load the resource as before and then to walk the 
loaded assembly references looking for a name match.

            assembly = Assembly.Load(resourceAssemblyName);

change to

            try {
              assembly = Assembly.Load(resourceAssemblyName);
            }
            catch (FileNotFoundException) {
              // Attempt to find assembly in loaded assembly list
              int index = 0;
              Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
              while ((assembly == null) && (index < assemblies.Length)) {
                if (String.Compare(assemblies[index].GetName().Name, resourceAssemblyName) == 0) {
                  assembly = assemblies[index];
                }
                index++;
              }
            }

Similar changes were made to TypeResolver.cs and DbProvider.cs. There should 
probably be a LoadAssembly() function created in a common module shared by all 
projects.

When not running in the T4 context, the exception is not thrown, so I'm 
assuming this is a dynamic compiler issue. The additional check to find the 
referenced assembly for the user shouldn't adversely affect performance.

Original issue reported on code.google.com by [email protected] on 13 Dec 2010 at 9:23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant