Click to See Complete Forum and Search --> : Terminate implicit session w/linq?


Efitap
October 19th, 2008, 06:25 AM
Hello, experts!

I need help with a scenario:

I have a photography server that builds image paths dynamically to clients for downloading. This works as intended except for the following:

Each time a viewer wants an image preview, a new session is created on the server, terminating only one hour later. When there is lots of traffic, the sessions accumulate, and end up overwhelming the server.

I initially loaded the image uri more or less like this:


-- snip --
string xmlSourceUrl = GetImageUrl( );

XElement sourceRoot = XElement.Load( xmlSourceUrl );

string result = ( from file in sourceRoot.Descendants( "PreviewUrl" ) select sourceRoot.Value ).Single< string >( );
-- snip --


Which is bad. It looks like linq for Xml supports loading xml directly over http, but there is no way to tell it afterwards that the session should end immediately.

I tried using a WebClient object, but I cannot find a "Terminate", "Close", "Abandon" or any other function to indicate that I would like the session to end immediately after reading the stream.
Here is how it looks:

// Create a webclient object to read with
WebClient wc = new WebClient( );

// prepare a reader object
TextReader reader = new StreamReader( wc.OpenRead( completeUrl ) );

/// To XElement using the reader
XElement result = XElement.Load( reader , LoadOptions.None );

// Close the reader
reader.Close( );

// client goes out of scope here - does session terminate?
return result;


Can anyone help please?

Mutant_Fruit
October 19th, 2008, 01:05 PM
I tried using a WebClient object, but I cannot find a "Terminate", "Close", "Abandon" or any other function to indicate that I would like the session to end immediately after reading the stream.

You're looking for WebClient.Dispose ();

Once you add that dispose, it looks like everything should fine. If there are memory problems, it's from other parts of your code.

Efitap
October 20th, 2008, 06:44 AM
Thank you for your reply. Do you mean to say that manually calling the Dispose object on the WebClient object will force it to end the session on the web server that it is connected to?