0
在BackgroundWorker的DoWork事件處理程序中調用此代碼,我創建一個WCF客戶端並在try塊中使用它,如果在處理程序返回之前發生異常,我想中止並重試5次。嵌套try/catch。好嗎?
private void WorkerDoWork(object sender, DoWorkEventArgs e)
{
var mmc = new ServiceClient();
try
{
e.Result = mmc.SubmitData(measure);
}
catch (Exception)
{
mmc.Abort();
mmc = new ServiceClient();
var counter = 0;
while ((bool)e.Result == false && counter++ < 5)
{
try
{
e.Result = mmc.SubmitData(measure);
}
catch (Exception)
{
mmc.Abort();
mmc = new ServiceClient();
}
}
}
finally
{
if (mmc.State == CommunicationState.Faulted)
{
mmc.Abort();
}
else
{
mmc.Close();
}
}
}
我沒有與代碼真的開心,感覺就像是東西腥吧!特別是嵌套的try/catch。
代碼無效鏈接。或者我應該重構它?