在 WCF 服务库项目中的何处设置和存储应用程序状态?
我有一些想要在整个 WCF 服务库中初始化和使用的东西。如果是在 ASP.NET 站点中,我会在 global.asax 的 Application_Start 方法中执行此操作,但是 WCF 服务库的等效方法是什么?
I have something I want to initialise and use throughout a WCF Service Library. If it was in an ASP.NET site I'd be doing it in the Application_Start method of the global.asax, but what's the equivalent for a WCF Service Library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用静态单例,我在保存所有应用程序状态的地方进行惰性初始化,每次这样做都感觉很恶心。
I use a static singleton that I lazy-initialize where I hold all of the app state, and I feel gross every time I do it.
您可以创建会话 WCF 组件。
请参阅我对此问题的回复: Silverlight 中的会话式 wcf 服务
您甚至可以持久会话对象,在您与它们断开连接后保留其状态(将其保存到文件或 SQL 服务器)。 Juval Lowy 的书的第四章也对此进行了一些详细介绍。
这就是您正在寻找的吗?
You can make sessionful WCF components.
Please see my response over on this question: Sessionful wcf service in Silverlight
You can even make durable sessionful objects that retain their state after you've disconnected from them (saving it to a file or sql server). Chapter 4 of Juval Lowy's book goes into some detail of that as well.
Is that kind of what you're looking for?
我现在仍处于 WCF 的学习模式,但我认为实际执行此操作的最优雅的方法是定义自己的类来保存状态并让它们实现 IExtension 接口。
请参阅这篇文章了解概述:
http://blogs .msdn.com/b/drnick/archive/2007/02/15/stashing-data-in-extensible-objects.aspx
这一个非常巧妙地使用了计时器:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/2793580f-b91a-4d4d-b98f-b7dcab70710a/wcf-periodic-methods-call-multithread
I'm still in learning mode right now with WCF, but I think the most elegant way to actually do this is to define your own class(es) that hold the state and have them implement the IExtension interface.
See this article for an overview:
http://blogs.msdn.com/b/drnick/archive/2007/02/15/stashing-data-in-extensible-objects.aspx
And this one for an very clever use involving a timer:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/2793580f-b91a-4d4d-b98f-b7dcab70710a/wcf-periodic-methods-call-multithread
我非常确定 IIS 上的 WCF 实际上是 ASP.NET 的专业化。它不会取代 ASP.NET,而是扩展了它。因此,您的 WCF 服务项目还可以包含 ASP.NET 内容 -
.aspx
和.asax
文件以及.svc
文件。因此,应该可以在 WCF 项目中直接使用 Global.asax 并使用常规的Application_Start
方法。值得记住的是,ASP.NET 应用程序在应用程序池启动时初始化,并在应用程序池停止时拆除。因此,在第一个请求到达并预热应用程序池之前,Application_Start
不会运行,然后如果应用程序池空闲足够长的时间,IIS 将处理它,并创建一个新的应用程序池。下次请求到来时创建,并获取自己的Application_Start
事件。我刚刚对此进行了显式测试,并且可以确认
Global
的事件确实运行。Application_Start
通过加载.aspx
页面和为其元数据视图加载.svc
页面来触发。奇怪的是,ASP.NET 创建了两个单独的Global
实例。第一个实例仅接收Application_Start
事件,第二个实例接收所有其他事件。如果您不使用 IIS 来托管 WCF 服务,则这些都不适用。
I'm pretty sure WCF on IIS is actually a specialization of ASP.NET. It doesn't replace ASP.NET, it extends it. So, your WCF service project can include ASP.NET content as well --
.aspx
and.asax
files alongside your.svc
files. As such, it should be possible to literally just useGlobal.asax
in your WCF project and use the regularApplication_Start
method. It is worth keeping in mind that the ASP.NET application is initialized when the Application Pool starts and torn down when the Application Pool stops. So,Application_Start
won't run until the first request arrives that warms up the app pool, and then if the app pool sits idle for long enough, IIS will dispose of it, and a new one will be created the next time a request comes in, getting its ownApplication_Start
event.I just did an explicit test of this and can confirm that
Global
's events do run.Application_Start
is triggered both by loading a.aspx
page and loading a.svc
page for its metadata view. Curiously, ASP.NET creates two separate instances ofGlobal
. The first instance receives only theApplication_Start
event, and the second instance receives all the other events.If you are not using IIS to host your WCF service, then none of this applies.