views:

96

answers:

5

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?

+1  A: 

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.

JP Alioto
Many thanks for the link, COM+ object pooling is a very interesting option
mjustin
Do you know if I can make use of COM+ object pooling in an ASP.NET application?
mjustin
A: 

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.

codemeit
The library currently says 'Treat my like a singleton' but the web application would like to create many instances of it with different states. The basic question is how to create many instances of the library and bind a web client session to one library instance.
mjustin
A: 

You can use locking eg:

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 calulation in its own time.

There will be threshold in which this will be stable/usable.

Mark Redman
Loading of the model can take a minute, after it is complete the users need to work in an interactive way with it. Queueing / load balancing is a good idea, but in this case the switching between clients requests and their data sets would take much time.
mjustin
A: 

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.

BeowulfOF
Let's assume there are design issues like static classes or singletons which can not be resolved quick enough, so that data model instances could not be created per user.
mjustin
A: 

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.

moin
This is exactly the answer I was afraid of :) You are right about the advantages of asynchronous processing, however it is only the first initialization which needs some time, then interaction with the loaded data model is fast. The problem would start if many users have to switch between models if only one model can be loaded at a time for the whole web application, causing initializations all the time. Binding a session to a library instance, or adding multi-user capability to the library, are the options. Do you know if I can make use of COM+ object pooling in an ASP.NET application?
mjustin
You could. But I am not sure that COM+ will solve anything unless you could create a separate pool for each model because it is the model-load-time which is the real problem. If I remember correctly, COM+ pooled instances do not have any state in them so this may be impossible to implement.
moin