I have a server client implementation using WCF
where the clients sends a Connect
message to the server when it starts.
This is the code I'm using in my client application (WCF service is self hosted in this case):
public void InitializeCommunicationChannel()
{
this._proxy = new DistributionServiceClient(this._context, "NetTcpBinding_IDistributionService");
Log.Write(Level.Debug, "Initialized proxy..");
//set credentials
this._proxy.ClientCredentials.Windows.ClientCredential.Domain = PasswordManager.ServerAdminDomain;
this._proxy.ClientCredentials.Windows.ClientCredential.UserName = PasswordManager.ServerAdminUsername;
this._proxy.ClientCredentials.Windows.ClientCredential.Password = PasswordManager.ServerAdminPassword;
this._proxy.InnerChannel.Faulted += new EventHandler(this.InnerChannel_Faulted);
this._proxy.InnerChannel.Closing += new EventHandler(this.InnerChannel_Closing);
//send a connect message to the server
try
{
Log.Write(Level.Debug, "Sending connect message..");
this.StationId = this._proxy.ClientConnected(ClientConfiguration.HostName, this.Version, ClientConfiguration.CurrentIpAddress);
Log.Write(Level.Debug, "Connect message sent..");
Log.Write(Level.Info, "Connected to server.");
this.ConnectionState = "Connected";
}
catch (ConfigurationErrorsException cEx)
{
Log.Write(Level.Error, cEx.Message);
Log.Write(Level.Debug, cEx.ToString());
}
catch (FaultException fEx)
{
//probably server didn't recognize configured ip or hostname
Log.Write(Level.Error, fEx.Message);
Log.Write(Level.Debug, fEx.ToString());
}
catch (CommunicationException cEx)
{
Log.Write(Level.Debug, cEx.Message);
Log.Write(Level.Debug, cEx.ToString());
}
catch (System.Security.Authentication.InvalidCredentialException iEx)
{
Log.Write(Level.Error, iEx.Message);
Log.Write(Level.Debug, iEx.ToString());
}
catch (Exception ex)
{
Log.Write(Level.Error, ex.Message);
Log.Write(Level.Debug, ex.ToString());
}
void InnerChannel_Closing(object sender, EventArgs e)
{
Log.Write(Level.Debug, "Communication channel is closing down..");
this.ConnectionState = "Attempting connection..";
}
void InnerChannel_Faulted(object sender, EventArgs e)
{
this.ConnectionState = "Not connected";
Log.Write(Level.Debug, "Communication channel faulted..");
//something went wrong
this._proxy.Abort();
this._proxy.InnerChannel.Faulted -= this.InnerChannel_Faulted;
this._proxy.InnerChannel.Closing -= this.InnerChannel_Closing;
//give the server a chance to get back online; retry after 10s
Thread.Sleep(10000);
//re-initialize the communication channel
this.InitializeCommunicationChannel();
}
The problem that I'm having is that when the server address is not correctly configured in the client's config file, the channel faults when I try to send the Connect
message, but no exception is being caught.
Is this because the connection is occurring on a different thread therefore it never ends up in my catch
code? How do I catch endpoint configuration exceptions in code? The InnerChannel_Faulted
's EventArgs
member doesn't contain any useful information.
Thanks a lot.