How does WCF take care of multiple client calls to the server?
Want to get a clear understanding of how WCF works. Lets say a WCF service has exposed a function A. Now the client creates 5 threads, and in each one of them calls the function A with differen开发者_运维技巧t parameters.
I assume this should happen - a new instance of function A would get created for every thread call to that function. Could some one confirm this. I have written a POC which is not doing this, its giving inconsistent results.
This depends on your service configuration via the ServiceBehavior attribute on the class implementing your service contract:
[ServiceBehavior(
InstanceContextMode = InstanceContextMode.PerSession,
ConcurrencyMode = ConcurrencyMode.Multiple)]
With the parameter InstanceContextMode
you tell WCF how you want to host your service:
- Single: One instance of your service class will be created which receives all service calls
- PerSession: For each connecting client a new instance will be created
- PerCall (default): For each call of every client a new instance will be created
The next thing is synchronisation, when one host object receives parallel operation calls. You can control the behavior with the ConcurrencyMode
parameter:
- Single (Default): WCF serialises all operations, so your service instance is executing exactly one or no operation call at a time.
- Reentrant: WCF delegates all operation calls to your service instance directly, but synchronises calls to another WCF service inside a service operation (rarely used i think).
- Multiple: WCF delegates all operation calls to your service instances directly without synchronization. You have to worry about synchronisation yourself.
The default is for a new instance of your wcf service to be created per call, this is documented e.g. here
There is no such thing like a new instance of function. It'd be rather instance of class.
You can configure how your Service should behave by changing proper meta attribute.
You can make your service implementation instantiated per call. It can also work as singleton (one and only service instance for all calls).
Here you can find information on creating singleton WCF service
And here there is a lot more about WCF services
精彩评论