Elsa 3.1.3中MongoDB批量写入报错:Payload.$type字段非法
问题描述
在Elsa项目中使用MongoDB作为持久化存储运行工作流时遇到错误,使用SQLite时无此问题。
错误信息
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
执行请求时发生未处理异常。
MongoDB.Driver.MongoBulkWriteException1[Elsa.Workflows.Runtime.Entities.StoredTrigger]: 批量写入操作导致一个或多个错误。WriteErrors: [ { Category : "Uncategorized", Code : 52, Message : "The dollar ($) prefixed field '$type' in 'Payload.$type' is not valid for storage." } ]. at MongoDB.Driver.MongoCollectionImpl1.BulkWriteAsync(IClientSessionHandle session, IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)
项目配置文件(ElsaServer.csproj)
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <RuntimeFrameworkVersion>8.0.0</RuntimeFrameworkVersion> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <GenerateDocumentationFile>True</GenerateDocumentationFile> <DocumentationFile>C:\nugget rewrite project\JustAtestApp\Docs\ORVWorkflowAdminServer.xml</DocumentationFile> <IsPackable>true</IsPackable> <StaticWebAssetsEnabled>false</StaticWebAssetsEnabled> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <Optimize>True</Optimize> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <Optimize>False</Optimize> </PropertyGroup> <ItemGroup> <Content Remove="Properties\launchSettings.json" /> <Content Remove="Properties\launchSettings_old.json" /> </ItemGroup> <ItemGroup> <_WebToolingArtifacts Remove="Properties\launchSettings.json" /> <_WebToolingArtifacts Remove="Properties\launchSettings_old.json" /> </ItemGroup> <ItemGroup> <PackageReference Include="Elsa" Version="3.1.3" /> <PackageReference Include="Elsa.Common" Version="3.1.3" /> <PackageReference Include="Elsa.CSharp" Version="3.1.3" /> <PackageReference Include="Elsa.Expressions" Version="3.1.3" /> <PackageReference Include="Elsa.Features" Version="3.1.3" /> <PackageReference Include="Elsa.Http" Version="3.1.3" /> <PackageReference Include="Elsa.Identity" Version="3.1.3" /> <PackageReference Include="Elsa.JavaScript" Version="3.1.3" /> <PackageReference Include="Elsa.Labels" Version="3.1.3" /> <PackageReference Include="Elsa.Liquid" Version="3.1.3" /> <PackageReference Include="Elsa.MongoDb" Version="3.1.3" /> <PackageReference Include="Elsa.SasTokens" Version="3.1.3" /> <PackageReference Include="Elsa.Scheduling" Version="3.1.3" /> <PackageReference Include="Elsa.Workflows.Api" Version="3.1.3" /> <PackageReference Include="Elsa.Workflows.Core" Version="3.1.3" /> <PackageReference Include="Elsa.Workflows.Management" Version="3.1.3" /> <PackageReference Include="Elsa.Workflows.Runtime" Version="3.1.3" /> <PackageReference Include="FluentStorage" Version="5.5.1" /> <PackageReference Include="Fluid.Core" Version="2.11.1" /> <PackageReference Include="Jint" Version="4.0.1" /> <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" /> <PackageReference Include="MongoDB.Driver" Version="2.28.0" /> <PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.5.0" /> <PackageReference Include="MongoDB.Driver.Extensions" Version="2.0.2" /> <PackageReference Include="Open.Linq.AsyncExtensions" Version="1.2.0" /> <PackageReference Include="Risk.Common.Classes" Version="1.0.102" /> <PackageReference Include="Risk.NetCore.ServiceUtilities" Version="8.0.0-RC-2" /> </ItemGroup> </Project>
Program.cs代码
using Elsa.Extensions; using Elsa.MongoDb.Extensions; using Elsa.MongoDb.Modules.Management; using Elsa.MongoDb.Modules.Runtime; var builder = WebApplication.CreateBuilder(args); builder.Configuration .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); builder.Services.AddElsa(elsa => { var configuration = builder.Configuration; elsa.UseMongoDb(configuration.GetConnectionString("MongoDb")); // Configure Management layer to use MongoDB. elsa.UseWorkflowManagement(management => management.UseMongoDb()); // Configure Runtime layer to use MongoDB. elsa.UseWorkflowRuntime(runtime => runtime.UseMongoDb()); // Default Identity features for authentication/authorization. elsa.UseIdentity(identity => { identity.TokenOptions = options => options.SigningKey = "sufficiently-large-secret-signing-key"; // This key needs to be at least 256 bits long. identity.UseAdminUserProvider(); }); // Configure ASP.NET authentication/authorization. elsa.UseDefaultAuthentication(auth => auth.UseAdminApiKey()); // Expose Elsa API endpoints. elsa.UseWorkflowsApi(); // Setup a SignalR hub for real-time updates from the server. elsa.UseRealTimeWorkflows(); // Enable C# workflow expressions elsa.UseCSharp(); // Enable HTTP activities. elsa.UseHttp(); // Use timer activities. elsa.UseScheduling(); // Register custom activities from the application, if any. elsa.AddActivitiesFrom<Program>(); // Register custom workflows from the application, if any. elsa.AddWorkflowsFrom<Program>(); }); // Configure CORS to allow designer app hosted on a different origin to invoke the APIs. builder.Services.AddCors(cors => cors .AddDefaultPolicy(policy => policy .AllowAnyOrigin() // For demo purposes only. Use a specific origin instead. .AllowAnyHeader() .AllowAnyMethod() .WithExposedHeaders("*"))); // Required for Elsa Studio in order to support running workflows from the designer. Alternatively, you can use the `*` wildcard to expose all headers or `x-elsa-workflow-instance-id`. // Add Health Checks. builder.Services.AddHealthChecks(); // Build the web application. var app = builder.Build(); // Configure web application's middleware pipeline. app.UseCors(); app.UseRouting(); // Required for SignalR. app.UseAuthentication(); app.UseAuthorization(); app.UseWorkflowsApi(); // Use Elsa API endpoints. app.UseWorkflows(); // Use Elsa middleware to handle HTTP requests mapped to HTTP Endpoint activities. app.UseWorkflowsSignalRHubs(); // Optional SignalR integration. Elsa Studio uses SignalR to receive real-time updates from the server. // Map the health check endpoint. app.MapHealthChecks("/health"); app.Run();
复现步骤
- 使用提供的ElsaServer.csproj和Program.cs创建新的Elsa Server项目
- 在appsettings.json中配置MongoDB连接字符串
- 运行应用程序
- 创建并运行新的Elsa Studio项目
- 创建新工作流并尝试运行
补充信息
- 使用SQLite作为持久化存储时无此错误
- MongoDB服务器本地运行,地址为
mongodb://localhost:27017/workflow
预期行为
工作流应无错误执行
实际行为
抛出MongoBulkWriteException,错误信息:"The dollar ($) prefixed field '$type' in 'Payload.$type' is not valid for storage."
环境信息
- Elsa版本:3.1.3
- Elsa.MongoDb版本:3.1.3
- .NET版本:8.0.0
已尝试添加缺失的包,但问题仍存在,怀疑有文档未提及的配置遗漏。
内容的提问来源于stack exchange,提问作者Medupi Ramaboea

