我正在使用 Microsoft Visual Studio Community 2022(64 位)- 预览版、.Net Maui、Azure Web App 和 SQL Server,并遵循了 ToDo 示例中给出的示例。 我成功地从另一个网站收集数据,然后将其保存到我的远程表中。
private async Task InitializeAsync()
{
// Short circuit, in case we are already initialized.
if (_initialized)
{
return;
}
try
{
// Wait to get the async initialization lock
await _asyncLock.WaitAsync();
// Create the offline store definition
//var connectionString = new UriBuilder { Scheme = "file", Path = Constants.OfflineDatabasePath, Query = "?mode=rwc" }.Uri.ToString();
var connectionString = new UriBuilder { Scheme = "file", Path = Constants.DatabasePath, Query = "?mode=rwc" }.Uri.ToString();
var store = new OfflineSQLiteStore(connectionString);
// Define the offline tables
store.DefineTable<Profile>();
store.DefineTable<ProfileStore>();
store.DefineTable<Employee>();
store.DefineTable<EmployeeJob>();
store.DefineTable<Payroll>();
store.DefineTable<PayrollDetails>();
var options = new DatasyncClientOptions
{
OfflineStore = store,
HttpPipeline = new HttpMessageHandler[] { new LoggingHandler() }
};
// Create the datasync client.
_DatasyncClient = new DatasyncClient(Constants.ServiceUri, options);
// Initialize the database
await _DatasyncClient.InitializeOfflineStoreAsync();
// Get a reference to the offline tables.
_ProfileTable = _DatasyncClient.GetOfflineTable<Profile>();
_ProfileStoreTable = _DatasyncClient.GetOfflineTable<ProfileStore>();
_EmployeeTable = _DatasyncClient.GetOfflineTable<Employee>();
_EmployeeJobTable = _DatasyncClient.GetOfflineTable<EmployeeJob>();
_PayrollTable = _DatasyncClient.GetOfflineTable<Payroll>();
_PayrollDetailsTable = _DatasyncClient.GetOfflineTable<PayrollDetails>();
**await _DatasyncClient.PushTablesAsync(); ** <-- This is what fails
// Set _initialized to true to prevent duplication of locking.
_initialized = true;
}
catch (Exception)
{
// Re-throw the exception.
throw;
}
finally
{
_asyncLock.Release();
}
}
Sometimes, it gets into a state where the _DatasyncClient.PushTablesAsync fails.
HResult = -2146233088
ex = {"Push operation has failed. See the PushResult for details."}
PushResult = {Microsoft.Datasync.Client.Offline.PushCompletionResult}
Errors[0]:
Status = PreconditionFailed
我已经登录,使用:
var options = new DatasyncClientOptions
{
OfflineStore = store,
HttpPipeline = new HttpMessageHandler[] { new LoggingHandler() }
};
_DatasyncClient = new DatasyncClient(Constants.ServiceUri, options);
它显示(我擦洗了一些数据):
[HTTP] >>> PUT https://<myapp>.azurewebsites.net/tables/employeejob/02a9b6696aa341bcabc4aba2491c1aea
[HTTP] >>> If-Match: "AAAAAAAAUlU="
[HTTP] >>> ZUMO-API-VERSION: 3.0.0
[HTTP] >>> User-Agent: Datasync/6.0.0.0
[HTTP] >>> User-Agent: (lang=Managed;os=Microsoft Windows 10.0.22621/Microsoft Windows NT 10.0.22621.0;arch=X64;version=6.0.0.0;test)
[HTTP] >>> X-ZUMO-VERSION: Datasync/6.0.0.0 (lang=Managed;os=Microsoft Windows 10.0.22621/Microsoft Windows NT 10.0.22621.0;arch=X64;version=6.0.0.0;test)
[HTTP] >>> Content-Type: application/json; charset=utf-8
[HTTP] >>> {"id":"02a9b6696aa341bcabc4aba2491c1aea","employeeID":"268315","profileDBID":"c6e7ea7d-3dde-48c3-a582-186e594d8d3f","role":"Manager","startDate":"2019-12-30T08:00:00Z","endDate":null,"version":"AAAAAAAAUlU="}
[HTTP] <<< PreconditionFailed Precondition Failed
[HTTP] <<< Date: Fri, 06 Oct 2023 08:22:58 GMT
[HTTP] <<< Server: Microsoft-IIS/10.0
[HTTP] <<< ETag: "AAAAAAAAUnc="
[HTTP] <<< X-Powered-By: ASP.NET
[HTTP] <<< Content-Length: 686
[HTTP] <<< Content-Type: application/json; charset=utf-8
[HTTP] <<< Last-Modified: Fri, 06 Oct 2023 07:29:01 GMT
[HTTP] <<< {"employeeID":"268315","profileDBID":"c6e7ea7d-3dde-48c3-a582-186e594d8d3f","role":"Manager","startDate":"2019-12-30T08:00:00","endDate":null,"id":"02a9b6696aa341bcabc4aba2491c1aea","updatedAt":"2023-10-06T07:29:01.806+00:00","version":"AAAAAAAAUnc=","deleted":false}
Exception thrown: 'Microsoft.Datasync.Client.Offline.PushFailedException' in System.Private.CoreLib.dll
数据确实成功进入我的在线数据库,所以我知道我的代码正在工作,但偶尔会失败,并且永远不会恢复。我唯一能做的就是删除我本地的离线数据库(或者我猜使用清除功能)
我的数据似乎没有任何问题。
等待 _DatasyncClient.PushTablesAsync();
期望它将本地数据库记录推送到在线数据库。
我研究了该错误并阅读了有关 Azure ETag 设置的内容,但不确定是否可以在 Microsoft.DataSync 客户端上设置
令人沮丧,因为我希望有一个可以同步到在线数据库的功能离线数据库,但我可能必须手动执行此操作。
答案是放弃 Microsoft.Datasync,它还没有准备好迎接黄金时段。 我现在在本地执行实体框架并手动复制到 Azure。