Missing assembly when using reflection to call component method
Quote:
>Hi all,
>I have a component called MyNamespace.MyComponent which uses another
>component called MyNamespace.MyChildComponent in doing some of its
>work. I'm using reflection to call a ComponentMethod method of
>MyNamespace.MyComponents. The code I use is something like this:
>*** start of code ***
>Dim AssemblyName as String = "MyNamespace.MyAssembly"
>'create the type of object from the assembly
>Dim Assm As System.Reflection.Assembly = System.Reflection.Assembly. _
> LoadFrom(AssemblyName)
>Dim Tp As System.Type = Assm.GetType("MyNamespace.MyComponent")
>If Tp Is Nothing Then
> Throw New Exception("Was unable to get the type for
>MyNamesapce.MyComponent")
>End If
>Dim Comp As Object = System.Activator.CreateInstance(Tp)
>'get the method from the type and invoke it
>Dim MI As System.Reflection.MethodInfo = Tp.GetMethod("ComponentMethod")
>Dim ArgArray As Object() = New Object(2) {}
>ArgArray(1) = "FirstArg"
>ArgArray(2) = "SecondArg"
>MI.Invoke(Comp, ArgArray)
>*** end of code ***
>However, when I do this, I get a message like this:
>"File or assembly name MyNamespace.MyChildComponent, or one of its
>dependencies was not found."
>I find this very odd, because the assembly MyNamespace.MyChildComponent
>*does* exist - in fact, when I call ComponentMethod from an instance of
>MyNamespace.MyComponent without using reflection, it works all right.
>Could someone please tell me what I am doing wrong here?
OK, I found the solution to the problem.
Just to explain, for Gary's sake,
* MyNamespace.MyAssembly is the assembly that contains the component
MyNamespace.MyComponent, i.e. MyComponent is a public class in the project
that produces the assembly MyNamespace.MyAssembly.dll when it is built.
* MyNamespace.MyComponent has a method called ComponentMethod.
* When ComponentMethod is called, it uses another component (in the
same MyNamespace.MyAssembly, but it could be anywhere, really) called
MyNamespace.MyChildComponent to do some work.
Now in the code I posted earlier, the Assembly.LoadFrom method was
being called from another module that I didn't speak about (let's
call it MyOtherNamespace.MyParentModule.exe). What I found out was
that the assembly MyNamespace.MyAssembly and all its dependencies should
have been put in the same directory as MyOtherNamespace.MyParentModule.
I found this out when I looked at the bind log files (I had to
look at these directly in the C:\Winnt\Microsoft.Net folders,
because Fuslogvw.exe didn't work for me).
I found that the .Net framework was looking for the MyNamespace.MyAssembly
assembly in the folder that MyOtherNamespace.MyParentModule.exe was in.
Once I moved the assembly (and its dependencies) to that folder,
everything was resolved. Thanks to everyone who made suggestions.
However, I have another problem - this has to do with again using
reflection to call component's methods, except that the function calls
are being done from threads that have been generated from a ThreadPool.
What I find is that if I have more than one thread calling a component's
method using reflection (using the code I have above), then the first
thread goes through all right - I can step through the code right up to
the line
Quote:
> MI.Invoke(Comp, ArgArray)
and beyond that, but any succesive thread chokes on that line and the
component's method (in the example above, this would be
MyNamespace.MyComponent.ComponentMethod) is never called for this
successive thread call.
Does anyone have any idea why there should be problems combining
thread pooling with reflection?
TIA,
Akin
aknak at aksoto dot idps dot co dot uk