C# 我可以在外部 DLL 中使用 ASP.NET Session[] 变量吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/454187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:59:31  来源:igfitidea点击:

Can I use ASP.NET Session[] variable in an external DLL

c#asp.netsession-statesession-variables

提问by Jon

I have two projects, the DLL project which has all my logic and data access stuff, and the ASP.NET project which does my forms etc.

我有两个项目,一个是包含我所有逻辑和数据访问内容的 DLL 项目,另一个是包含我的表单等的 ASP.NET 项目。

I am a bit confused. I thought if I added the System.Web namespace reference to the DLL project I would be able to reference the Session state information of the ASP.NET page.

我有点困惑。我想如果我将 System.Web 命名空间引用添加到 DLL 项目中,我将能够引用 ASP.NET 页面的会话状态信息。

I could use each page to get the session info out and pass it to the DLL for the processing but would love to be able to process things directly from the DLL class(s).

我可以使用每个页面来获取会话信息并将其传递给 DLL 进行处理,但希望能够直接从 DLL 类中处理内容。

Is this possible?

这可能吗?

I have fiddled with the System.Web namespace and just seem able to get a reference to the Session variable.

我摆弄了 System.Web 命名空间,似乎只能获得对 Session 变量的引用。

Thanks all.

谢谢大家。

Jon

乔恩

采纳答案by JoshBerke

You should be able to use HttpContext.Current.Session

你应该能够使用 HttpContext.Current.Session

Edit

编辑

While yes I agree you should not tightly couple your Business Logic DAL or etc assemblies to ASP.Net session. There are plenty of valid cases for accessing HTTP Context outside of a web project.

虽然是的,但我同意您不应该将您的业务逻辑 DAL 或其他程序集与 ASP.Net 会话紧密耦合。在 Web 项目之外访问 HTTP 上下文有很多有效的案例。

Web Controls is probably one of the best examples, reusable HTTP modules, etc..

Web Controls 可能是最好的例子之一,可重用的 HTTP 模块等。

Now one option, if you want to have your DLL pull the stuff from Session, is to abstract out session. So you could define an interface like IStorage, that your library will know how to use. Then you can have a SessionStorage or MemoryStorage class and use IoC to inject the appropriate class into your library classes. This gives you the freedom to code it how you wanted it to be coded without tying your code to Session. Oh and one other benefit if done properly can be used to not tie your code to the session on the web either.

现在一种选择,如果你想让你的 DLL 从 Session 中提取东西,就是抽象出 session。所以你可以定义一个像 IStorage 这样的接口,你的库会知道如何使用它。然后,您可以拥有一个 SessionStorage 或 MemoryStorage 类,并使用 IoC 将适当的类注入到您的库类中。这使您可以自由地按照您希望的方式对其进行编码,而无需将您的代码绑定到 Session。哦,如果做得好,另一个好处可用于不将您的代码绑定到网络上的会话。

回答by Tom Anderson

As long as the Assembly is loaded in the scope of the Session, it will have access.

只要在 Session 范围内加载了程序集,它就可以访问。

Although this type of tight coupling isn't really recommended.

虽然这种类型的紧耦合并不是真正推荐的。

回答by Darin Dimitrov

You can always use HttpContext.Current.Session in your DLL but this is considered as bad practice. A better approach would be to pass the values stored in the session dictionary to your DLL instead of it referencing the session. Another benefit you will gain is that the code in your DLL won't be coupled to the ASP.NET runtime meaning it will be easier to test.

您始终可以在 DLL 中使用 HttpContext.Current.Session ,但这被认为是不好的做法。更好的方法是将存储在会话字典中的值传递给您的 DLL,而不是它引用会话。您将获得的另一个好处是 DLL 中的代码不会耦合到 ASP.NET 运行时,这意味着它会更容易测试。

回答by user56305

As said by the others, you can always use HttpContext.Current.Session in your DLL, I assume it's your BAL, but you need to be really carefull. What if your DLL is later consumed by a windows service, or some other app that doesn't have an HTTPContext? Whenever I've done this it's always been in a property get method where I wrap the attempt to access HttpContext.Current.Session in a try catch block and if anything goes wrong I repull the needed data from the db.

正如其他人所说,您始终可以在 DLL 中使用 HttpContext.Current.Session,我认为这是您的 BAL,但您需要非常小心。如果您的 DLL 稍后被 Windows 服务或其他没有 HTTPContext 的应用程序使用怎么办?每当我这样做时,它总是在属性 get 方法中,我将尝试访问 HttpContext.Current.Session 的尝试包装在 try catch 块中,如果出现任何问题,我会从数据库中撤回所需的数据。

回答by Abhijit Maity

Do not use HttpContext.Current.Session as your dll will not run always with the Web Application. It may run with any other application like Windows,Console itc.

不要使用 HttpContext.Current.Session,因为您的 dll 不会始终与 Web 应用程序一起运行。它可以与任何其他应用程序一起运行,例如 Windows、Console itc。

It is better to use a Method which is actully accept a parameter, which will come form Session Value, if you are using ASP.Net Application, otherwise there will not be any dependency of the application. If your dll project already developed and you are trying to modify the exsiting business logic then no, dont modify your exsiting method, use an Overload method.

如果您使用的是 ASP.Net 应用程序,最好使用一个实际上接受一个参数的方法,该参数将来自会话值,否则将不会有应用程序的任何依赖项。如果您的 dll 项目已经开发并且您正在尝试修改现有的业务逻辑,那么不,不要修改您的现有方法,使用重载方法。