Skip to content

Commit b4fa2f6

Browse files
committed
Mongodb documentation updated with details on object serialization
1 parent 9a04d1c commit b4fa2f6

File tree

1 file changed

+32
-0
lines changed
  • src/providers/WorkflowCore.Persistence.MongoDB

1 file changed

+32
-0
lines changed

src/providers/WorkflowCore.Persistence.MongoDB/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,35 @@ Use the .UseMongoDB extension method when building your service provider.
1717
```C#
1818
services.AddWorkflow(x => x.UseMongoDB(@"mongodb://localhost:27017", "workflow"));
1919
```
20+
21+
### State object serialization
22+
23+
By default (to maintain backwards compatibility), the state object is serialized using a two step serialization process using object -> JSON -> BSON serialization.
24+
This approach has some limitations, for example you cannot control which types will be used in MongoDB for particular fields and you cannot use basic types that are not present in JSON (decimal, timestamp, etc).
25+
26+
To eliminate these limitations, you can use a direct object -> BSON serialization and utilize all serialization possibilities that MongoDb driver provides. You can read more in the [MongoDb CSharp documentation](https://mongodb.github.io/mongo-csharp-driver/1.11/serialization/).
27+
To enable direct serilization you need to register a class map for you state class somewhere in your startup process before you run `WorkflowHost`.
28+
29+
```C#
30+
private void RunWorkflow()
31+
{
32+
var host = this._serviceProvider.GetService<IWorkflowHost>();
33+
if (host == null)
34+
{
35+
return;
36+
}
37+
38+
if (!BsonClassMap.IsClassMapRegistered(typeof(MyWorkflowState)))
39+
{
40+
BsonClassMap.RegisterClassMap<MyWorkflowState>(cm =>
41+
{
42+
cm.AutoMap();
43+
});
44+
}
45+
46+
host.RegisterWorkflow<MyWorkflow, MyWorkflowState>();
47+
48+
host.Start();
49+
}
50+
51+
```

0 commit comments

Comments
 (0)