我有一个使用WCF
的服务器客户端实现,客户端在启动时向服务器发送Connect
消息。
这是我在客户端应用程序中使用的代码(在这种情况下,WCF服务是自托管的):
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();
}
我遇到的问题是,当客户端的配置文件中未正确配置服务器地址时,当我尝试发送Connect
消息时,通道出现故障,但没有异常被捕获。 / p>
这是因为连接发生在另一个线程上,因此它永远不会在我的catch
代码中结束吗?如何在代码中捕获端点配置异常? InnerChannel_Faulted
的{{1}}成员不包含任何有用的信息。
非常感谢。