简体   繁体   中英

Per-Call / Singleton Hybrid solution for WCF

I have some components of my WCF service that I want to be shared; however I want the service to run on a per-session basis (in case one session creates an exception that breaks it for the rest of the callers)

Right now my service contract is configured like;

[ServiceBehavior(
    ConcurrencyMode = ConcurrencyMode.Multiple,
    InstanceContextMode = InstanceContextMode.Single
)]

If I have thread-safe singleton components like Loggers that i want to be shared through ALL sessions; how can I make a WCF that is NOT Single Instance in the front (per session preferably), but has back-end components that are single instance (like logging, data access, etc..)

if you are already using some dependency injection framework - let it control object lifetime for you. Register logger and database access objects with single instance lifetime scope, and resolve in your wcf service on every call - you will get the same instance each time.

If you are controlling lifetime of your objects manually - design loggers and database access objects as singletons (or create some parent singleton service), and access its instance in wcf service. Be careful to avoid service locator antipattern in this case.

WCF service itself in both cases can be configured as InstanceContextMode.PerCall or PerSession.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM