I'm trying to create an instance of a class in a new app domain within my
process (code is below), but it always ends up executing in my original app
domain.
Here's the code to instantiate the class "Service1" in a new app domain:
Dim AD1 As AppDomain = AppDomain.CreateDomain("Service1Domain")
Dim obj As Object =
AD1.CreateInstanceFromAndUnwrap(System.IO.Directory.GetCurrentDirectory &
"\Service1.dll", "Service1.Service1")
obj.GetType().InvokeMember("Initialize", BindingFlags.InvokeMethod, Nothing,
obj, Nothing)
Here's the code for the "Service1" class:
Public Class Service1
Inherits MarshalByRefObject
Shared Sub Initialize()
Console.WriteLine("Service1 initialized.")
Console.WriteLine("Service1 App Domain: " &
AppDomain.CurrentDomain.FriendlyName)
Console.WriteLine("Service1 Executing Thread: " &
AppDomain.CurrentDomain.GetCurrentThreadId)
End Sub
End Class
However, I end up executing in the new app domain if I use the
ExecuteAssembly method instead:
AD1.ExecuteAssembly(System.IO.Directory.GetCurrentDirectory &
"\TestExecutable.exe")
Any ideas of what I'm doing wrong?
Thanks!
Web Developer - 24 Jan 2006 22:10 GMT
I figured this out. I needed to provide binding flags on the CreateInstance
call:
Dim flags As BindingFlags = BindingFlags.Public Or BindingFlags.Instance Or
BindingFlags.CreateInstance
> I'm trying to create an instance of a class in a new app domain within my
> process (code is below), but it always ends up executing in my original app
[quoted text clipped - 29 lines]
>
> Thanks!