Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


What I have tried:

What have you tried
What have you tried
What have you tried
Posted
Updated 21-Nov-18 18:59pm
v5
Comments
F-ES Sitecore 20-Nov-18 5:20am    
Not sure I 100% understand the question, but the solution is probably to clone the object in asp.net to ensure each user gets its own copy.

Session["xyz"] = myObject.Clone();

google cloning objects in c# if you don't know how.
[no name] 20-Nov-18 5:42am    
I'm not talking about that. 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.

Hi,

I'm not quite sure if I follow you, but if you are trying to synchronize data and have the clients (Windows/Web/Services) get the updates, then you may want to rethink your approach. The Web is stateless and Sessions isn't reliable. You'd need a persistent data store such as a database to keep the data instead of relying to Sessions. There are a number of things that can cause Session state to mysteriously disappear which includes:

(1) Your Session State timeout has expired
(2) You update your web.config or other file type that causes your AppDomain to recycle
(3) Your AppPool in IIS recycles
(4) You update your site with a lot of files, and ASP.NET proactively destroys your AppDomain to recompile and preserve memory.

If I were you, I would use an API/WebService to host data instead of using a Class library/DLL. This is where you handle and manage database CRUD operations. The API/WebService will then act as your central gateway to broadcast information and have your WinForms and ASP.NET WebApps access to it to get the data.
 
Share this answer
 
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.

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

I do not want to extend more.
 
Share this answer
 
Comments
Richard Deeming 23-Nov-18 10:45am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution. DO NOT post your reply as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900