views:

1458

answers:

4

I am using VSTS2008 + C# + .Net 3.5 to develop WCF service hosted in IIS. Then I generate client proxy code automatically by using Add Service Reference function from VSTS 2008.

My question is, suppose I create a client proxy instance, then use this specific instance to call various methods exposed by WCF service at server side. Then, each time I make a method call will it establish a new connection? Or there will be a constant connection between client and server (i.e., the life time of the connection is from creation of the client proxy instance, to the disposal of the client proxy instance)?

I am using basicHttpBinding.

A: 

The connection is held until the proxy is disposed.

EDIT

It will keep the TCP connection open, atleast if you use reliable messaging. I base this on, that reliable messaging fails if the TCP connection is lost. See:

http://codeidol.com/csharp/wcf/WCF-Essentials/Reliability/

EDIT 2

I take back the comment about the using statement. See:

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

A little off topic, but we have stopped using the Add Service Reference, instead we use the method describe here:

http://www.dnrtv.com/default.aspx?showNum=103

Note: This only works if you have control over both the client and the server.

Shiraz Bhaiji
Thanks, so does it mean the underlying TCP connection is kept open (alive) until the proxy disposed?
George2
No you shouldn't. Wrapping the proxy in a using is a bad idea as if Dispose gets called on a proxy in a faulted state then it throws an exception, hiding the exception which caused the fault in the first place.
blowdart
Thanks Shiraz, if I am not using reliable messaging and just use default settings for basicHttpBinding, will underlying connection always be live? Or each time we call a method there will be a new connection?
George2
Hi blowdart, how about your answer to my original question? :-)
George2
Hi Shiraz, I watched the video you recommended for 10 minutes, it is really cool and useful, but does it contain information about your alternative method of using Add Service Reference? Please help to clarify, thanks.
George2
Hi Shiraz, if I am not using reliable messaging and just use normal/default configuration for basicHttpConnection, will the connection be open all the time, or close/reopen each time we make a method call? Thanks!
George2
@George2 I might have posted the wrong link try this one instead http://www.dnrtv.com/default.aspx?showNum=122
Shiraz Bhaiji
+3  A: 

The connection will be closed when the underlying channel closes - by default, the BasicHttpBinding sends a connection HTTP header in messages with a Keep-Alive value, which enables clients to establish persistent connections to the services that support them.

This doesn't mean an instance of the service is kept alive, just the connection to the web server, if the web server supports it.

If you want the connection to close after every call then you can turn it off on the server side by defining a custom binding thus

<services> 
 <service>
  <endpoint address=""
        binding="customBinding"
        bindingConfiguration="HttpBinding" 
        contract="IContract" />
 </service>
</services>

<bindings>
 <customBinding>
   <binding name="HttpBinding" keepAliveEnabled="False"/>
 </customBinding>
</bindings>

Connections will close depending on how long your proxy hangs around for, and the generated proxy will reopen it again if it needs to.

blowdart
Thanks! Two confusions, 1. "This doesn't mean an instance of the service is kept alive" -- what do you you mean "an instance of the service is kept alive"? 2. "if the web server supports it." -- for IIS, how to check whether support or not?
George2
Sorry two more confusions,3. "and the generated proxy will reopen it again if it needs to." -- underlying WCF will take care of it automatically and no exception is thrown if connection is closed and re-open again? No need for developer to handle this scenario? 4. "Connections will close depending on how long your proxy hangs around for" -- is it configurable?
George2
"An instance of the service" means an instance, on the web server computer, of the class that implements the service contract. Some people have assumed that when you create a proxy instance, it creates the service instance. This is not true.
John Saunders
Thanks John, 1. I think when instance will be created or reused dependent on instancing behavior, correct? 2. how about your comments to my question 2, 3, and 4? :-)
George2
2. IIS by default supports keep alive.3. Yes.4. Well you can configure the time out, but discovering the exact criteria under which underlying connections are closed would mean examining the .NET source.In the end this really shouldn't be something you're caring about.
blowdart
Thanks blowdart, 1. for my question #2, I am asking how to check whether in IIS kept alive is configured on or off, any ideas? 2. from 4, seems normally we do not need to configure timeout value? Do you have any experience of configuring this value?
George2
+1 @blowdart thanks for the comment below on not using "using" with a wcf proxy, am currently fixing it in a few hundred places.
Shiraz Bhaiji
Heh, it's a horrible thing to fix. There are nice wrappers around for it - see http://www.infoq.com/news/2009/03/WCF-Dispose for some options
blowdart
+2  A: 

George, one thing to consider is that your code should try not to care about how, if, or when, the connection opens or closes. That's primarily the concern of the channel, and the channel should be able to manage the connection as it sees fit, without having to worry that you've written code that depends on how the channel "minds its own business".

Only if you see, or suspect, a performance problem, should you worry about implementation details like this. If you're concerned that there might be such a problem, then create a quick proof of concept application, and watch the network traffic with Fiddler or some other tool. In most cases, that will be a waste of time.

John Saunders
Thanks John, I totally agree with you. Currently I met with an issue when transfer large data from server to client. Here is my question, http://stackoverflow.com/questions/1281269/response-size-limitaiton-of-a-wcf-web-serivces I am suspecting whether the openTimeout parameter causes this issue, and I think I need to know whether connection needs to establish each time I made a method call or just open once. If just open once, this parameter should not cause this issue because I made some successful method call before this error method call, but if connection needs to establish
George2
(continued) each time, I think this parameter may impact the result. Any comments for either this parameter or whether connection needs to establish each time a method call happens or just once?
George2
Even if it needed to establish itself multiple time, each time would use the configured parameters.
John Saunders
+3  A: 

Then, each time I make a method call will it establish a new connection?

Yes, that's the default behavior and the preferred behavior - it saves you a lot of grief!

"This doesn't mean an instance of the service is kept alive" -- what do you you mean "an instance of the service is kept alive"?

In the default and preferred case of a "per-call" services, this is what happens:

  • the client proxy issues a call to the service
  • the message is serialized on the client side and sent across the wire
  • the server side has a "channel listener" which will pick up that message and see which service class will handle the call
  • the message dispatcher on your server side will instantiate an instance of "YourServiceClass"
  • the message dispatcher on the server side will now call that method on the newly created service class instance, and grab the results and package them up for the respose
  • the service class object on the server side is freed
  • the response is sent back to your client

That's one of the reasons that your service classes should be as lean, as independent of anything else as possible - they'll typically be instantiated for each request coming in, and freed afterwards.

This may seem like a really bad idea - but if you had service object instances lingering around for a longer time, you'd have to do a lot of bookkeeping in order to track their state and so on, so in the end, it's actually easier (and in general a lot safer and simpler) to create a service class, let it handle the request, and then free it again.

Marc

marc_s
Hi Marc, I am confused. From discussion with blowdart, seems keep alive is the default behavior, and if keep alive, the connection should be established only once when we made the 1st method call, not open/close each time when we make a method call. Any comments?
George2