Search code examples
c#asp.net.netclassasp.net-4.5

C# class property getting cached?


I have a class and am setting a UID as a class property because it is used in many methods throughout the program and I don't want to keep having to pass in a value to it. The issue I'm getting is that when I run context.Response.Write(uid) and refresh the page, I get the same value every time.

If I move createRandomString() to context.Response.Write(createRandomString()), I get a new UID every time I reload.

I'm very new to using C# and coming from a PHP background, this is a bit odd for me. I would think that setting a class property would change every load. I'm thinking it probably has something to do with when the program is compiled, the UID is permanently set which still wouldn't make sense.

Code where property is getting set:

public class Emailer : IHttpHandler {
    // Define UID
    static string uid = createRandomString();

CreateRandomString Code:

public static string createRandomString() {
    Guid g = Guid.NewGuid();
    string GuidString = Convert.ToBase64String(g.ToByteArray());
    GuidString = GuidString.Replace("=", "");
    GuidString = GuidString.Replace("+", "");

    return GuidString;
}

Solution

  • Solved it by keeping the fields static but setting the value upon load.

    Fields:

    // Define UID
    static string uid = "";
    
    // Define upload path for files
    static string uploadPath = "";
    

    Setting:

    // Define UID
    uid = createRandomString();
    uploadPath = @"c:\" + uid + @"\";