You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a library for COM interop recently to communicate with a software used internally on my job, but I'm experiencing some strange behavior on dynamic proxy creation for the classes i use to exchange and call methods from interop. Below are some code referring to how the classes actually are defined and how the dynamic proxies are integrated into the code itself.
The main idea is when this proxy class is instantiated, a lazy reference for the COM interface is wrapped by a dynamic proxy so all method calls made to the "instance" property are expected to be intercepted by the class defined on the InterceptableFromAttribute.
To give some context on how COM reference is being wrapped as a dynamic proxy, below is the code for the "GetDynamicProxyForInterface" and "ResolveInstance" methods called on implementation.
publicstaticLazy<TInterface>ResolveInstance<TInterface>(thisIProxyBase<TInterface>proxy)whereTInterface:class{Func<TInterface>lazyDelegate;IInterceptor[]?interceptors;interceptors= GetInterceptorsFromProxyMetadata(proxy);// Creating the lazy delegate instance initializer for proxy instance propertylazyDelegate=()=> GetDynamicProxyForInterfaceInternal(proxy, interceptors);returnnewLazy<TInterface>(lazyDelegate);}privatestatic TInterface GetDynamicProxyForInterfaceInternal<TInterface>(thisIProxyBase<TInterface>proxy,IInterceptor[]?interceptors,TInterface?interfaceRef=default)whereTInterface:class{vargenerator= Generator;TInterfaceinterfaceProxy=default;// In case the interface is not an empty/null referenceif(interfaceRef is not null){interfaceProxy=(TInterface)generator.CreateInterfaceProxyWithTargetInterface(
interfaceRef,
interceptors);}elseif(interfaceRef isnull){TInterfacetInterfaceReference= proxy.CreateNew<TInterface>();interfaceProxy=(TInterface)generator.CreateInterfaceProxyWithTargetInterface(
tInterfaceReference,
interceptors);}returninterfaceProxy;}
The problem is that if i copy/paste this code in a new class file in one project (let's consider a console app for example) and call any method through instance property the code runs fine and as expected is intercepted by the target class. But when I put this class definition in a separated class library project and then reference it on the console app example, the dynamic proxy is also created when the class is instantiated, but for all method calls the following exception is shown:
System.MissingMethodException : Error:Missingmethod 'instance <return_type>[com_assembly] t_com_interface::SomeMethod()' fromclass 'Castle.Proxies.ApplicationProxy'.
at t_com_interface::SomeMethod()// Rest of stack trace
Obs.: It appears even when a method that is virtual (so it's possible to intercept) is called.
I tried to create a COM object directly through Activator.CreateInstance and also removing the logic of dynamic proxy creation from the constructor and it's working fine. It only appears to struggle when the dynamic proxy is called from another project/assembly that it's not where it was created.
It's the first time I'm trying to use the library so it's kinda complicated to figure out why this is happening, does anyone have an idea what could be the issue?
The text was updated successfully, but these errors were encountered:
I'm working on a library for COM interop recently to communicate with a software used internally on my job, but I'm experiencing some strange behavior on dynamic proxy creation for the classes i use to exchange and call methods from interop. Below are some code referring to how the classes actually are defined and how the dynamic proxies are integrated into the code itself.
The main idea is when this proxy class is instantiated, a lazy reference for the COM interface is wrapped by a dynamic proxy so all method calls made to the "instance" property are expected to be intercepted by the class defined on the InterceptableFromAttribute.
To give some context on how COM reference is being wrapped as a dynamic proxy, below is the code for the "GetDynamicProxyForInterface" and "ResolveInstance" methods called on implementation.
The problem is that if i copy/paste this code in a new class file in one project (let's consider a console app for example) and call any method through instance property the code runs fine and as expected is intercepted by the target class. But when I put this class definition in a separated class library project and then reference it on the console app example, the dynamic proxy is also created when the class is instantiated, but for all method calls the following exception is shown:
Obs.: It appears even when a method that is virtual (so it's possible to intercept) is called.
I tried to create a COM object directly through Activator.CreateInstance and also removing the logic of dynamic proxy creation from the constructor and it's working fine. It only appears to struggle when the dynamic proxy is called from another project/assembly that it's not where it was created.
It's the first time I'm trying to use the library so it's kinda complicated to figure out why this is happening, does anyone have an idea what could be the issue?
The text was updated successfully, but these errors were encountered: