2013-01-05 109 views
0

以下是我的WCF連接代碼。如果連接超時,我想重試3次。我怎樣才能做到這一點? 感謝 DAMOwcf連接超時重試

C#代碼

   // Get the status from the client machine 
       ServiceReference1.JobsClient Client = new ServiceReference1.JobsClient(); 
       Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(ConfigurationManager.AppSettings["ServicePath"]); 
       FactoryAuditEventNotification.ServiceReference1.ReturnClass SystemStatus_Result; 
       SystemStatus_Result = Client.SystemState(System.Environment.MachineName); 

       if (SystemStatus_Result.ErrorCode < 0) // Error, set the textbox state 
        this.textBoxState.BeginInvoke((MethodInvoker)(() => this.textBoxState.Text = "Unknown")); 
       else 
        this.textBoxState.BeginInvoke((MethodInvoker)(() => this.textBoxState.Text = "Success")); 

回答

2

爲什麼不使用一個for循環?

for(int currentCount = 1;currentCount <= 3;currentCount++) 
{ 
    try 
    { 
    //your connection logic 
    } 
    catch(EndpointNotFoundException) 
    { 
    //throw after three retries 
    if(currentCount == 3) 
     throw; 
    } 
    catch(Exception) 
    { 
    //Handle other exceptions as necessary 
    } 
}