473,396 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 misunderstanding what
might be going on?
Oct 3 '06 #1
4 3013
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(static 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.comwrote in message
news:Q6**************@TK2MSFTNGXA01.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(static 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(AppDomain), 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
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...
6
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...
6
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,...
0
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...
12
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...
7
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...
1
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...
6
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.