Asp.net core 如何在ASP.NET核心应用程序中使用域集合通知?
我尝试使用Realm.NET SDK中的示例代码,但我的处理程序代码从未得到一致调用:Asp.net core 如何在ASP.NET核心应用程序中使用域集合通知?,asp.net-core,realm,Asp.net Core,Realm,我尝试使用Realm.NET SDK中的示例代码,但我的处理程序代码从未得到一致调用: var token = realm.All<Person>().SubscribeForNotifications ((sender, changes, error) => { // Access changes.InsertedIndices, changes.DeletedIndices, and changes.ModifiedIndices }); var-token=re
var token = realm.All<Person>().SubscribeForNotifications ((sender, changes, error) =>
{
// Access changes.InsertedIndices, changes.DeletedIndices, and changes.ModifiedIndices
});
var-token=realm.All()
{
//访问changes.InsertedIndices、changes.DeletedIndices和changes.ModifiedIndices
});
我试过从许多不同的线程运行它,但我怀疑它们都没有looper/runloop,SDK中提到了这一要求。甚至有可能在ASP.NET内核中创建一个与Realm一起工作的looper/runloop线程吗?我该怎么做 终于想出了如何实现适用于领域通知的looper/run循环。解决方案的关键部分包括:
public class RealmNotificationService : IHostedService
{
private System.Threading.Thread _thread;
public Task StartAsync(CancellationToken stoppingToken)
{
_thread = new System.Threading.Thread(() =>
Nito.AsyncEx.AsyncContext.Run(() => Looper(stoppingToken))
);
_thread.Start();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken stoppingToken)
{
return Task.CompletedTask;
}
private async void Looper(CancellationToken stoppingToken)
{
using (var connection = await Realm.GetInstanceAsync({YOUR-CONFIG}))
{
var token = connection.All<{YOUR-OBJECT}>().SubscribeForNotifications((sender, changes, error) =>
{
// Access changes.InsertedIndices, changes.DeletedIndices, and changes.ModifiedIndices
});
}
while (!stoppingToken.IsCancellationRequested)
{
await connection.Realm.RefreshAsync();
await Task.Delay(2000, stoppingToken);
}
}
}
公共类重新通知服务:IHostedService
{
私有系统.Threading.Thread\u线程;
公共任务StartAsync(取消令牌停止停止)
{
_线程=新系统。线程。线程(()=>
Nito.AsyncEx.AsyncContext.Run(()=>Looper(stoppingToken))
);
_thread.Start();
返回Task.CompletedTask;
}
公共任务StopAsync(CancellationToken stoppingToken)
{
返回Task.CompletedTask;
}
专用异步无效循环器(CancellationToken stoppingToken)
{
使用(var connection=await Realm.GetInstanceAsync({YOUR-CONFIG}))
{
var token=connection.All().SubscribeForNotifications((发送方、更改、错误)=>
{
//访问changes.InsertedIndices、changes.DeletedIndices和changes.ModifiedIndices
});
}
同时(!stoppingToken.IsCancellationRequested)
{
wait connection.Realm.RefreshAsync();
等待任务。延迟(2000,stoppingToken);
}
}
}
比尔·劳达堡的回答几乎对我有用。但是,最后的while
循环在using
之外,因此连接
未定义
通过使用将该循环移动到内部,我能够使其工作。我还删除了RefreshAsync()
,因为它似乎不需要;领域自动实时更新,无需刷新
这是我的密码:
public class RealmNotificationService : IHostedService
{
private System.Threading.Thread _thread;
public Task StartAsync(CancellationToken stoppingToken)
{
_thread = new System.Threading.Thread(() =>
Nito.AsyncEx.AsyncContext.Run(() => Looper(stoppingToken))
);
_thread.Start();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken stoppingToken)
{
return Task.CompletedTask;
}
private async void Looper(CancellationToken stoppingToken)
{
using (var connection = await Realm.GetInstanceAsync({YOUR-CONFIG}))
{
var token = connection.All<{YOUR-OBJECT}>().SubscribeForNotifications((sender, changes, error) =>
{
// Access changes.InsertedIndices, changes.DeletedIndices, and changes.ModifiedIndices
});
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(2000, stoppingToken);
}
}
}
}
公共类重新通知服务:IHostedService
{
私有系统.Threading.Thread\u线程;
公共任务StartAsync(取消令牌停止停止)
{
_线程=新系统。线程。线程(()=>
Nito.AsyncEx.AsyncContext.Run(()=>Looper(stoppingToken))
);
_thread.Start();
返回Task.CompletedTask;
}
公共任务StopAsync(CancellationToken stoppingToken)
{
返回Task.CompletedTask;
}
专用异步无效循环器(CancellationToken stoppingToken)
{
使用(var connection=await Realm.GetInstanceAsync({YOUR-CONFIG}))
{
var token=connection.All().SubscribeForNotifications((发送方、更改、错误)=>
{
//访问changes.InsertedIndices、changes.DeletedIndices和changes.ModifiedIndices
});
同时(!stoppingToken.IsCancellationRequested)
{
等待任务。延迟(2000,stoppingToken);
}
}
}
}