クライアントが起動時にサーバーにメッセージを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
メッセージを送信しようとするとチャネルに障害が発生することですが、例外はキャッチされません。
これは、接続が別のスレッドで発生しているためであり、catch
コードで終了することはありませんか? コードでエンドポイント構成の例外をキャッチするにはどうすればよいですか? のメンバーには有用な情報が含まれていませんInnerChannel_Faulted
。EventArgs
どうもありがとう。