Questions about WCF proxy sessions

152 Views Asked by At

You know how you add a service reference to your client and then it creates this nice proxy so you could invoke methods on server side? Something like this:

MyServiceClient proxy = new MyServiceClient();
proxy.GetMessage();

I've got a couple of questions about what just happened:

  1. When is the session with the server established? When I added the service reference? When I created the proxy? Or when I invoked the method?

  2. If I will create another MyServiceClient instance will it be a singleton copy of the instance we created before(proxy) or it's a new instance entirely?

3

There are 3 best solutions below

2
On

There is no 'session' by default. A new connection is made when you make a service call and lasts only until the response is received. If you want to group multiple calls (to ensure they use the same instance and in turn enforce processing in sequence) you need to use the SessionMode property on your service contract, which will consequently affect how the proxy client behaves.

0
On

Session is established when you invoke the method. This is optional - the default in WCF is that there is no session. In general if you create a new proxy then it is totally separate from the previous proxy. There are some exceptions:

  • You can force your proxies to use the same session (for example if you give them a common client security behavior that shares tokens)
  • The server can decide it treats all requests as belonging to the same session (PerHost instancing) in which case all requests (from all proxies, not just those on your machine) will land on the same server context.
0
On