Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore
26.8.13.2
dotnet add package Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore --version 26.8.13.2
NuGet\Install-Package Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore -Version 26.8.13.2
<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore" Version="26.8.13.2" />
<PackageVersion Include="Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore" Version="26.8.13.2" />
<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore" />
paket add Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore --version 26.8.13.2
#r "nuget: Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore, 26.8.13.2"
#:package Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore@26.8.13.2
#addin nuget:?package=Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore&version=26.8.13.2
#tool nuget:?package=Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore&version=26.8.13.2
Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore
MAF 会话级存储 EF Core 模块。为 MAF Hosting 场景提供 AgentSessionStore 的持久化实现(EfCoreAgentSessionStore<TDbContext>),复用消息级模块 Galosys.Foundation.Agents.AI.EntityFrameworkCore 的会话行实体,将完整会话(含 StateBag)序列化持久化并在重启后恢复。
消息级聊天历史存储(
ChatHistoryProvider)在Galosys.Foundation.Agents.AI.EntityFrameworkCore模块中。
安装
<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore" />
注册(覆盖内存存储)
AddEfCoreAgentSessionStore<TDbContext>() 以 RemoveAll + AddSingleton 覆盖 Hosting 默认的 InMemoryAgentSessionStore:
using Microsoft.Extensions.DependencyInjection;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddAgentsHostingCore(); // 默认内存 Agent 会话存储
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=agents.db"));
builder.Services.AddEfCoreAgentSessionStore<MyDbContext>(); // 覆盖为 EF Core 持久化
HostedAgentBuilder 接入
using Microsoft.Agents.AI.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddAgentsHostingCore();
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=agents.db"));
var agentBuilder = builder.Services.AddAIAgent("assistant", "You are a helpful assistant.");
agentBuilder.WithEfCoreSessionStore<MyDbContext>();
多用户/多租户隔离(应用侧组合,模块不内置)
会话键默认 (agent, conversationId) 无主维度;需要隔离时用官方 IsolationKeyScopedAgentSessionStore 包装并自定 SessionIsolationKeyProvider(可读 ITenantContext / 用户标识):
public sealed class TenantSessionIsolationKeyProvider : SessionIsolationKeyProvider
{
private readonly ITenantContext _tenants;
public TenantSessionIsolationKeyProvider(ITenantContext tenants) => _tenants = tenants;
public override ValueTask<string?> GetSessionIsolationKeyAsync(CancellationToken ct)
=> ValueTask.FromResult<string?>(_tenants.TenantId > 0 ? $"t{_tenants.TenantId}" : null);
}
var agentBuilder = builder.Services.AddAIAgent("assistant", "You are a helpful assistant.");
agentBuilder.WithEfCoreSessionStore<MyDbContext>(new TenantSessionIsolationKeyProvider(...));
隔离键非空时会话键改写为 {key}::{conversationId},不同租户/用户同名会话互不可见。
聊天历史持久化(消息级)
会话级存储持久化的是会话状态(StateBag);若要让聊天消息落库(DevUI / OpenAI Responses / Conversations 端点亦生效,每次 Run 结束自动写入 ac_agent_message),需在 agent 上挂载 EfCoreChatHistoryProvider。用官方 AddAIAgent factory 重载手动绑定到 ChatHistoryProvider 槽位:
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=agents.db"));
// 1. 注册消息级聊天历史存储(EfCoreChatHistoryProvider)
builder.Services.AddEfCoreChatHistoryProvider<MyDbContext>(options =>
{
// 可选:消息完整性密钥 / 上下文压缩器 / 过滤
// options.SigningKeyProvider = () => vault.GetKey();
});
// 2. 官方 factory 重载:挂载 EfCoreChatHistoryProvider 到 ChatHistoryProvider 槽位
builder.Services.AddAIAgent("assistant", (sp, key) =>
{
var chatClient = sp.GetRequiredService<IChatClient>();
return chatClient.AsAIAgent(new ChatClientAgentOptions
{
Name = key,
ChatOptions = new ChatOptions { Instructions = "You are a helpful assistant." },
ChatHistoryProvider = sp.GetRequiredService<EfCoreChatHistoryProvider<MyDbContext>>()
});
})
.WithAITools(...); // 官方接线工具
对比:
EfCoreAgentSessionStore(会话级)在 DevUI 端点链路不被自动触发,需手动调用;EfCoreChatHistoryProvider(消息级)挂在 agent 上后由ChatClientAgent自动持久化。
会话级 + 消息级 + 记忆/RAG + 中间件组合接线见基础模块
Galosys.Foundation.Agents.AI.HostingREADME 的「注册并接线 Agent(官方AddAIAgentfactory 方式)」。
存储形态与限制
- 客户端(单进程):Sqlite + 单例
DbContext,单写进程。 - 服务端(多实例):
DbContextScoped,使用 SQL Server / Npgsql;Sqlite 不支持多实例并发写。 - 会话级
EfCoreAgentSessionStore与消息级 provider 均支持IServiceScopeFactory按操作解析,agent 生命周期 Scoped/Singleton 均安全,无 root-provider 异常。 - 消息级与会话级写入时机不同(
RunAsync前后),单写进程下无冲突。
依赖
Galosys.Foundation.Agents.AI.Hosting— MAF Hosting(AgentSessionStore/SessionIsolationKeyProvider/IsolationKeyScopedAgentSessionStore)Galosys.Foundation.Agents.AI.EntityFrameworkCore— 消息级基础模块(复用AgentSessionEntity)Galosys.Foundation.EntityFrameworkCore— GalosysDbContext<T>基类Microsoft.Agents.AI.Hosting— MAF Hosting(1.13.0-preview)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Galosys.Foundation.Agents.AI.EntityFrameworkCore (>= 26.8.13.2)
- Galosys.Foundation.Agents.AI.Hosting (>= 26.8.13.2)
- Galosys.Foundation.EntityFrameworkCore (>= 26.8.13.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.