Reputation: 36654
For a new web application project using .NET, an existing library (written in C#) will be made available online which performs some calculations on information in a data model.
The library needs to be accessed by many users at the same time. Every user will work with a different set of data. Users also can modify model data and repeat the calculation with modified input parameters.
Currently, the library can only handle one data model at a time. (Let's assume there are design issues like static classes and singletons).
If a .NET web application (a simple web frontend using HTML web pages) wants to use this library, is there a technology available to create a separate instance of the library for every client, to keep the models and other parameters separated?
Upvotes: 1
Views: 264
Reputation: 28174
You could load the library in question into separate AppDomains. That way you can have multiple singleton instances, each isolated in an AppDomain.
Jeffrey Richter "Using AppDomains to make Non-Threadsafe code Threadsafe"
Upvotes: 1
Reputation: 24525
You can use locking, e.g.:
lock (typeof(YOUR_MODEL_CLASS))
{
}
or try and queue calculations, save the parameters to a database file and have a service run through and do each calculation in its own time.
There will be a threshold in which this will be stable/usable.
Upvotes: 0
Reputation: 45127
You could load the module externally and create a new instance every time or use object pooling by use of .NET Enterprise Services (COM+), but if the object is stateful, you are still going to have scalability problems.
Per comment: Yes, you can. Check out this example, for a quickstart.
Upvotes: 1
Reputation: 1149
If you are talking about a component that is not set up to be safe in a multithreaded environment then the answer is yes. However, you must select a mechanism that 'synchronizes' access to the component. The simplest form in C# being the "lock" syntax.
If you are talking about a component that allows a single instance at a time only then the answer is no. This is because web applications are inherently multithreaded. Using it in asp.net will require you to lock the entire server to wait until the component finishes which is usually a bad thing. My best solution to this is to make the use of such a component an asynchronous operation. Allow the user to specify the input parameters, then save this data somewhere and use a scheduler of some kind to process the data one at a time. When the process finishes, notify the user that the work is done etc.
Also, when the component takes a lot of time to process it's better to not let the user interact with the component synchronously anyway.
Upvotes: 0
Reputation: 9240
Most classes of the .NET Framework are not made for multi threaded access, as you can see in the MSDN.
If you say, your Users work with different sets data, so your program needs to create instances of the classes in the library for every user - that sounds unproblematic to me.
Critical would it be, if many users would work on the same set of data, then you would have to care about multi-threading ability of the library.
Upvotes: 0
Reputation: 26668
If you'd like users share a singleton object within your asp.net application instance, it needs to be implemented with thread-safety for any kind of the accesses and modifications.
You can create a wrapper for using and managing the instances of the library.
Upvotes: 0