Reputation: 15715
I just followed this tutorial and played a little bit with the code. I'm almost sure I read somewhere that there is a timeout for the channel, so it might get automatically closed eventually. So I tried simply opening a new channel in my client for each method I wanted to invoke, and eventually (after lots of calls) I got errors.
Seems like there is a limit on how many channels I can have open at the same time. But since the channel is an instance of a custom object, I don't see how can I close it or kill it or whatever I need to do with it to get rid of it so I can create other channels.
Then I noticed on the CreateChannel
documentation that my TChannel
should implement IChannel
(which the tutorial I linked above doesn't do). So, is this how I would close my channel? If so, how would I close it or what should I do on my implementation of the Close
method? And what should I do on the implementation of every other method if I do have to implement the interface?
Or should I just use a single channel for as long as it lasts? Anyway, how am I supposed to know whether is faulted or open or closed if all I have is an instance of my own class?
As you can see I'm pretty lost on the subject so I hope you can point me in the right direction.
Upvotes: 0
Views: 720
Reputation: 2453
Peladao basically hits the nail on the head.
To clarify some of what he is saying, CreateChannel will create a (proxy) object which implements both your custom service interface and IClientChannel.
Typically you do keep the channel open and reuse its calls. Beware also that once it enters a fault state there is no recovery, you must open a new channel. As Peladao mentions a fault state can be detected via ((IClientChannel)channel).State, and also don't forget you'll generally get an exception too.
If memory serves, the debug process for WCF accepts 10 simultaneous channels for a service.
Upvotes: 1
Reputation: 4090
ChannelFactory<TChannel>.CreateChannel
creates and return a channel of your specified service type. The returned object already implements IChannel
. You (normally?) don't need to implement your own Close
method, nor any other methods of IChannel
.
Normally you don't create a new channel for every call, you just re-use it. (Only in some specific cases it may be better to create a new channel for every call).
You can close the channel by casting it to IClientChannel
. Use this pattern:
try
{
((IClientChannel)channel).Close();
}
catch (Exception ex)
{
((IClientChannel)channel).Abort();
}
You can use ((IClientChannel)channel).State
to get the state of the channel (i.e. CreatedOpened
, Faulted
, Closed
).
Upvotes: 5