简体   繁体   中英

WCF Memory Performance InstanceContextMode

I've been learning my way around WCF and I've got a question regarding the InstanceContextMode.

Correct me if I'm wrong, but the WCF will instantiate your object, then call the service method on it per call by default.

You can then set it to be PerSession or Single.

It seems to me that it would make more sense to have something between session and call which could reuse your object. Ie if my service were reentrant, then I could open connections to databases etc. in the constructor, then leave the object in memory to answer calls concurrently.

But in the current WCF implementation, it seems that it will always recreate the object no matter what.

The samples here

http://msdn.microsoft.com/en-us/library/aa967565.aspx

show how to use extensibility to exercise more fine-grained control over how instances are created and destroyed (eg pooling).

Even while using PerCall instancemode, you can still re-use heavy data structures if those are static. Remember, static objects will have the lifetime of the appdomain so lets say if you initialized a static object (which required an expensive operation), it will be visible to other WCF per call instances as well and will remain alive until the appdomain is destroyed (the lifetime of appdomain depends upon your server settings).

Be careful to handle the synchronization issues and also NOT to have any information in this static class which you DON'T want to share between all your per call instances.

My $0.02

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