Hi all,
I'm trying to create COM wrapper around a class that needs to use remoting
to perform its tasks, so that I can unload the AppDomain after it's done.
I've build the code to create the AppDomain and load the MarshalByRef object
into the other domain. All works well when running this code in .NET.
However, if I run the exact same code through COM the Remoting through the
AppDomain fails. Specifically it looks like the Proxy is created but it
cannot be cast to the proper type.
public TypeParser CreateTypeParser()
{
if (!this.CreateAppDomain(null))
return null;
object T = this.LocalAppDomain.CreateInstance( "wwReflection",
"Westwind.wwReflection.TypeParser" ).Unwrap();
/// *** Ok both in COM and WinForm - returns MarshalByRef object
// System.Windows.Forms.MessageBox.Show( T.GetType().Name + "\r\n" +
T.ToString() );
// *** this works in WinForm but not in COM. COM get null
TypeParser Parser = T as Westwind.wwReflection.TypeParser;
if (Parser == null)
{
/// this code always fires with COM
System.Windows.Forms.MessageBox.Show( "Parser is null" );
return new TypeParser();
}
// Always get a result in WinFOrm
return Parser;
return new TypeParser();
}
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("wwReflection.TypeParser")]
public class TypeParser : MarshalByRefObject
{
...
}
Using the TypeParser object directly through COM works fine - it appears
something in the Remoting system is behaving very differently in COM hosting
than in WinForm hosting. Note that there is no error for the call to
CreateInstance() as there would be if the type can't found or instantiated.
If I mistype the type name it fails with an exception.
Any ideas on what's different?
+++ Rick ---

Signature
Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
Rick Strahl [MVP] - 26 Jan 2005 11:54 GMT
It turns out the problem is not due to COM interop but rather a problem in
Assembly loading. Interestingly enough the errors reflect nothing about the
fact that the assembly couldn't found or loaded correctly.
The exact problem is that CreateInstance in a new AppDomain is not
respecting the new AppDomains AppBasePath, but using the parents AppDomain
settings instead. The result was that the Assembly wanted to load out of
some odd directory.
There's more info on this here:
http://west-wind.com/weblog/posts/1358.aspx
+++ Rick ---

Signature
Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
> Hi all,
>
[quoted text clipped - 14 lines]
>
> object T = this.LocalAppDomain.CreateInstance( "wwReflection",
"Westwind.wwReflection.TypeParser" ).Unwrap();
> /// *** Ok both in COM and WinForm - returns MarshalByRef object
> // System.Windows.Forms.MessageBox.Show( T.GetType().Name + "\r\n" +
[quoted text clipped - 32 lines]
>
> +++ Rick ---