Wpf/winforms C# and ASP.NET with the same DLL (I need in Aspx ref Session["Variable"]) RRS feed

  • Question

  • 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.

    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.

    • Edited by zequion1 Tuesday, November 20, 2018 12:51 PM
    Tuesday, November 20, 2018 9:43 AM

All replies

  • Hi zequion1,

    If you want to use session in library class, we need to add a reference to System.Web as it does not be there by default in a Class Library project. and the usage like this:

    HttpContext httpContext = HttpContext.Current;         
    httpContext.Session["Test"] = "SessionValue";

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 21, 2018 3:19 AM
  • I need a static global variable for each user to be able to modify values by reference and Session ("User") is not.
    Wednesday, November 28, 2018 8:04 AM