Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

StreamWriter invalidates web service cache: bug?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BLUE - 14 Jun 2007 11:10 GMT
I insert a string in cache in a property set and I retrieve that string in
the get.

I retrieve my string with Read web method and I insert it with Write web
method: to try cache I do Read, Write, Read.

When I do the second Read, string is null if in the property set,
before/after inserting data into cache I write this code:

using (StreamWriter streamWriter = new
StreamWriter(@"D:\Log.txt"))
{
       streamWriter.Write(cacheValue);
       streamWriter.Flush();
}

Debuggin the Write, in the watch windows I see the correct just inserted
value of cache.

The same behaviour persists if I write "Hello world" instead of cacheValue:
this deny to use cache in a web service where you need to write to a file!!!

It seems to be a bug since this behaviour is a nonsense!

Thanks,
Luigi.
Mr. Arnold - 14 Jun 2007 11:45 GMT
>I insert a string in cache in a property set and I retrieve that string in
>the get.
[quoted text clipped - 20 lines]
>
> It seems to be a bug since this behaviour is a nonsense!

Again, you should post to a dotnet.webservice NG that deals with Web service
issues, using VB, C# and C++ .NET. They have most likely been down the path
you're trying to go down.
Jon Skeet [C# MVP] - 14 Jun 2007 11:55 GMT
<snip>

> The same behaviour persists if I write "Hello world" instead of cacheValue:
> this deny to use cache in a web service where you need to write to a file!!!
>
> It seems to be a bug since this behaviour is a nonsense!

This all sounds very unlikely. Could you post a short but complete
program which demonstrates the behaviour? See http://pobox.com/~skeet/csharp/complete.html
for what I mean by that.

Jon
BLUE - 14 Jun 2007 14:43 GMT
Finally I've found the error: if I try to write a file to bin dir (my desire
was to put all there to be tidy) the cache does not retain the values.

This is the code I used.

<%@ WebService Language="C#" Class="WS.MyWS" %>

using System;
using System.IO;
using System.Web.Services;
using System.Xml;

namespace WS
{
   [WebService(Namespace="http://microsoft.com/webservices/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   public class MyWS: WebService
   {
       private DateTime LastUpdateDateTime
       {
           get
           {
               string path = this.Server.MapPath("bin\\File.txt");
               DateTime utcNow = DateTime.UtcNow;
               DateTime lastUpdateDateTime;
               string cacheValue =
(string)this.Context.Cache["lastUpdateDateTime"];
               if (cacheValue != null)
                   return XmlConvert.ToDateTime(cacheValue,
XmlDateTimeSerializationMode.Utc);

               StreamReader streamReader = null;
               try
               {
                   streamReader = new StreamReader(path);
                   string s = streamReader.ReadToEnd().Trim();
                   // I want to see if cache value is taken so I comment
the following line
                   // lastUpdateDateTime = XmlConvert.ToDateTime(s,
XmlDateTimeSerializationMode.Utc);
                   lastUpdateDateTime = utcNow.AddYears(-7);
               }
               catch
               {
                   File.Delete(path);
                   lastUpdateDateTime = utcNow.AddYears(-7);
               }
               finally
               {
                   if (streamReader != null)
                       streamReader.Close();
               }

               return lastUpdateDateTime;
           }
           set
           {
               try
               {
                   string cacheValue = XmlConvert.ToString(value,
XmlDateTimeSerializationMode.Utc);
                   this.Context.Cache["lastUpdateDateTime"] = cacheValue;

                   string path = this.Server.MapPath("bin\\File.txt");
                   using (StreamWriter streamWriter = new
StreamWriter(path))
                   {
                       streamWriter.Write(cacheValue);
                       streamWriter.Flush();
                   }
               }
               catch
               {
               }
           }
       }

        [WebMethod]
       public string Read()
       {
           DateTime dt = this.LastUpdateDateTime;
           return XmlConvert.ToString(dt,
XmlDateTimeSerializationMode.Utc);
       }

       [WebMethod]
       public void Write()
       {
           this.LastUpdateDateTime = DateTime.UtcNow;
       }
   }
}
Jon Skeet [C# MVP] - 14 Jun 2007 15:00 GMT
> Finally I've found the error: if I try to write a file to bin dir (my desire
> was to put all there to be tidy) the cache does not retain the values.

Right. I suspect ASP.NET thinks it's a change to your application, and
is recycling it (restarting the web app, basically).

Writing to the bin directory sounds like a bad idea anyway though.

Jon
Mr. Arnold - 14 Jun 2007 16:11 GMT
> Finally I've found the error: if I try to write a file to bin dir (my
> desire was to put all there to be tidy) the cache does not retain the
> values.

One shouldn't write to the Bin directory or to wwwroot/appvirtual. It's a
security risk that a hacker can exploit. One reads a Web.config,  App.config
or even a text.fle that has pathing pointing somewhere instead of anywhere
but the Web server itself on wwwroot.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.