Unanswered CreateInstance throws cannot load type

  • 1 สิงหาคม 2555 12:39
     
      มีโค้ด

    So we have a over 33 VB6 COM+ component which we are migrating to C#. Within these VB6 components we are capturing the dll name, classname, and parameters into a csv file.

    I've written a unit test to read this csv file and to dynamically load the VB6 dll via CreateComInstanceFrom.

    All of these COM+ components implement 1 of 2 interfaces, either an IQuery or IUpdate. I call IsAssignableFrom to determine which interface it is using and then call the appropriate method.

    This works fine. Now comes the C# side.

    The migrated C# COM+ component does not want to load dynamically.

    I've got the following code             

                        System.Reflection.Assembly _assembly; 
                        _assembly = System.Reflection.Assembly.LoadFrom(assembly.CSharpAssemblyName);
                        CSharptype = _assembly.GetType(vb6Class.CSharpClassName);
    
                        CSharpcsimClass = _assembly.CreateInstance(CSharptype.FullName, true);
                        //CSharpcsimClass = Activator.CreateInstance(CSharptype);

    So my assembly.CSharpAssemblyName is the following - C:\\Projects\\bin\\MDRX.PM.Server.RegQrySvr.dll

    It generates the _assembly object with no errors.

    {MDRX.PM.Server.RegQrySvr, Version=1.0.141.0, Culture=neutral, PublicKeyToken=5429b6a38ddbad39}

    Then I call GetType passing in the name of the class I want - RegQrySvr.RegQueryRegInfoByPatient

    It also executes successfully

    CSharptype = {Name = "RegQueryRegInfoByPatient" FullName = RegQrySvr.RegQueryRegInfoByPatient"}

    But then the CreateInstance fails

    Message = "Cannot load type 'RegQrySvr.RegQueryRegInfoByPatient, MDRX.PM.Server.RegQrySvr, Version=1.0.141.0, Culture=neutral, PublicKeyToken=5429b6a38ddbad39'."

    If I add a reference to my project to MDRX.PM.Server.RegQrySvr.dll, this set will execute successfully, but I don’t want to add the reference since we will have 33 COM+ components once all these VB6 components are migrated


    Joe Pacelli

ตอบทั้งหมด

  • 1 สิงหาคม 2555 14:13
    ผู้ดูแล
     
     

    What is the type declaration for the variable, CSharpcsimClass, and how are you declaring it?  What type should it be, Object or something else?  It seems that it should be a type defined within your RegQrySrv assembly.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


  • 1 สิงหาคม 2555 14:48
     
      มีโค้ด

    CSharpcsimClass is defined as object because it can be any one of the 33 COM+ components.

    I change the output directory of this project to be the same directory this assembly reside's in and now instead of the cannot load type. I get the following error.

    Message = "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"

    Only change I did was putting this output in the same directory as my COM+ component output.

    I get this message regardless of how I try to create it

                        CSharpcsimClass = _assembly.CreateInstance(CSharptype.FullName, true);
                        //CSharpcsimClass = Activator.CreateInstanceFrom(assembly.CSharpAssemblyName, vb6Class.CSharpClassName);
                        //CSharpcsimClass = Activator.CreateComInstanceFrom(assembly.CSharpAssemblyName, vb6Class.CSharpClassName).Unwrap();
    


    Joe Pacelli

  • 1 สิงหาคม 2555 15:24
    ผู้ดูแล
     
     

    That error might be a result of where the assembly is located, off of the root directory.  I think it is a security violation from the OS.

    http://windows.microsoft.com/en-US/Windows7/How-do-I-open-a-file-if-I-get-an-access-denied-message  

    http://blogs.msdn.com/b/astebner/archive/2005/08/16/452523.aspx  

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • 1 สิงหาคม 2555 16:45
     
     
    Does the RegQrySvr.RegQueryRegInfoByPatient class is public and have a public parameterless constructor ?
  • 2 สิงหาคม 2555 11:59
     
     

    Yes it has a public parameterless constructor and the class itself is public.

    At one point it ran yesterday. Now it's back to the first error message I received

    Message = "Cannot load type 'RegQrySvr.RegQueryRegInfoByPatient, MDRX.PM.Server.RegQrySvr, Version=1.0.141.0, Culture=neutral, PublicKeyToken=5429b6a38ddbad39'."

    I moved all the output directories to be the same, so all dll's are in the same directory now. So I'm not sure why I get this error


    Joe Pacelli

  • 3 สิงหาคม 2555 13:08
    ผู้ดูแล
     
     
    Make sure that the assembly version that your project is looking for matches the version of the file that is being found.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • 22 สิงหาคม 2555 6:36
     
     
    For the access denied error, when reproducing the issue, please use process monitor tool (http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) to log file/registry activities of your app, and then you can analyze the file/registry activities log to find out which file (or registry key) your app is trying to access with insufficient permission.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Regards,
    Eric Yang
    Microsoft Online Community Support