I’m trying to figure out how to get my WCF proxy class to wait to be ready before proceeding, or allowing calls to the service.
I tried something like this:
_proxy = new WCFBlackjack(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/blackjack/IWCFBlackjack"));
while(!_proxy.State.Equals(CommunicationState.Opened)) {
if(!_proxy.State.Equals(CommunicationState.Opening)) {
try {
_proxy.Open();
} catch(EndpointNotFoundException enfe) {
/* ... */
}
}
System.Threading.Thread.Sleep(1);
}
But obviously that doesn’t work otherwise I wouldn’t be here. At first it seems to work but the ChannelFactory throws an InvalidOperationException on the open statement.
I’m new to WCF and maybe I’m thinking too much along the lines of something that is Sockets-esque but I would like my client to continue to retry until the endpoint becomes available or can be found, and somehow fire an on-connected event or something along those lines.
With the EndpointNotFoundException, you’re probably seeing a message that looks something like, “There was no endpoint listening at…”. What is happening is that during your while loop, you open the proxy on the first pass, handle the error, and then on the second pass you get the InvalidOperationException that says, “The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be modified while it is in the Opened state.”
If you abort the connection on the existing proxy, you will not be able to use it for communication going forward. Once you close the connection, the proxy object will be disposed of. The solution is to close the existing proxy and create a new one. I have updated your code below: