1

I am new to WCF. Is the client connected when the service is instantiated

 SampleService client = new SampleService();

Or does it connect and disconnect when a method is called on the client?

client.sampleMethod();

I want to connect to the client and send multiple messages across the same connection. Is this possible?

ministrymason
  • 1,783
  • 2
  • 20
  • 33
  • 1
    It will connect when used and there shouldn't be any problem doing multiple calls to the service. It might be a good idea to explicitly close it when done though. – Jontatas Jan 17 '12 at 09:12

3 Answers3

1

To answer your second question: the connection is kept open by default.

For the first, and as an clarification to above statement: it depends on the binding. For starters, there is no Connect() method in ClientBase<TChannel> (which a WCF proxy client inherits from), so there'll be no use calling it since that'll throw a compiler error.

A connection to the service is made upon the first call to the service; the constructor does nothing more than bring the client object in a usable state, it does not connect to the service.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • So doing client.Open() will keep the same connection alive till i call client.Close()? So i can open the client send multiple messages on the same connection then close. Is this the best strategy for performance? thanks – ministrymason Jan 17 '12 at 10:47
  • @ministrymason that is correct. Though again, it depends on the binding. A basicHttp binding will do nothing when you call `Open()`, it will open the connection upon the first service method call. – CodeCaster Jan 17 '12 at 11:13
0

It is connected when you use the service. Also you would need to call client.Close() if you would write it like that. We mostly write our service calls like this:

using(var client = new SampleServiceClient()){
  client.MethodA();
  client.MethodB();
}

So yes you can call multiple methods on one instance of the service. By writing the using statement you make sure that the service is disposed after usage.

Or if you like:

var client = new SampleServiceClient();
client.MethodA();
client.MethodB();
client.Close();
JoJa
  • 612
  • 5
  • 8
  • 2
    No, it's not. The channel is opened on the call to client.MetodA(); Try this by setting a wrong endpoint adress and try to new up a new client. That will cause an exeption when calling MethodA, not on the new SampleServiceClient() – Per Kastman Jan 17 '12 at 09:21
  • 2
    And btw, do not use the use using statement with service proxies. Microsoft explains why at http://msdn.microsoft.com/en-us/library/aa355056.aspx – Per Kastman Jan 17 '12 at 09:23
  • @KMan I know about the Microsoft recommendation but this is not correct. There have been multiple blog post concerning this topic and this is a point of discussion in the community. This is a choice you have to make for your own. – JoJa Jan 17 '12 at 09:31
  • 1
    Still, if the actual coders of the product says, don't do it, I would listen. ;) – Jontatas Jan 17 '12 at 09:40
  • it's a bug in the disposing of the service (WCF bug) and even their solution isn't save ;). Never had any problems with it though so I keep using it until I do cross this issue. :) – JoJa Jan 17 '12 at 09:48
0

You will need to call the generated proxy it's Connect method.

e.g.

SampleServiceProxy client = new SampleServiceProxy();
client.Connect()
client.SampleMethod()
Zong
  • 6,160
  • 5
  • 32
  • 46
msjonathan
  • 514
  • 2
  • 11
  • 26