orleans 2.0 进阶之自定义持久化储存
程序员文章站
2023-11-10 21:41:16
一丶简单介绍下目录结构和项目依赖,如图 二丶主要核心自定义代码 1. 添加自定义实现类 CustomProvider 2. 在silo中注入代替默认实现 3. 在grain类上启用 三丶运行结果如下 示例代码下载地址:SimpleStorage ......
一丶简单介绍下目录结构和项目依赖,如图
二丶主要核心自定义代码
1. 添加自定义实现类 customprovider
public class customprovider : igrainstorage { public task clearstateasync(string graintype, grainreference grainreference, igrainstate grainstate) { throw new notimplementedexception(); } public task readstateasync(string graintype, grainreference grainreference, igrainstate grainstate) { if(graintype== "grain.usergrain") { var id = grainreference.getprimarykeylong(); //模拟从数据库读出user var user = new userstate() { mobilephone = "124214214", name = "李四" }; grainstate.state = user; } return task.completedtask; } public task writestateasync(string graintype, grainreference grainreference, igrainstate grainstate) { if (graintype== "grain.usergrain") { var id = grainreference.getprimarykeylong(); //模拟保存数据库 } return task.completedtask; } }
2. 在silo中注入代替默认实现
.configureservices(services => { var providername = "customprovider"; services.tryaddsingleton(sp => sp.getservicebyname<igrainstorage>(providerconstants.default_storage_provider_name)); services.addsingletonnamedservice<igrainstorage, customprovider>(providername); })
3. 在grain类上启用
[storageprovider(providername = "customprovider")]
三丶运行结果如下
示例代码下载地址:simplestorage
上一篇: 基于C# WPF框架的贪吃蛇
下一篇: ThinkPHP连接数据库的方式汇总