Wpf / winforms C#和ASP.NET使用相同的DLL(我需要在Aspx ref Session [" Variable"]中) [英] Wpf/winforms C# and ASP.NET with the same DLL (I need in Aspx ref Session["Variable"])

查看:49
本文介绍了Wpf / winforms C#和ASP.NET使用相同的DLL(我需要在Aspx ref Session [" Variable"]中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个必须在Wpf / Winforms c#和asp.net c#中工作的程序,调用这两种技术共有的dll。



当我需要一般变量的结构(也包含每个用户的信息)时,dll中存在的函数通过引用返回它,以便在整个程序中变量的任何变化都可用。现在效果很好。
但是,在Asp.net中,我需要相同的函数来通过引用或值返回结构,并使用每个用户的变量保存会话。如果在WPF / Winforms中通过引用返回,则在Asp.net中,所有用户都将共享相同的值。



命名空间Name_Comun

{  ;  public struct St_Comun

    {   public int User;

       公共字符串名称;

    }


  公共课Cls_Comun

   {   //一般变量的结构

       public static Name_Comun.St_Comun StComun;



       public Cls_Comun()

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

      }¥b $ b   }
}



public void Anyfunction()

{   // (现在)。通过引用检索具有一般变量的结构。

    ref Name_Comun.St_Comun StComun = ref Name_Comun.Cls_Comun.Fcn_StComunGet();



   //更改变量。该变量通过引用保存。

   StComun.User = 100;



   //保存更改。 (在Wpf / Winforms中不保存任何内容,因为它是一个引用)。在Aspx中保存会话["StComun"]。

   Name_ComunAdd.Cls_ComunAdd.Fcn_StComunSet(ref StComun);

}



//返回带有常规变量的结构的函数。

public static ref Fcn_StComunGet()

{   //返回静态变量的地址。

    ref Name_Comun.St_Comun StComun = ref Name_Comun.Cls_Comun.StComun;



    //(此时不可能)我需要返回会议的参考或价值。例如:return ref Session [" StComun"]或return session [" StComun"]

   if(Aplication == Aspx)StComun = ref Session [" StComun"];

  返回StComun;

}



public static void Fcn_StComunSet(ref Name_Comun.St_Comun StComun)

{if( Aplication!= Aspx)返回;

  if(Aplication == Aspx)Session [" StComun"] = StComun;

}



我解释的是函数返回一般变量的结构,因为它可以修改变量,并且可以访问程序其余部分的变化。但是如果它是Aspx,考虑到结构的Session必须是
用于每个用户,我不知道如何为同一个函数返回它,因为该函数我使用它数百个所有功能的时间。



我需要:

我需要它,如果它是Aspx,函数public static function ref Fcn_StComunGet()返回类似于返回ref会话[" StComun"]。



如果您返回会话[" StComun],它也有帮助,但请记住如果程序不是Aspx,函数返回ref而不是value。



如果不可能返回ref Session [" Variable"]那么我需要函数返回Session [" Variable"],但此函数也用于WPF / Winforms,现在返回ref。通过这种方式,在我修改变量的那一刻,它是
可以不保存。
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.

推荐答案

你好zequion1,

Hi zequion1,

如果你想在库类中使用session,我们需要添加一个对System.Web的引用,因为它在类库项目中默认不存在。和这样的用法:

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";




祝你好运,

张龙


这篇关于Wpf / winforms C#和ASP.NET使用相同的DLL(我需要在Aspx ref Session [" Variable"]中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆