473,796 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AppDomains and static unmanaged variables

Our application environment consists of three basic layers:

1. Third-party unmanaged DLLs that were written before the CLR was invented
and maintain a significant amount of information (including memory
management and connection pooling constructs) as static variables which were
intended to scoped to the process.

2. Managed C++ assemblies (.NET 1.1) that wrap the unmanaged DLLs as nice
neat classes with managed interfaces.

3. ASP.NET 1.1 applications that call the managed assemblies.

When running just one ASP.NET web app, everything is fine, even with
high-volume multi-threaded access. But introduce a second web app in the
same AppPool, and eventually the state of the unmanaged DLLs in #1 become
corrupted. The only explanation I can come up with is that calling the
unmanaged DLLs from multiple AppDomains is breaking the assumption that the
original developers made that static variable are global to the process.

Is there a good solution to this problem? Perhaps a way to force static
variables to be global in a process again? Or am I misunderstandin g what
might be going on?
Oct 3 '06 #1
4 3034
Hello Stephen,

Based on your description, my understanding on your problem is that you
have an ASP.NET web application which uses a managed C++ component library
that act as a wrapper of an unmanaged c++ dll. This application works well
when there is only single web application work in the Application Pool, but
get messed state management when running multiple web applications in the
same application pool(IIS), correct?

As you mentioned that the C++ unmanaged dll uses some static variables to
manage global connection pool(or other states), I think this should be the
cause of the problem. As for ASP.NET(or other .net framework/clr based
application), it is running at AppDomain based boundary, that means global
variables(stati c class members in .net applications) is only visible in its
own AppDomain, not in the entire host process(which is different from the
variable sharing scope for win32 application---process based scope).

For your scenario, in IIS6, all the ASP.NET applications (which use the
same App Pool) will be hosted in the same worker process, therefore, if
you've used any unmanaged c++ code which use global variables, they'll be
accessible by all the applications in the worker process no matter they're
in the same AppDomain or not. Therefore, I'm wondering whether it is
possible that you'll configure those applications to use separate
application pool(in IIS) if they'll access the same win32/c++ global
variables? So far this is the only means I can get to prevent the win32/
c++ global variables to be accessed by multiple applications.

Please feel free to let me know if you have any other concerns or ideas on
this.
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 3 '06 #2
Yes you have correctly restated the problem.

In my testing, resorting to the workaround you described (putting the web
apps in different AppPools, and thus different processes) does fix the
problem. In real life, however, this is not an option in a few cases. One
important case is SharePoint, where the site and the _layouts pages are
implemented as separate web apps, but (as far as I can tell) they need to be
in the same AppPool. I tried putting them in a separate AppPools and this
broke the SharePoint site.

I cannot recode the unmanaged DLL, as it was written by a third party. So
my only choice is to code some protection into my wrapper classes. For
example, is there a way to force all accesses to my wrapper class (or
wrapper assembly) to be marshaled over to a singleton AppDomain?

In the old COM days, when threading was our biggest problem, we has
apartment models to protect legacy code that code not handle
multi-threading. Is there nothing to protect legacy code that does not
understand about AppDomains?

Thanks!

"Steven Cheng[MSFT]" <st*****@online .microsoft.comw rote in message
news:Q6******** ******@TK2MSFTN GXA01.phx.gbl.. .
Hello Stephen,

Based on your description, my understanding on your problem is that you
have an ASP.NET web application which uses a managed C++ component library
that act as a wrapper of an unmanaged c++ dll. This application works well
when there is only single web application work in the Application Pool,
but
get messed state management when running multiple web applications in the
same application pool(IIS), correct?

As you mentioned that the C++ unmanaged dll uses some static variables to
manage global connection pool(or other states), I think this should be the
cause of the problem. As for ASP.NET(or other .net framework/clr based
application), it is running at AppDomain based boundary, that means global
variables(stati c class members in .net applications) is only visible in
its
own AppDomain, not in the entire host process(which is different from the
variable sharing scope for win32 application---process based scope).

For your scenario, in IIS6, all the ASP.NET applications (which use the
same App Pool) will be hosted in the same worker process, therefore, if
you've used any unmanaged c++ code which use global variables, they'll be
accessible by all the applications in the worker process no matter they're
in the same AppDomain or not. Therefore, I'm wondering whether it is
possible that you'll configure those applications to use separate
application pool(in IIS) if they'll access the same win32/c++ global
variables? So far this is the only means I can get to prevent the win32/
c++ global variables to be accessed by multiple applications.

Please feel free to let me know if you have any other concerns or ideas on
this.
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no
rights.

Oct 3 '06 #3
Thanks for your reply Stephen,

Yes, for unmanaged c++ component, the managed appdomain has no sense of the
global variables so that it can not determine whether the access is proper.
So far I haven't found any good means to marshal such unmanaged calls to be
STA like. I'll help you consult some other IIS and .NET engineers to see
whether there is any possible workaround. I'll update you as soon as
possible.
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 4 '06 #4
Hello Stephen,

After some further discussion with some other .net engineers, I'm afraid so
far we haven't any better means to adjust the behavior at .net layer. This
is because no matter how to do in our managed wrapper class, the undelrying
unmanaged dll do not have sense of .NET application boundary(AppDom ain), it
will always react to method calls from different calling managed appdomain
as the same ones and thus the problem still remains.

If you do feel this any urgent issue, I would suggest you consider contact
CSS for further assistance due to the restriction of newsgroup support.

Please feel free to let me know if there is anything else we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 9 '06 #5

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

Similar topics

4
2955
by: Mountain Bikn' Guy | last post by:
I need some advice on this. I am working on a fairly complex calculator app (C#) with lots of functions (and these functions in turn use math functions from an unmanaged C DLL). A calculation takes a lot of time (up to hours), and should run on a separate thread from the one that the GUI uses. The GUI also needs to display various properties for each function (such as parameters that can be set). It does this with property grid and other...
6
2815
by: mflanagan | last post by:
I have unmanaged C++ program that will load a managed C++ dll and then call a function in that dll. The managed C++ routine will call some C# routines. The unmanaged C++ main program will make these calls (to the managed C++ dll) many times, from many different threads. Each thread will do a LoadLibrary, call one C++ routine in the managed dll, and upon return, unload the library and exit. I'm concerned about class variables (static)...
6
4503
by: Pete Davis | last post by:
I'm confused about what precisely the limitations are on loading plugins in separate app domains. In all my previous apps that supported plugins, I've loaded them into the same domain as the app, but I've just started playing around with separate AppDomains and I'm finding that I'm not having problems where I expected I would, so maybe someone can help me understand a bit better. I've read that objects instantiated in separate AppDomains...
0
242
by: Yevgeny Menaker | last post by:
I am struggling with the following: We have legacy classes written in C++ that are compiled to static libraries. Some of these classes are singletons. In order to make these classes available to C#, we wrapped them with Managed C++. The wrapper classes works fine in most of the cases. However, if they are used in ASP.NET Web Applications a problem may arise in the following scenario: Step 1: The first Web Application starts in its own...
12
2587
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in advance, Joe
7
2126
by: Boni | last post by:
Dear all, I have a global variable in the native code. I was assuming that I create a IJW CLI wrapper class and create an instance of this class in other appdomain, then the global variables will not be shared between to. But it is apparently not the case. Any ideas how I could separate the globals, when 2 instanes of native code must run in parallel. Thanks a lot, Boni
1
2570
by: billr | last post by:
hi there, I hope that someone will be able to shed some light on little old confused me. We are developing an application which will be deployed onto a Terminal Server machine. The application will be used concurrently by multiple users. We have a static object (which as you well know is only static per AppDomain), -I think I've just figured out the answer to my question, but perhaps some confirmation wouldn't go amiss-
6
4131
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including memory management and connection pooling constructs) as static variables which were intended to scoped to the process. 2. Managed C++ assemblies (.NET 1.1) that wrap the unmanaged DLLs as nice neat classes with managed interfaces.
5
6780
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that (1) holds for unmanaged C++ code, but not for managed code. I have class library with both managed and unmanaged static variables that are not referenced by any part of the program. All the
0
9684
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
10236
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
10182
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,...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
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
5445
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...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.