I have a Silverlight client calling a WCF Service on an IIS web server. It uses the default basicHttpBinding setting for the calls. My client code has the usual Visual Studio generated proxy that is generated when using the 'Update Service Reference' menu option.
Does every call to the service using that proxy use the same connection? Or does it create a connection each time a call is made and then close it down once the reply is received? As the client is actually making a SOAP call over HTTP I just assumed that every service request had a new connection created but I want to check if that is the case?
(I need to know because if it creates a new connection each time then each request could end up at a different server because there are several servers being load balanced. It is uses a single connection for the duration of the proxy then I can assume they all end up at the same machine and so cache state information for better performance.)
You have to differ between connection and session. Connection allows you calling the server. Session allows you maintaining state among subsequent requests from the same client. Application session for example allows using server side caching. First of all BasicHttpBinding does not support session.
HTTP 1.1 specification describes that each connection should be opened as persistant. When you call first HTTP request to a new server, a persistant connection is established and it remains opened for subsequent calls to the same server. If you do not call the server again it is closed after some timeout. Persistant connection openning and closing is handled internally and it is fully transparent to developers.
Persistant connections are used by all browsers and HTTP APIs including .NET HttpWebRequest and so all HTTP based bindings. You can demand that new connection is created and closed for each request/response by creating custom binding with HTTP transport channel and property KeepAliveEnabled set to false. It will add additional overhead because new TCP connection will be established for each request/response. Establishing TCP connection is time consuming operation.
Persistant HTTP connection is not related to WCF application session. WCF session is by default handled between single service proxy instance and single service instance. All subsequent calls from the same proxy instance are handled by the same service instance (PerSession instancing). WCF application session is built on top of any other session - connection, security, reliable. BasicHttpBinding does not support any of these session types so it can't use WCF application session (and PerSession instancing). Each request for service exposed on BasicHttpBinding is by default handled by new service instance (PerCall instancing).
By HTTP specification the client should be able to open only two concurrent persistant HTTP connections to the same server. Persitant HTTP connections are shared for all service proxies calling the same server from the same client machine in the same period of time. Moreover single proxy instance can call the service from many different connections if long period of time elapses among calls. Those are reason why persistant HTTP connection can't be used as connection session. HTTP is not connection oriented - it only allows reusing connection for performance reasons.
The inactivity timeout of persistant HTTP connection in WCF is 100 seconds. I have found this timeout by measuring in Procmon. I have unanswered question about setting this timeout to different value.
When you are using load balancing you can't also rely on connection. The persistant HTTP connection is opened between client and load balancer. But it is responsibility of the load balancing algoritm to select processing server. In case of BasicHttpBinding it can be simple Round Robin because processing servers will not use any kind of session. In case of session oriented binding you have to use some algoritm with session affinity (sticky sessions) which will forward all requests from the same session to the same server so the same service instance can handle them. But it is not the case of BasicHttpBinding.
来源:https://stackoverflow.com/questions/4525231/does-a-wcf-service-with-basichttpbinding-create-a-new-connection-for-each-reques