locked
How to fix 'FileNotFoundException: Could not load file or assembly' when try to subscribe COM event. RRS feed

  • Question

  • When I subscribe to event of COM+ object I get error:

    System.Reflection.TargetInvocationException
      HResult=0x80131604
      Message=Exception has been thrown by the target of an invocation.
      Source=System.EnterpriseServices
      StackTrace:
       at System.EnterpriseServices.IRemoteDispatch.RemoteDispatchNotAutoDone(String s)
       at System.EnterpriseServices.RemoteServicedComponentProxy.Invoke(IMessage reqMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at ComCalculator.Calculator.add_CalculatedEvent(CalculatedHandler value)
       at ConsoleApp1.Program.Main(String[] args) in C:\Users\vekas\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 66

    Inner Exception 1:
    FileNotFoundException: Could not load file or assembly 'ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    I create simple COM+ object and client application both on C#.

    I register it via regsvcs and add it to GAC via gacutil.

    COM+ object (dll library, .net framework 4.6.1):

    [assembly: ApplicationActivation(ActivationOption.Server)]
    [assembly: ApplicationAccessControl(AccessChecksLevel = AccessChecksLevelOption.Application, Authentication = AuthenticationOption.None, ImpersonationLevel = ImpersonationLevelOption.Anonymous, Value = false)]
    [assembly: ApplicationName("ComCalculator")]
    [assembly: AssemblyKeyFile("ComCalculator.snk")]
    
    
    
    [Guid("FB49CFF3-546F-43EC-B295-E59F8E09644F")]
        [ComVisible(true)]
        [ProgId("ComCalculator.Calculator")]
        [ComSourceInterfaces(typeof(IEvents))]
        [ClassInterface(ClassInterfaceType.None)]
        [EventTrackingEnabled(true)]
        public class Calculator : ServicedComponent, ICalculator
        {
            [ComVisible(false)]
            public delegate void CalculatedHandler(string args);
    
            public event CalculatedHandler CalculatedEvent;
    
            [DispId(1)]
            public int Sum(int a, int b)
            {
                if (CalculatedEvent != null)
                    CalculatedEvent("tet");
                return a + b;
            }
        }
    
    [Guid("5D71F0EB-EEFD-4FEC-9821-C6526E11DB7B")]
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface ICalculator
        {
            [DispId(1)]
            int Sum(int a, int b);
        }
    
    [Guid("A7487C90-11E6-4A4B-BA21-E43F0BCF89EF")]
        [ComVisible(true)]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IEvents
        {
            [DispId(1)]
            void CalculatedEvent(string args);
        }

    Client:

    ComCalculator.Calculator c = new ComCalculator.Calculator();
    c.CalculatedEvent += new ComCalculator.Calculator.CalculatedHandler(C_CalculatedEvent); //error

    I enabled Fusion Log and found this error in folder "dllhost.exe":

    *** Assembly Binder Log Entry  (9/19/2019 @ 3:16:09 PM) ***
    
    The operation failed.
    Bind result: hr = 0x80070002. The system cannot find the file specified.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\WINDOWS\SysWOW64\dllhost.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: DisplayName = ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
     (Fully-specified)
    LOG: Appbase = file:///C:/WINDOWS/SysWOW64/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = dllhost.exe
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: No application configuration file found.
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1/ConsoleApp1.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1.EXE.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1/ConsoleApp1.EXE.
    LOG: All probing URLs attempted and failed.
    
    
    *** Assembly Binder Log Entry  (9/19/2019 @ 3:16:09 PM) ***
    
    The operation failed.
    Bind result: hr = 0x80070002. The system cannot find the file specified.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\WINDOWS\SysWOW64\dllhost.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: DisplayName = ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
     (Fully-specified)
    LOG: Appbase = file:///C:/WINDOWS/SysWOW64/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = dllhost.exe
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: No application configuration file found.
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1/ConsoleApp1.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1.EXE.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1/ConsoleApp1.EXE.
    LOG: All probing URLs attempted and failed.

    Thursday, September 19, 2019 12:23 PM

Answers

  • I didn't find reason of such behavior, but I found solution for it in another forum.

    http://www.rsdn.org/forum/dotnet/127922.flat#127922

    I a nutshell, subscription created via additional library on C++ using IConnectionPointContainer

    • Marked as answer by Vadik16 Sunday, October 27, 2019 11:18 AM
    Sunday, October 27, 2019 11:18 AM

All replies

  • How did you declare ComCalculator?  Did you add it as a reference?  It's curious to me that it's trying to load ConsoleApp1.DLL.  Did you name both projects ConsoleApp1?

    Your console app was compiled specifically for 32-bit.  Did you do the same for your COM object?  If you compiled it for AnyCPU, it will  be 64-bit on a 64-bit system.  In-process COM invocations cannot cross the 32/64 boundary.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, September 19, 2019 6:08 PM
  • ComCalculator added to ConsoleApp1 as a reference.

    Name of COM object project is ComCalculator. Target framework: 4.6.1. Output type: Class Library. It compiled for AnyCPU.

    Name of project that use ComCalculator is ConsoleApp1. Target framework: 4.6.1. Output type: Console Application. It was compiled for x86 and I try to change it for AnyCPU, but i get the same error in Fusion log.

    System: Windows 10 Home x64.

    Fusion log after changing ConsoleApp1 to AnyCPU:

    *** Assembly Binder Log Entry  (9/20/2019 @ 8:32:15 AM) ***
    
    The operation failed.
    Bind result: hr = 0x80070002. The system cannot find the file specified.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\WINDOWS\SysWOW64\dllhost.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: DisplayName = ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
     (Fully-specified)
    LOG: Appbase = file:///C:/WINDOWS/SysWOW64/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = dllhost.exe
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: No application configuration file found.
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1/ConsoleApp1.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1.EXE.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1/ConsoleApp1.EXE.
    LOG: All probing URLs attempted and failed.
    
    
    *** Assembly Binder Log Entry  (9/20/2019 @ 8:32:15 AM) ***
    
    The operation failed.
    Bind result: hr = 0x80070002. The system cannot find the file specified.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\WINDOWS\SysWOW64\dllhost.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: DisplayName = ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
     (Fully-specified)
    LOG: Appbase = file:///C:/WINDOWS/SysWOW64/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = dllhost.exe
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: No application configuration file found.
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1/ConsoleApp1.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1.EXE.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/ConsoleApp1/ConsoleApp1.EXE.
    LOG: All probing URLs attempted and failed.

    Friday, September 20, 2019 6:22 AM
  • Then I don't understand why the Appbase is C:\Windows\SysWow64.  That's for 32-bit applications.  It's possible that's just a quirk of the Fusion log tool, but it's very suspicious.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, September 20, 2019 6:35 AM
  • I didn't find reason of such behavior, but I found solution for it in another forum.

    http://www.rsdn.org/forum/dotnet/127922.flat#127922

    I a nutshell, subscription created via additional library on C++ using IConnectionPointContainer

    • Marked as answer by Vadik16 Sunday, October 27, 2019 11:18 AM
    Sunday, October 27, 2019 11:18 AM