我有召喚出一個互聯網服務類來獲得一些數據:編程修改在溫莎城堡的依賴
public class MarketingService
{
private IDataProvider _provider;
public MarketingService(IDataProvider provider)
{
_provider = provider;
}
public string GetData(int id)
{
return _provider.Get(id);
}
}
目前我有兩個供應商:HttpDataProvider和FileDataProvider。通常我會連接到HttpDataProvider,但如果外部Web服務失敗,我想改變系統綁定到FileDataProvider。例如:
public string GetData(int id)
{
string result = "";
try
{
result = GetData(id); // call to HttpDataProvider
}
catch (Exception)
{
// change the Windsor binding so that all future calls go automatically to the
// FileDataProvier
// And while I'm at it, retry against the FileDataProvider
}
return result;
}
所以當這個被執行後,所有將來的MarketingService實例都會自動連接到FileDataProvider。有誰知道如何在飛行中更改Windsor綁定?
我們已經成功地使用了這個實現,但是我們的頂級消耗線程正在使用字典,因此我們將其更改爲ConcurrentDictionary以使其線程安全,如下所示:https://blogs.msdn.microsoft.com/tess/2009/12/21 /高CPU-在淨APP-使用-A-靜態通用詞典/ – Calum 2016-11-18 11:38:34