locked
I need ref Session ["Variable"] RRS feed

  • Question

  • User1557998713 posted

    I'm doing a program that has to work in Wpf/Winforms c# and in asp.net c# calling a dll that is common to both technologies.

    When I need the structure of general variables, which also contains the information of each user, a function that exists in the dll returns it by reference so that any change in a variable is available at the moment throughout the program. This now works well. But, in Asp.net i need the same function to return the structure by reference or by value, with the session saved with the variables of each user. If returned by reference like in WPF/Winforms, in Asp.net all users would share the same values.

    namespace Name_Comun
    {   public struct St_Comun
        {   public int User;
            public string Name;
        }

       public class Cls_Comun
       {   // Structure of general variables
           public static Name_Comun.St_Comun StComun;

           public Cls_Comun()
          {  Name_Comun.Cls_Comun.StComun = new Name_Comun.St_Comun();
          }
       }
    }

    public void Anyfunction()
    {   // (At present). Retrieves by reference the structure with the general variables.
        ref Name_Comun.St_Comun StComun = ref Name_Comun.Cls_Comun.Fcn_StComunGet();

       // Change a variable. The variable is saved by reference.
       StComun.User = 100;

       // Save the changes. (In Wpf/Winforms not save anything because it is a reference). In Aspx save the Session["StComun"].
       Name_ComunAdd.Cls_ComunAdd.Fcn_StComunSet (ref StComun);
    }

    // Function that returns the structure with the general variables.
    public static ref Fcn_StComunGet()
    {   // Returns the address of the static variable.
        ref Name_Comun.St_Comun StComun = ref Name_Comun.Cls_Comun.StComun;

        // (It is not possible at this time). I NEED to return the Session for ref or value. Like: return ref Session["StComun"] or return Session["StComun"]
       if (Aplication == Aspx) StComun = ref Session["StComun"];
       return StComun;
    }

    public static void Fcn_StComunSet(ref Name_Comun.St_Comun StComun)
    { if (Aplication != Aspx) return;
      if (Aplication == Aspx) Session["StComun"] = StComun;
    }

    What I explain is that a function returns the structure of general variables as ref to be able to modify variables and that the changes in the rest of the program are accessible. But if it is Aspx, taking into account that a Session of the structure must be used for each user, I do not know how to do it for the same function to return it, since that function I use it hundreds of times in all my functions.

    I NEED:
    I need that if it is Aspx, the function public static function ref Fcn_StComunGet() returns something similar to return ref Session ["StComun"].

    It also helps if you return return Session ["StComun"], but keep in mind that the function returns ref, not value, in case the program is not Aspx.

    Tuesday, November 20, 2018 12:25 PM

All replies

  • User475983607 posted

    As I understand your question, it's simply not possible.  ASP Session (InProc) is stored in the web server's memory.  A Session cookie is used to lookup the user's Session on the web server during an HTTP request.

    Look into storing the shared values in a database.

    Tuesday, November 20, 2018 12:37 PM
  • User1557998713 posted

    I can not use a database just for this reason and it would also slow down the entire program, since each function retrieves the structure of general variables.

    If is not possible return ref Session["Variable"] then I need the function to return Session["Variable"], but this function is also used in WPF/Winforms and is now returning ref. In this way, at the moment that I modify a variable, it is available without saving.

    Tuesday, November 20, 2018 12:46 PM
  • User753101303 posted

    Hi,

    Not 100% to grasp your design. Do you have to expose a structure rather than a class which might be a part of your problem (an object istance is a reference).

    Also it seems you are trying to share a bit more than needed ie in addition to the class (or struct) definition itself, it seems you are trying to share its storage implementation (using a static variable which will likely never work in ASP.NET) as well ?

    For now my understanding is that you are trying to standardize what is a "user account" accross all your applications ?

    BTW using  a db doesn't mean you have a bad performance. You could still use the session as a "cache".

    Tuesday, November 20, 2018 1:03 PM
  • User1557998713 posted

    In the structure I have all the information I need and also the user information. I get that structure in each function with ref Fcn_StComunGet() and if I make any changes, it is saved at the end with Fcn_StComunSet(). I need ref to be the return because sometimes, before finishing, I call other functions that in turn recover the structure and save it.

    I need that ref Fcn_StComunGet() can return the Session if it is aspx, preferably in ref mode, but it would serve me for value, if it is with the same function.. The Session object comes from somewhere. Does anyone know how to do ref to that place? Now I assign the Session Variable to a global static, but that means that in Aspx all the users have the same variables.

    When you say to use the session as a cache, how do you do it?

    Tuesday, November 20, 2018 1:19 PM
  • User475983607 posted

    InProc Session is stored in server memory with a limited life time.   There is no guarantee that Session will exists at any point in time due to many factors.  The common Session (or Cache) programming pattern in ASP is to always check if Session is null before trying to access Session.  If Session is null, then repopulate Session usually from a DB.

    Also it is considered poor design when referencing Session in a library.  The accepted approach is passing the Session object or passing the Session values to the class or struct.

    Tuesday, November 20, 2018 2:17 PM
  • User1557998713 posted

    The problem is that a function returns the ref of a structure that contains general variables and that is a public static variable. In Aspx each user must have the same for the function to return it. If the session variable does not exist, the function creates it at that moment. But the point is that you have to return a ref to a static that contains the structure value that is in a Session of each user. If this is not possible, a hundred of functions can not assign the variables to the returned structure and I can not have a structure variable for Wpf / Winforms and another for Aspx.

    I can create an array to store each Session, but that's not technically good. Viable solutions without programming thousands of lines?

    Tuesday, November 20, 2018 2:27 PM
  • User475983607 posted

    zequion

    The problem is that a function returns the ref of a structure that contains general variables and that is a public static variable. In Aspx each user must have the same for the function to return it. If the session variable does not exist, the function creates it at that moment. But the point is that you have to return a ref to a static that contains the structure value that is in a Session of each user. If this is not possible, a hundred of functions can not assign the variables to the returned structure and I can not have a structure variable for Wpf / Winforms and another for Aspx.

    Sorry, the design simply does not make logical sense.   The main issue is a WPF application has one user per instance.  An ASP.NET Application is one application for many users.  Static references simply will not work as every application user will reference the same memory location.

    zequion

    I can create an array to store each Session, but that's not technically good. Viable solutions without programming thousands of lines?

    Session is already a static dictionary unlocked per user by using a key persisted in a cookie.  Again, simply pass Session as a parameter.

    I'm thinking that you need an Interface... Can you explain at a high level the problem you are trying to solve?  Are you simply trying to identify the user?

    Tuesday, November 20, 2018 2:56 PM
  • User1557998713 posted

    I need to return a ref of the session in the Fcn_StComunGet () function. The ref must point to a static variable. How is it done if is a static dictionary?

    Tuesday, November 20, 2018 3:03 PM
  • User753101303 posted

    Hi,

    Do you have to use a struct ? Which might be part of the problem. Also it seems you are using a static which will never work in an ASP.NET application. First what do you want to share exactly ? It seems you want to standardize user account ?  Start with something simple and move forward for example you could start by standardizing what a user is rather than how this user information is stored in each kind of application. If confirmed you could maybe staert with https://docs.microsoft.com/en-us/dotnet/api/system.security.claims.claimsidentity?view=netframework-4.7.2

    Edit: saw the question you posted since them. IMHO this is not the problem. For now the problem is that you store then this in a static property and it will never work fine in an ASP.NET application (all users would use this same static value).

    As pointed by https://forums.asp.net/members/mgebhard.aspx I believe you should first explain what you are trying to do. You want to handle a user identifier and a "callsign" ?

    Tuesday, November 20, 2018 3:10 PM
  • User1557998713 posted

    It is a large program that employs struct and I can not change the operation. The function to try to standardize the return to the address of memory of that struct, is something very logical. There is a static Array that manages the server where each session of each user is contained. I do not want to duplicate that information while maintaining another parallel array myself. Is there a static place on the server that I can use?

    Tuesday, November 20, 2018 3:52 PM
  • User753101303 posted

    So if I understood currently you are doing something such as :

    var usr=MyClass.GetValue;
    usr.Whatever=100;
    MyClass.SetValue(usr);

    in your app and you try to switch to something that could look like :

    var usr=MyClass.GetValue;
    usr.Whatever=100; // and SetValue is not needed any more ?

    It's IMO sometimes best to do the plunge and do simple code changes ie you could keep the same principle, mark some methods as obsolete and switch to an alternative that could allow to do MyClass.User.Whatever=100; ? (and at first it could be interoperable with the old approach).

    Technically speaking I doubt adding ref (or you had it already ?) is compatible with the current library anyway (a bit likelt field and property access using the same syntax but the later being actually method calls).



    Tuesday, November 20, 2018 5:42 PM
  • User1557998713 posted

    Repeat with me 100 times: "It can not be done". "It can not be done". "It can not be done". "It can not be done".
    If we continue like this, we will end our arrogance that leads us to think that it is the others who always do things badly.

    Wednesday, November 21, 2018 6:55 AM
  • User1557998713 posted
    I'm not talking about that. You have not understood the problem because you have to read it well and that takes time.
    I raise my need to use a simple function that standardizes the return of a structure in ref mode to be able to modify variables at the moment and that are available in the rest of the program. I make a call to that function almost every function of the program, in hundreds of places.
    It works well in WPF / WinForms but not in Asp.net, where there is a static dictionary that contains the information of each user's Session variables. To be able to return in ref mode, I need to be given the possibility to access that dictionary regardless of the duration of the information. If the information no longer exists, my function will create a new session.
    Microsoft does not return that ref, so you can not standardize that pointer to the information, but that only happens in asp.net
    Every time I find a precarious or serious failures in the code of Microsoft and I indicate it to him, the deaf ones become or they respond of arrogant form saying that I consider my program of another form. The serious errors that I have found do not correct them.
    I have been working on something for a long time and serious failures in Sql Server are hurting me without Microsoft taking charge and nobody does anything:
    Sql Server 2017 with Transact and synonyms Problems. Microsoft does not respond.
    <a href="https://social.msdn.microsoft.com/Forums/en-US/6e1cb5ed-60fb-4d27-af97-9d31c8691139/sql-server-2017-with-transact-and-synonyms-problems-microsoft-does-not-respond?forum=transactsql">https://social.msdn.microsoft.com/Forums/en-US/6e1cb5ed-60fb-4d27-af97-9d31c8691139/sql-server-2017-with-transact-and-synonyms-problems-microsoft-does-not-respond?forum=transactsql</a>
    
    I do not want to extend more.
    Thursday, November 22, 2018 6:02 AM
  • User475983607 posted

    I'm not talking about that. You have not understood the problem because you have to read it well and that takes time.
    I raise my need to use a simple function that standardizes the return of a structure in ref mode to be able to modify variables at the moment and that are available in the rest of the program. I make a call to that function almost every function of the program, in hundreds of places.
    It works well in WPF / WinForms but not in Asp.net, where there is a static dictionary that contains the information of each user's Session variables. To be able to return in ref mode, I need to be given the possibility to access that dictionary regardless of the duration of the information. If the information no longer exists, my function will create a new session.
    Microsoft does not return that ref, so you can not standardize that pointer to the information, but that only happens in asp.net
    Every time I find a precarious or serious failures in the code of Microsoft and I indicate it to him, the deaf ones become or they respond of arrogant form saying that I consider my program of another form. The serious errors that I have found do not correct them.
    I have been working on something for a long time and serious failures in Sql Server are hurting me without Microsoft taking charge and nobody does anything:
    Sql Server 2017 with Transact and synonyms Problems. Microsoft does not respond.
    <a href="https://social.msdn.microsoft.com/Forums/en-US/6e1cb5ed-60fb-4d27-af97-9d31c8691139/sql-server-2017-with-transact-and-synonyms-problems-microsoft-does-not-respond?forum=transactsql">https://social.msdn.microsoft.com/Forums/en-US/6e1cb5ed-60fb-4d27-af97-9d31c8691139/sql-server-2017-with-transact-and-synonyms-problems-microsoft-does-not-respond?forum=transactsql</a>

    I do not want to extend more.

    The singleton pattern stores global variables and cannot be used to store user data in ASP.NET.  This is a not a hidden concept and has little to do with ASP.NET.  This same situation exists on any platform and every web site scripting framework.   As stated above, a web application hosts many users so all the user are accessing the same memory location,  Your solution requires a Name_Comun instance of each user on the web application.  The username can be used to key the array.

    namespace Name_Comun
    {   public struct St_Comun
        {   public int User;
            public string Name;
        }
    
       public class Cls_Comun
       {   // Structure of general variables
           public static Name_Comun.St_Comun StComun;
    
           public Cls_Comun()
          {  Name_Comun.Cls_Comun.StComun = new Name_Comun.St_Comun();
          }
       }
    }

    IMHO, it would be much easier to use an OOP approach rather than a procedural programming approach.

    Thursday, November 22, 2018 1:53 PM
  • User1557998713 posted

    I talk about finding problems, it's a good time to improve the operation, but you can not if you own the software is a arrogant one who always says that you do it in another way. In this case, I will have to manage my own static array, existing somewhere in the server where the data is already found. Thank you !!!

    Thursday, November 22, 2018 2:09 PM