Groups | Blog | Home
all groups > dotnet general > july 2004 >

dotnet general : Web Services Shared Resource/Memory



Grandpa Pete
7/17/2004 7:03:23 PM

How can I share resources across all users of a web service for read/write
access.

*Situation* *one*:

I want to have an in memory counter that all users of the webservice could
access. Call it g_count.

Then I create a web method called Update_g_count.

public int g_count( int incr )
{
g_count += incr;
return g_count;
}

So, how do I define g_count so it's global across all users?

*Situation* *two*:

I have an XML file that I want to load into an XmlDocument.

I want to set up a Web Method that uses XPath to find a node, and then
change the value of the InnerText.

How do I put this XmlDocument into memory so all users can update it without
collisions when they try to update the nodes?

Do I need to use a mutex ?

How can this mutex apply across all users of the web service ?

Do I put it in global.asax ?



--
incognito
Saurabh Nandu
7/18/2004 2:20:01 AM
Hi,

You can use the Application object to store the object accross web services.

public int g_count()
{
......
// Get the count
int gcount = (int) Context.Application["gcount"];

...// Process it

//Save it back
Context.Application["gcount"] = gcount ;

}


Although its not a recomended way to share such resources in read/write mode when specially in the case of web applications.

You can using the lock C# keyword to lock access to the object while you are modifying it.

--
Regards,
Saurabh Nandu
AksTech Solutions, reflecting your needs...
[ www.AksTech.com ]
[ www.MasterCSharp.com ]



[quoted text, click to view]
Donnie Darko
7/18/2004 6:32:50 PM

You say that this is not a 'recommended' way to share resources in web
applications.

Can you suggest a recommended way?

Specifically, I want to share access to an XmlDocument across all users of
my web service. I want them to be able to query and update nodes on the
document.


[quoted text, click to view]

--
incognito @ http://kentpsychedelic.blogspot.com/

Man is the best computer we can put aboard a spacecraft ... and the only one
Saurabh Nandu
7/18/2004 8:42:02 PM
hi,

XML is best when its used as a data exchange document, and not as a database as such. The reason i don't recomend using it as a database (shared amongst users) is that you end up writing a lot of locking code to ensure that when one user is updating the document others cannot write to it and things like that.

Now suppose 2 users tried to update at the same time, ones request will pass through other will fail since the document gets locked.

FOr multiple read/write the best source to store data is a database!! Its meant to help you d multiple read/writes without locking the users.



--
Regards,
Saurabh Nandu
AksTech Solutions, reflecting your needs...
[ www.AksTech.com ]
[ www.MasterCSharp.com ]



[quoted text, click to view]
Guy LaRouche
7/19/2004 6:12:25 AM
[quoted text, click to view]

Actually, I already created a Windows service that allows multithreaded
access to an XmlDocument for simulaneous 'users' to read/write. I use the
Mutex class. I mutex is like queueing system, it takes in requests from
multiple threads or sources and let's them in one at a time, so there is no
collision.

Xml /is/ a 'data' base.

What you are referring to as a database is a Sql relational database.
Tables in a relational database are orthogonal. Each record has all the
fields of all the others, and no more.

Xml is a hierarchical database. But, it is also 'ragged'. Not all nodes
have to have the same attributes, or child nodes.

For many uses, Xml could be idea. That is why I am interested in creating
this web service.

I also feel that many web 'database' applications need only a single table
with very complicated nodes. In that case, a relational database, such as
sql server, is the *wrong* choice for web-data applications; and Xml could
be *very* useful.

[quoted text, click to view]

--
incognito @ http://kentpsychedelic.blogspot.com/
Saurabh Nandu
7/19/2004 9:25:01 PM
Hi,

If you read my post the main reason I have stated to not go for XML as a database, is that you need to write a lot of code to get multi-threading right! Not everyone is able to write multi-threaded applications correctly...plus you loose all the other features RDBMS system provide to you like Transactions, Security, etc.

I agree with the benefits you have listed which would warrent using XML as a datasource, but for most users they tend to use it in the same format as one uses a RDBMS.

--
Regards,
Saurabh Nandu
AksTech Solutions, reflecting your needs...
[ www.AksTech.com ]
[ www.MasterCSharp.com ]



[quoted text, click to view]
AddThis Social Bookmark Button