【发布时间】:2017-04-01 16:32:33
【问题描述】:
我必须在 SQLite 数据库 中同步来自 WCF WebServices 的数据。 这种同步代表了十几个 WebService,可以“分组”为 4 类:
- “用户的”权利
- “Forms”数据,可从两侧(用户/服务器)更新
- “服务器”数据,仅从服务器更新
- “Views”数据,在本地复制服务器的视图
对 WebService 的每次调用都是通过 HttpClient 完成的:
response = await client.PostAsync(webServiceName, content);
每个 WebService 都有自己的 async 方法,其中 WebService 响应被反序列化:
public static async Task<string> PushForm(List<KeyValuePair<string, string>> parameters)
{
var response = await JsonParser.GetJsonFromUrl(WebServiceName.PushForm.Value, parameters);
Forms forms = new Forms();
try
{
forms = JsonConvert.DeserializeObject<Forms>(response);
return response;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
然后我有一个 SynchronizationService 类,我在其中按类别重新组合对 WebServices 的调用:
public async Task<bool> SynchronizeServerData()
{
bool result = false;
try
{
result = true;
List<Table1> tables1 = await WebServices.GetListTable1(null);
if (tables1 != null)
{
ServiceLocator.Current.GetInstance<IRepository>().DeleteAll<Table1>(true);
ServiceLocator.Current.GetInstance<IRepository>().AddAll<Table1>(tables1);
}
List<Table2> tables2 = await WebServices.GetListTable2(null);
if (tables2 != null)
{
ServiceLocator.Current.GetInstance<IRepository>().DeleteAll<Table2>(true);
ServiceLocator.Current.GetInstance<IRepository>().AddAll<Table2>(tables2);
}
List<Table3> tables3 = await WebServices.GetListTable3(null);
if (tables3 != null)
{
ServiceLocator.Current.GetInstance<IRepository>().DeleteAll<Table3>(true);
ServiceLocator.Current.GetInstance<IRepository>().AddAll<Table3>(tables3);
}
...
}
catch (Exception e)
{
result = false;
}
return result;
}
最后,在主视图模型中,我调用了以下每个方法:
public async void SynchronizeData(bool firstSync)
{
IsBusy = true;
var resUsers = await _synchronisation.SynchronizeUsersRights();
var resServer = await _synchronisation.SynchronizeServerData();
var resForm = await _synchronisation.SynchronizeForms();
var resViews = await _synchronisation.SynchronizeViews();
IsBusy = false;
}
但是由于使用了“await”,性能并不好。
=> 我想知道是否有一种简单的方法可以“并行化”调用以优化性能?或者是否可以为此将数据恢复与 SQLite 更新分开?
【问题讨论】:
标签: c# wcf asynchronous synchronization async-await