Invalid Type Cast Problem.

D

Danish Ahmed

Hi all

I developed a Visio AddIn in C# through Visual Studio.NET.

This .NET component access my other .NET assemblies for example logger assembly.

I also made a ClassLoader class ( that is in another assembly ClassLoader.dll ) that loads a class from given assembly.

The class loader assembly is statically binded to Visio AddIn assembly.

Due to assembly unloading problem first i load that assembly in another AppDomain and search for the given class name.

I do this through another class RemoteClassChecker that is also in ClassLoader.dll.

RemoteClassChecker is derived from MarshalByRefObject.

ClassLoader first creates RemoteClassChecker instance in another AppDomain through AppDomain.CreateInstanceAndUnwrap method and cast it to RemoteClassChecker and then validates the class presence in the given assembly through RemoteClassChecker's other functions.

If class is present or not it unloads the AppDomain in which RemoteClassChecker made. So the result is if class is present then assembly is loaded in current AppDomain otherwise not.

It all does work well in whole .NET environment but in Visio-AddIn scenario if my other assemblies are not present in VISIO.EXE folder CLR throws InvalidCastException( Specified cast is not allowed ) but if the assemblies are in VISIO.EXE folder again all things work well.

Buttttttttttttt!!! it creates problem for deployment of my assemblies. I want to deploy these in my own private folder for AddIn. Also i do not want to make assemblies strongly typed.

Also i know that when a .NET component is accessed from COM side CLR creates an AppDomain "DefaultDomain" and sets its base directory that of VISIO.EXE.

I also configured the CodeBase value in registry. Is there any other setting in registry that tolds CLR for DefaultDomain settings like BaseDirectory???? I ask this also as my guess about this problem is that my other assemblies are not in BaseDirectory's folder or in its relative folders so CLR would not find meta data of RemoteClassChecker's meta data when unwraping marshaled RemoteClassLChecker object.

Also note that if i do not cast object reference from AppDomain.CreateInstanceAndUnwrap method then no error occurrred. So i try reflection to invoke methods of RemoteClassChecker but this is also not working:)

Is it a COMInterop problem? or is it related to cross AppDomain accessing?? or some thing else???

If anyone have a solution for this then please reply. Also if there are some ins and outs of this topic then please give links of those resources.

Thanks in advance.

Regards

Danish Ahmed
 
C

Chris [Visio MVP]

This is a just a wild guess, because I haven't gotten down to learning the details that you are talking about. I have heard about "COM shims" helping Appdomain problems with .NET add-ins. Here's a link that talks about such:

http://blogs.msdn.com/dancre/archive/2004/05/08/128350.aspx

And this link talks about the COM Shim wizard that helps ease the whole process of wrapping and add-in in a shim:

http://msdn.microsoft.com/library/d...dno2k3ta/html/ODC_Office_COM_Shim_Wizards.asp


--
Hope this helps,

Chris Roth
Visio MVP






Hi all

I developed a Visio AddIn in C# through Visual Studio.NET.

This .NET component access my other .NET assemblies for example logger assembly.

I also made a ClassLoader class ( that is in another assembly ClassLoader.dll ) that loads a class from given assembly.

The class loader assembly is statically binded to Visio AddIn assembly.

Due to assembly unloading problem first i load that assembly in another AppDomain and search for the given class name.

I do this through another class RemoteClassChecker that is also in ClassLoader.dll.

RemoteClassChecker is derived from MarshalByRefObject.

ClassLoader first creates RemoteClassChecker instance in another AppDomain through AppDomain.CreateInstanceAndUnwrap method and cast it to RemoteClassChecker and then validates the class presence in the given assembly through RemoteClassChecker's other functions.

If class is present or not it unloads the AppDomain in which RemoteClassChecker made. So the result is if class is present then assembly is loaded in current AppDomain otherwise not.

It all does work well in whole .NET environment but in Visio-AddIn scenario if my other assemblies are not present in VISIO.EXE folder CLR throws InvalidCastException( Specified cast is not allowed ) but if the assemblies are in VISIO.EXE folder again all things work well.

Buttttttttttttt!!! it creates problem for deployment of my assemblies. I want to deploy these in my own private folder for AddIn. Also i do not want to make assemblies strongly typed.

Also i know that when a .NET component is accessed from COM side CLR creates an AppDomain "DefaultDomain" and sets its base directory that of VISIO.EXE.

I also configured the CodeBase value in registry. Is there any other setting in registry that tolds CLR for DefaultDomain settings like BaseDirectory???? I ask this also as my guess about this problem is that my other assemblies are not in BaseDirectory's folder or in its relative folders so CLR would not find meta data of RemoteClassChecker's meta data when unwraping marshaled RemoteClassLChecker object.

Also note that if i do not cast object reference from AppDomain.CreateInstanceAndUnwrap method then no error occurrred. So i try reflection to invoke methods of RemoteClassChecker but this is also not working:)

Is it a COMInterop problem? or is it related to cross AppDomain accessing?? or some thing else???

If anyone have a solution for this then please reply. Also if there are some ins and outs of this topic then please give links of those resources.

Thanks in advance.

Regards

Danish Ahmed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top