开发者

Where to set up and store application state in a WCF Service Library project?

开发者 https://www.devze.com 2022-12-17 13:08 出处:网络
I have something I want to initialise and use t开发者_Python百科hroughout 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 w

I have something I want to initialise and use t开发者_Python百科hroughout 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?


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.


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?


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


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 use Global.asax in your WCF project and use the regular Application_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 own Application_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 of Global. The first instance receives only the Application_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号