In my code I check to see if a file exists and store it in the Application object in asp.net.
Later on I access this boolean to do something in my web service.
Now I want to rewrite my web service in wcf and things are different here. The application object does not exist.
What is the best way to do this task: On web site application startup record a boolean. Pass it to m开发者_如何学Pythony wcf so it knows about it when it calls a function (without the boolean being part of the method parameters)
The last post here offers an option for sharing state between calls:
Static classes always exist for the lifetime of the application. They are useful in WCF for maintaining state because they don't get reinstantiated every time a call or new WCF connection is made.
A lot of people don't realize that WCF supports Dependency Injection (DI) patterns such as Constructor Injection without too much hassle.
Define a class that encapsulates the knowledge (the boolean) you want to know about and inject an instance of that class into your WCF service and ask it about the value (and anything else you want to know about).
If you scope the injected class as a long-lived object (usually referred to as a Singleton, but not to be confused with the Singleton design pattern) you can keep asking it about the value and you will get the same answer every time.
Among many other things, this post describes how to inject dependencies into a WCF service implementation when it doesn't have a default constructor.
The Application object in ASP.NET is mainly needed for backwards compatibility with classic ASP applications.
It is essentially a static Dictionary<string, object>
with locking semantics that are compatible with classic ASP.
You can easily replace it by storing your application-wide state in any suitable static field, providing your own locking where needed. Then you don't need to worry if you are running as an ASP.NET app, a WCF app, or something else.
精彩评论