简体   繁体   中英

VB.net ASP.net class Library Project Shared (Static) variable inside public class

We have a class library project is use to hold our basic data definitions, sql functions and generic functions. This class library is used in or winform project and our asp.net project.

In one of the classes we have our generic functions setup and shared variables.

Public Class Generic
    Public Shared CurrentDeviceUniqueIdentifier As String
    Public Shared TimeDiffFromServer As Long
    Public Shared myLogger As Logger

....
end Class

And they are accessed in class library also in the winform/asp.net project.

 If Generic.myLogger IsNot Nothing Then Generic.myLogger.WriteLog(...)

These shared variables aren't causing any problems winform project since there is single instance of these variables and system doesn't have any issues.

But the asp.net project gets these shared variables confused across sessions hense they are shared.

I could convert the class to a module since system is designed in VB instead of C#. But not sure if that would resolve the issue.(it should) In case in the future we might want to convert to C# and this wouldn't work.

But I was curious is there any other way to get around this problem where shared variables to lock down into asp.net session.

Static classes and data are shared across ASP.NET sessions. Period. Changing to Module would not change the problem since _everything" in a Module is shared. If you want to have separate instances for each session then you'll have to make them "not Shared " and change your code to store instances in session:

Session("Logger") = new Logger()

There's no "hack" that I'm aware of to allow separate Shared classes by session in ASP.NET.

The short answer is no.

VB Shared variable or C# static variable are AppDomain wide (for practical purpose, you can think of that as process wide) and therefore you have a single value.

To have a different value for different ASP.Net session, you have to put it in the Session dictionary, there is no way around that.

You might also want to look at threading issues. In general, ASP.Net processes request concurrently, and if you have shared variables (even in the same session), that might be accessed concurrently and your code need to be thread safe.

For now I needed a simple and fast solution inside the class library. Converted the class to module. Removed all shared (static) Codes from all variables and functions. As of now it all works as in functionality and compiling. I am hoping the data crossing over should be resolved too. I will keep an eye on the logs to see if it happens again but I doubt it will.

I still prefer another way of doing this where it works in winforms and asp.net so we can convert to C# in the future.

For reference changed version of the code:

Public Module Generic
    Public CurrentDeviceUniqueIdentifier As String
    Public TimeDiffFromServer As Long
    Public myLogger As Logger

....
end Class 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM