473,729 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a static method in another AppDomain

Hi

I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have the same class/static method definition statictly linked to my EXE and when I call InvokeMember(.. .), even though I got the Type from the new AppDomain, it calls the static method that I am staticly linked to and not the static method in the dynamicly loaded DLL

Does anybody know how to force a static variable call into another AppDomain if there is duplicate definition clashes across domains

I'll appreciate any help. Thank you
Chris
Jul 21 '05 #1
5 15365
Chris,

By domains do you mean namespaces? If you have two seperate namespaces, one
which is your library's, and the other is your programs, then reference your
built in static method like this

this.MyStaticMe thod(...);

but then reference the one in the other namespace like this:

MyDynamicDomain .MyClass.MyStat icMethod(...);
If I've missed the boat, let me know.

Thanks.
Dan.
"Chris" <cp******@hotma il.com> wrote in message
news:FD******** *************** ***********@mic rosoft.com...
Hi,

I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a
class. The problem arise is that I have the same class/static method
definition statictly linked to my EXE and when I call InvokeMember(.. .),
even though I got the Type from the new AppDomain, it calls the static
method that I am staticly linked to and not the static method in the
dynamicly loaded DLL.
Does anybody know how to force a static variable call into another AppDomain if there is duplicate definition clashes across domains?
I'll appreciate any help. Thank you.
Chris

Jul 21 '05 #2
I don't have the original post to reply to, but this is to address the
original post.

Have you tried using the Type.GetMethod( ) and then calling Invoke() on the
MethodInfo object instead? My guess is that theres some confusion, but at
least doing it this way, you can verify the MemberInfo (an ancestor of
MethodInfo) to be sure that it's on the correct type.

My guess is your Type.InvokeMemb er() is getting confused somewhere along the
way.

I had a similar problem a while back and by using the method I outlined, I
was able to track down the original problem with my Type.InvokeMemb er()
call.

Pete

"Daniel Bass" <DanielBass TAKE at OUT CAPS WORDS Postmaster.co.u k> wrote in
message news:%2******** *********@TK2MS FTNGP10.phx.gbl ...
Chris,

By domains do you mean namespaces? If you have two seperate namespaces, one which is your library's, and the other is your programs, then reference your built in static method like this

this.MyStaticMe thod(...);

but then reference the one in the other namespace like this:

MyDynamicDomain .MyClass.MyStat icMethod(...);
If I've missed the boat, let me know.

Thanks.
Dan.
"Chris" <cp******@hotma il.com> wrote in message
news:FD******** *************** ***********@mic rosoft.com...
Hi,

I have a scenario where I've created another AppDomain to dynamically
load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have the same class/static method
definition statictly linked to my EXE and when I call InvokeMember(.. .),
even though I got the Type from the new AppDomain, it calls the static
method that I am staticly linked to and not the static method in the
dynamicly loaded DLL.

Does anybody know how to force a static variable call into another

AppDomain if there is duplicate definition clashes across domains?

I'll appreciate any help. Thank you.
Chris


Jul 21 '05 #3
The problem is that even though you are creating a new appdomain you are
still calling it from the context of the default appdomain. You need to
create an object derived from MarshalByRefObj , create an instance of it in
the new appdomain and then via remoting into the new appdomain call a method
in it that will create the object and then call its method. This link does a
good job of describing things, and you can google up a bunch of sample code
pretty easily.

http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx
"Chris Mouton" <an*******@disc ussions.microso ft.com> wrote in message
news:5B******** *************** ***********@mic rosoft.com...

Hi Dan,

Thank you for replying. No when I mean domain, I mean AppDomain. Here is the sample code of the DLL that I am loading and the method that I'm
calling: AppDomainSetup setup = new AppDomainSetup( );
setup.Applicati onBase = someDirectory;
AppDomain appDomain = AppDomain.Creat eDomain("Sample Domain", null, setup);

Common.SampleIn terface convertedClass = (Common.SampleI nterface)
appDomain.Creat eInstanceAndUnw rap(
"SampleDLL" ,
"SampleDLL.Samp leDLLClass");

Assembly[] assemblies = appDomain.GetAs semblies();
Type type = assemblies[1].GetType("Sampl eNamespace.Samp leClass", true, false); type.InvokeMemb er("Initialize "
,BindingFlags.P ublic | BindingFlags.In vokeMethod | BindingFlags.St atic
,null
,null
,new Object[] {});

When this code is executed, the Initialize on my SampleClass from staticly linked DLL is called. I know this because I can have check code in the
SampleClass that says that it has not been initialized and if I look at the
Output window when in debugging mode, I can view that the wrong assemblies
are being loaded.
Does this help clarify the problem?

Kind Regards
Chris Mouton

----- Daniel Bass wrote: -----

Chris,

By domains do you mean namespaces? If you have two seperate namespaces, one which is your library's, and the other is your programs, then reference your built in static method like this

this.MyStaticMe thod(...);

but then reference the one in the other namespace like this:

MyDynamicDomain .MyClass.MyStat icMethod(...);
If I've missed the boat, let me know.

Thanks.
Dan.
"Chris" <cp******@hotma il.com> wrote in message
news:FD******** *************** ***********@mic rosoft.com...
> Hi,
>> I have a scenario where I've created another AppDomain to
dynamically load
a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have the same class/static method
definition statictly linked to my EXE and when I call InvokeMember(.. .), even though I got the Type from the new AppDomain, it calls the static method that I am staticly linked to and not the static method in the
dynamicly loaded DLL. >> Does anybody know how to force a static variable call into another AppDomain if there is duplicate definition clashes across domains? >> I'll appreciate any help. Thank you.

> Chris


Jul 21 '05 #4

Hi David

Thank you for the link and although I've been through, I reread it to see if I missed anything the first time. What you describing is completely correct and it is exactly what I've done. Over the weekend I've written a small application and dlls to simulate the situation. I tried to incorporate all the suggestions that everybody has given me (thanks all!) but I can't seem to be able to call the static method in the other domain. First I thought it was a name clash, ie both AppDomain (current and loaded) has the same signature and the static method in the current AppDomain is callled only, but I found that I couldn't even call the static method in the loaded AppDomain under a new name

If anybody has any further suggestions on how to call a static method in another (dynamicly loaded) AppDomain, please let me know. Pretty please...

Thank yo
Chris Mouto

----- David Levine wrote: ----

The problem is that even though you are creating a new appdomain you ar
still calling it from the context of the default appdomain. You need t
create an object derived from MarshalByRefObj , create an instance of it i
the new appdomain and then via remoting into the new appdomain call a metho
in it that will create the object and then call its method. This link does
good job of describing things, and you can google up a bunch of sample cod
pretty easily

http://www.gotdotnet.com/team/clr/AppdomainFAQ.asp
"Chris Mouton" <an*******@disc ussions.microso ft.com> wrote in messag
news:5B******** *************** ***********@mic rosoft.com..
Hi Dan
Thank you for replying. No when I mean domain, I mean AppDomain. Here i the sample code of the DLL that I am loading and the method that I'
calling
AppDomainSetup setup = new AppDomainSetup( )
setup.Applicati onBase = someDirectory
AppDomain appDomain = AppDomain.Creat eDomain("Sample Domain", null, setup)
Common.SampleIn terface convertedClass = (Common.SampleI nterface appDomain.Creat eInstanceAndUnw rap
"SampleDLL"
"SampleDLL.Samp leDLLClass")
Assembly[] assemblies = appDomain.GetAs semblies()

Type type = assemblies[1].GetType("Sampl eNamespace.Samp leClass", true

false) type.InvokeMemb er("Initialize
,BindingFlags.P ublic | BindingFlags.In vokeMethod | BindingFlags.St ati
,nul
,nul
,new Object[] {})
When this code is executed, the Initialize on my SampleClass from staticl linked DLL is called. I know this because I can have check code in th
SampleClass that says that it has not been initialized and if I look at th
Output window when in debugging mode, I can view that the wrong assemblie
are being loaded Does this help clarify the problem
Kind Regard Chris Mouto
----- Daniel Bass wrote: ----
Chris
By domains do you mean namespaces? If you have two seperat

namespaces, on which is your library's, and the other is your programs, the reference you built in static method like thi
this.MyStaticMe thod(...)
but then reference the one in the other namespace like this
MyDynamicDomain .MyClass.MyStat icMethod(...)
If I've missed the boat, let me know Thanks

Dan "Chris" <cp******@hotma il.com> wrote in messag news:FD******** *************** ***********@mic rosoft.com..
Hi
I have a scenario where I've created another AppDomain t

dynamically loa a DLL(s) into. In this newly loaded DLL I want to call a stati method on class. The problem arise is that I have the same class/static metho
definition statictly linked to my EXE and when I cal InvokeMember(.. .) even though I got the Type from the new AppDomain, it calls th stati method that I am staticly linked to and not the static method in th
dynamicly loaded DLL. Does anybody know how to force a static variable call into another AppDomain if there is duplicate definition clashes across domains? I'll appreciate any help. Thank you.

Chris

Jul 21 '05 #5
I haven't tried calling a static method except indirectly when I used
ExecuteAssembly (this invokes the static Main method in the executed
assembly). Invoking an instance method was no problem at all. From your
sample code it appears that you are still not invoking the method in the
context of the remote appdomain. You should define a class, derived from
MBRO, that implements the functionality you are using directly from the
default appdomain. The code you show here...

Assembly[] assemblies = appDomain.GetAs semblies();
Type type = assemblies[1].GetType("Sampl eNamespace.Samp leClass", true,
false);
type.InvokeMemb er("Initialize "
,BindingFlags.P ublic | BindingFlags.In vokeMethod | BindingFlags.St atic
,null
,null
,new Object[] {});

....I assume you are executing from the default appdomain. By loading a type
into the default appdomain you are loading up all the assemblies as well
into the default appdomain, and it will try to execute the method in the
default appdomain. Create a method in convertedClass and move this code
into it so that when it call the method the code is executing remotely, and
call the method via the unwrapped object handle.

This wont necessarily solve the apparent problem of not calling the static
method. What error do you get? Are any exceptions thrown?

You can also try to use AppDomian.DoCal lback(). This should invoke the
method in the context of the other appdomain. Again, I haven't used it with
static methods.

"Chris Mouton" <cp******@hotma il.com> wrote in message
news:3D******** *************** ***********@mic rosoft.com...

Hi David,

Thank you for the link and although I've been through, I reread it to see if I missed anything the first time. What you describing is completely
correct and it is exactly what I've done. Over the weekend I've written a
small application and dlls to simulate the situation. I tried to incorporate
all the suggestions that everybody has given me (thanks all!) but I can't
seem to be able to call the static method in the other domain. First I
thought it was a name clash, ie both AppDomain (current and loaded) has the
same signature and the static method in the current AppDomain is callled
only, but I found that I couldn't even call the static method in the loaded
AppDomain under a new name.
If anybody has any further suggestions on how to call a static method in another (dynamicly loaded) AppDomain, please let me know. Pretty please....
Thank you
Chris Mouton

----- David Levine wrote: -----

The problem is that even though you are creating a new appdomain you are still calling it from the context of the default appdomain. You need to create an object derived from MarshalByRefObj , create an instance of it in the new appdomain and then via remoting into the new appdomain call a method in it that will create the object and then call its method. This link does a good job of describing things, and you can google up a bunch of sample code pretty easily.

http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx
"Chris Mouton" <an*******@disc ussions.microso ft.com> wrote in message
news:5B******** *************** ***********@mic rosoft.com...
>> Hi Dan,
>> Thank you for replying. No when I mean domain, I mean AppDomain. Here is
the sample code of the DLL that I am loading and the method that I'm
calling:
> AppDomainSetup setup = new AppDomainSetup( );
> setup.Applicati onBase = someDirectory;
> AppDomain appDomain = AppDomain.Creat eDomain("Sample Domain", null, setup); >> Common.SampleIn terface convertedClass = (Common.SampleI nterface) > appDomain.Creat eInstanceAndUnw rap(
> "SampleDLL" ,
> "SampleDLL.Samp leDLLClass");
>> Assembly[] assemblies = appDomain.GetAs semblies();

> Type type = assemblies[1].GetType("Sampl eNamespace.Samp leClass", true, false);
> type.InvokeMemb er("Initialize "
> ,BindingFlags.P ublic | BindingFlags.In vokeMethod |
BindingFlags.St atic > ,null
> ,null
> ,new Object[] {});
>> When this code is executed, the Initialize on my SampleClass from staticly linked DLL is called. I know this because I can have check code in the SampleClass that says that it has not been initialized and if I look at the Output window when in debugging mode, I can view that the wrong assemblies are being loaded. >> Does this help clarify the problem?
>> Kind Regards > Chris Mouton
>> ----- Daniel Bass wrote: -----
>> Chris,
>> By domains do you mean namespaces? If you have two seperate namespaces, one
> which is your library's, and the other is your programs, then

reference your
> built in static method like this
>> this.MyStaticMe thod(...);
>> but then reference the one in the other namespace like this:
>> MyDynamicDomain .MyClass.MyStat icMethod(...);
>>> If I've missed the boat, let me know.
>> Thanks.

> Dan.
>>> "Chris" <cp******@hotma il.com> wrote in message

> news:FD******** *************** ***********@mic rosoft.com...
>> Hi,
>>> I have a scenario where I've created another AppDomain to

dynamically load
> a DLL(s) into. In this newly loaded DLL I want to call a

static method on a
> class. The problem arise is that I have the same class/static
method > definition statictly linked to my EXE and when I call

InvokeMember(.. .),
> even though I got the Type from the new AppDomain, it calls the static
> method that I am staticly linked to and not the static method

in the > dynamicly loaded DLL.
>>> Does anybody know how to force a static variable call into
another > AppDomain if there is duplicate definition clashes across

domains? >>> I'll appreciate any help. Thank you.
>> Chris
>>>

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
11788
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that performs the query is contained in a delegate function that I execute via a second thread. On 1 or 2 of the 600+ servers the query hangs. I've tried to use Thread.Join() coupled with a Thread.Abort() but this does not kill the thread. Based on...
5
4867
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have the same class/static method definition statictly linked to my EXE and when I call InvokeMember(...), even though I got the Type from the new AppDomain, it calls the static method that I am staticly linked to and not the static method in the dynamicly...
4
3105
by: mnowosad | last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time an executing code within an AppDomain references a class for the the first time since the AppDomain was created/loaded, the .NET executes the assignments done in the class static variables declarations and runs the static constructor of that class. So, I expected that, as in ASP.NET web site, for a given Web Service site, the AppDomain would be initialized upon...
6
2393
by: depalau | last post by:
I'm running into some issues on maintaining a static variable across the lifetime of a web service and I was wondering if anyone could help. Background: We have developed a C#/1.1 web service running on IIS 5/6 that interfaces with a 3rd party DLL referenced by using the DLLImport attribute. Interacting with this 3rd party software through this dll requires an
2
2316
by: Dave Burns | last post by:
Hello, We have a situation where a managed C++ assembly links with native C++ dll. There is a callback mechanism which calls back into the managed code asynchronously. Since native classes cannot hold onto a managed reference, we need to have a managed static member which we access during the callback and then get into the managed code. This works great in a console or WinForm app. But in ASP.NET it doesn't. The reason is that each...
4
4530
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.