refactor (env) : 修复了运行环境构建顺序,以及同步上下文在内置ioc中传递的问题

This commit is contained in:
fengjiayi
2026-01-27 17:24:19 +08:00
parent f335439732
commit dddc3b3b53
14 changed files with 215 additions and 123 deletions

View File

@@ -941,6 +941,11 @@ namespace Serein.Library.Api
/// </summary>
IFlowControl FlowControl { get; }
/// <summary>
/// 流程依赖类库接口
/// </summary>
IFlowLibraryService FlowLibraryService { get; }
/// <summary>
/// 流程事件接口
/// </summary>
@@ -1122,6 +1127,13 @@ namespace Serein.Library.Api
/// </summary>
void ExitRemoteEnv();
*/
/// <summary>
/// 启动远程服务
/// </summary>
/// <returns></returns>
Task StartRemoteServerAsync(int port = 7525);
/// <summary>
/// (用于远程)通知节点属性变更
/// </summary>

View File

@@ -0,0 +1,86 @@
using Serein.Library;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace Serein.Library.Api
{
/// <summary>
/// 流程依赖程序集管理
/// </summary>
public interface IFlowLibraryService
{
/// <summary>
/// 是否加载了基础依赖
/// </summary>
bool IsLoadedBaseLibrary { get; }
/// <summary>
/// 加载基础依赖
/// </summary>
/// <returns></returns>
FlowLibraryInfo LoadBaseLibrary();
/// <summary>
/// 获取已加载的方法信息
/// </summary>
/// <returns></returns>
List<FlowLibraryInfo> GetAllLibraryInfo();
/// <summary>
/// 加载指定依赖
/// </summary>
/// <param name="libraryfilePath"></param>
/// <returns></returns>
FlowLibraryInfo? LoadFlowLibrary(string libraryfilePath);
/// <summary>
/// 卸载程序集
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
bool UnloadLibrary(string assemblyName);
/// <summary>
/// 获取委托
/// </summary>
/// <param name="assemblyName"></param>
/// <param name="methodName"></param>
/// <param name="dd"></param>
/// <returns></returns>
bool TryGetDelegateDetails(string assemblyName, string methodName, out DelegateDetails dd);
/// <summary>
/// 获取方法描述
/// </summary>
/// <param name="assemblyName"></param>
/// <param name="methodName"></param>
/// <param name="md"></param>
/// <returns></returns>
bool TryGetMethodDetails(string assemblyName, string methodName, out MethodDetails md);
/// <summary>
/// 获取反射方法信息
/// </summary>
/// <param name="assemblyName"></param>
/// <param name="methodName"></param>
/// <param name="methodInfo"></param>
/// <returns></returns>
bool TryGetMethodInfo(string assemblyName, string methodName, out MethodInfo methodInfo);
/// <summary>
/// 获取依赖程序集中的类型
/// </summary>
/// <param name="fullName"></param>
/// <param name="type"></param>
/// <returns></returns>
bool TryGetType(string fullName, out Type? type);
/// <summary>
/// 获取依赖程序集中的类型
/// </summary>
/// <param name="fullName"></param>
/// <returns></returns>
Type? GetType(string fullName);
/// <summary>
/// 获取某个节点类型对应的方法描述
/// </summary>
/// <param name="nodeType"></param>
/// <returns></returns>
List<MethodDetails> GetMdsOnFlowStart(NodeType nodeType);
}
}

View File

@@ -51,6 +51,8 @@ namespace Serein.Library
/// <inheritdoc/>
public UIContextOperation UIContextOperation => throw new NotImplementedException();
public IFlowLibraryService FlowLibraryService => throw new NotImplementedException();
/* public Task<(bool, RemoteMsgUtil)> ConnectRemoteEnv(string addres, int port, string token)
{
throw new NotImplementedException();

View File

@@ -89,7 +89,8 @@
<ItemGroup>
<!--<ProjectReference Include="..\Serein.Library.MyGenerator\Serein.Library.NodeGenerator.csproj " OutputItemType="Analyzer" />-->
<!-- ReferenceOutputAssembly="false" -->
<ProjectReference Include="..\Serein.Library.MyGenerator\Serein.Library.NodeGenerator.csproj " OutputItemType="Analyzer" />
<!--<ProjectReference Include="..\Serein.Library.MyGenerator\Serein.Library.NodeGenerator.csproj " OutputItemType="Analyzer" />-->
<ProjectReference Include="..\Serein.Library.NodeGenerator\Serein.Library.NodeGenerator.csproj" OutputItemType="Analyzer"/>
</ItemGroup>
</Project>

View File

@@ -17,7 +17,7 @@ namespace Serein.Library.Utils
public class UIContextOperation
{
private SynchronizationContext context;
private readonly Func<SynchronizationContext> getUiContext = null;
public Func<SynchronizationContext> GetUiContext = null;
static UIContextOperation()
{
@@ -55,7 +55,7 @@ namespace Serein.Library.Utils
[SereinIOCCtor(IsIgnore = true)]
public UIContextOperation(Func<SynchronizationContext> getUiContext)
{
this.getUiContext = getUiContext;
this.GetUiContext = getUiContext;
}
/// <summary>
@@ -65,9 +65,14 @@ namespace Serein.Library.Utils
/// <param name="onException">异常发生时的回调</param>
public void Invoke(Action uiAction, Action<Exception> onException = null)
{
if(context is null && getUiContext != null)
if(context is null && GetUiContext != null)
{
context = getUiContext.Invoke();
while (GetUiContext is null)
{
continue;
}
context = GetUiContext.Invoke();
}
context?.Post(state =>
{
@@ -92,9 +97,13 @@ namespace Serein.Library.Utils
/// <returns></returns>
public Task InvokeAsync(Action uiAction, Action<Exception> onException = null)
{
if (context is null && getUiContext != null)
if (context is null )
{
context = getUiContext.Invoke();
while (GetUiContext is null)
{
continue;
}
context = GetUiContext.Invoke();
}
var tcs = new TaskCompletionSource<bool>();

View File

@@ -21,25 +21,23 @@ namespace Serein.NodeFlow.Env
{
private readonly IFlowEnvironment flowEnvironment;
private readonly IFlowEnvironmentEvent flowEnvironmentEvent;
private readonly FlowLibraryService flowLibraryService;
private readonly IFlowLibraryService flowLibraryService;
private readonly FlowOperationService flowOperationService;
private readonly FlowModelService flowModelService;
private readonly UIContextOperation uiContextOperation;
private readonly Lazy<UIContextOperation> uiContextOperation;
public FlowControl(IFlowEnvironment flowEnvironment,
IFlowEnvironmentEvent flowEnvironmentEvent,
FlowLibraryService flowLibraryService,
IFlowLibraryService flowLibraryService,
FlowOperationService flowOperationService,
FlowModelService flowModelService,
UIContextOperation uiContextOperation)
FlowModelService flowModelService)
{
this.flowEnvironment = flowEnvironment;
this.flowEnvironmentEvent = flowEnvironmentEvent;
this.flowLibraryService = flowLibraryService;
this.flowOperationService = flowOperationService;
this.flowModelService = flowModelService;
this.uiContextOperation = uiContextOperation;
uiContextOperation = new Lazy<UIContextOperation>(() => flowEnvironment.IOC.Get<UIContextOperation>());
contexts = new ObjectPool<IFlowContext>(() => new FlowContext(flowEnvironment), context => context.Reset());
flowTaskOptions = new FlowWorkOptions
{
@@ -340,7 +338,7 @@ namespace Serein.NodeFlow.Env
{
flowWorkManagement.Exit();
}
uiContextOperation?.Invoke(() => flowEnvironmentEvent.OnFlowRunComplete(new FlowEventArgs()));
uiContextOperation.Value.Invoke(() => flowEnvironmentEvent.OnFlowRunComplete(new FlowEventArgs()));
IOC.Reset();
GC.Collect();
return Task.FromResult(true);

View File

@@ -22,7 +22,7 @@ namespace Serein.NodeFlow.Env
{
public FlowEdit(IFlowEnvironment flowEnvironment,
IFlowEnvironmentEvent flowEnvironmentEvent,
FlowLibraryService flowLibraryManagement,
IFlowLibraryService flowLibraryManagement,
FlowOperationService flowOperationService,
FlowModelService flowModelService,
UIContextOperation UIContextOperation,
@@ -45,7 +45,7 @@ namespace Serein.NodeFlow.Env
private readonly IFlowEnvironment flowEnvironment;
private readonly IFlowEnvironmentEvent flowEnvironmentEvent;
private readonly FlowLibraryService flowLibraryManagement;
private readonly IFlowLibraryService flowLibraryManagement;
private readonly FlowOperationService flowOperationService;
private readonly FlowModelService flowModelService;

View File

@@ -49,11 +49,46 @@ namespace Serein.NodeFlow.Env
public FlowEnvironment()
{
ISereinIOC ioc = new SereinIOC();
ioc.Register<ISereinIOC>(()=> ioc) // IOC容器接口
ioc.Register<ISereinIOC>(() => ioc) // IOC容器接口
.Register<IFlowEnvironment>(() => this) // 流程环境接口
.Register<IFlowEnvironmentEvent, FlowEnvironmentEvent>() // 流程环境事件接口
.Register<IFlowEdit, FlowEdit>() // 流程编辑接口
.Register<IFlowControl, FlowControl>() // 流程控制接口
.Register<IFlowLibraryService, FlowLibraryService>() // 流程库服务
.Register<LocalFlowEnvironment>() // 本地环境
.Register<FlowModelService>() // 节点/画布模型服务
.Register<FlowCoreGenerateService>() // 代码生成
.Register<FlowOperationService>() // 流程操作
.Register<NodeMVVMService>() // 节点MVVM服务
.Build();
// 设置JSON解析器
if (JsonHelper.Provider is null)
{
JsonHelper.UseJsonProvider(new NewtonsoftJsonProvider());
}
// 默认使用本地环境
localFlowEnvironment = ioc.Get<LocalFlowEnvironment>();
currentFlowEnvironmentEvent = ioc.Get<IFlowEnvironmentEvent>();
currentFlowEnvironment = localFlowEnvironment;
SereinEnv.SetEnv(localFlowEnvironment);
}
/// <summary>
/// 提供上下文操作进行调用
/// </summary>
/// <param name="operation"></param>
public FlowEnvironment(UIContextOperation operation)
{
ISereinIOC ioc = new SereinIOC();
ioc.Register<ISereinIOC>(() => ioc) // IOC容器接口
.Register<IFlowEnvironment>(() => this) // 流程环境接口
.Register<IFlowEnvironmentEvent, FlowEnvironmentEvent>() // 流程环境事件接口
.Register<IFlowLibraryService, FlowLibraryService>()
.Register<UIContextOperation>(() => operation) // 流程环境接口
.Register<IFlowEdit, FlowEdit>() // 流程编辑接口
.Register<IFlowControl, FlowControl>() // 流程控制接口
.Register<LocalFlowEnvironment>() // 本地环境
.Register<FlowModelService>() // 节点/画布模型服务
.Register<FlowLibraryService>() // 流程库服务
@@ -67,56 +102,19 @@ namespace Serein.NodeFlow.Env
{
JsonHelper.UseJsonProvider(new NewtonsoftJsonProvider());
}
// 默认使用本地环境
currentFlowEnvironment = ioc.Get<LocalFlowEnvironment>();
localFlowEnvironment = ioc.Get<LocalFlowEnvironment>();
currentFlowEnvironmentEvent = ioc.Get<IFlowEnvironmentEvent>();
SereinEnv.SetEnv(currentFlowEnvironment);
currentFlowEnvironment = localFlowEnvironment;
SereinEnv.SetEnv(localFlowEnvironment);
}
/// <summary>
/// 提供上下文操作进行调用
/// 管理当前环境
/// </summary>
/// <param name="operation"></param>
public FlowEnvironment(UIContextOperation operation)
{
ISereinIOC ioc = new SereinIOC();
ioc.Register<ISereinIOC>(()=> ioc) // IOC容器接口
.Register<UIContextOperation>(() => operation) // 流程环境接口
.Register<IFlowEnvironment>(() => this) // 流程环境接口
.Register<IFlowEnvironmentEvent, FlowEnvironmentEvent>() // 流程环境事件接口
.Register<IFlowEdit, FlowEdit>() // 流程编辑接口
.Register<IFlowControl, FlowControl>() // 流程控制接口
.Register<LocalFlowEnvironment>() // 本地环境
.Register<FlowModelService>() // 节点/画布模型服务
.Register<FlowLibraryService>() // 流程库服务
.Register<FlowCoreGenerateService>() // 代码生成
.Register<FlowOperationService>() // 流程操作
.Register<NodeMVVMService>() // 节点MVVM服务
.Build();
// 设置JSON解析器
if (JsonHelper.Provider is null)
{
JsonHelper.UseJsonProvider(new NewtonsoftJsonProvider());
}
// 默认使用本地环境
currentFlowEnvironment = ioc.Get<LocalFlowEnvironment>();
currentFlowEnvironmentEvent = ioc.Get<IFlowEnvironmentEvent>();
SereinEnv.SetEnv(currentFlowEnvironment);
}
/*
/// <summary>
/// 本地环境事件
/// </summary>
private readonly IFlowEnvironmentEvent flowEnvironmentEvent;
/// <summary>
/// 远程环境事件
/// </summary>
private IFlowEnvironmentEvent remoteFlowEnvironmentEvent;
*/
private LocalFlowEnvironment localFlowEnvironment;
/// <summary>
/// 管理当前环境
@@ -129,8 +127,6 @@ namespace Serein.NodeFlow.Env
/// </summary>
private IFlowEnvironmentEvent currentFlowEnvironmentEvent;
private int _loadingProjectFlag = 0; // 使用原子自增代替锁
/// <summary>
/// 传入false时将停止数据通知。传入true时
@@ -159,6 +155,9 @@ namespace Serein.NodeFlow.Env
/// <inheritdoc/>
public IFlowControl FlowControl => currentFlowEnvironment.FlowControl;
/// <inheritdoc/>
public IFlowLibraryService FlowLibraryService => currentFlowEnvironment.FlowLibraryService;
/// <inheritdoc/>
public ISereinIOC IOC => currentFlowEnvironment.IOC;
@@ -259,12 +258,6 @@ namespace Serein.NodeFlow.Env
SetProjectLoadingFlag(true);
}
/* /// <inheritdoc/>
public void MonitorObjectNotification(string nodeGuid, object monitorData, MonitorObjectEventArgs.ObjSourceType sourceType)
{
currentFlowEnvironment.FlowControl.MonitorObjectNotification(nodeGuid, monitorData, sourceType);
}*/
/// <inheritdoc/>
public bool TryUnloadLibrary(string assemblyName)
{
@@ -312,43 +305,14 @@ namespace Serein.NodeFlow.Env
#endif
#endregion
/*
/// <inheritdoc/>
public async Task<bool> StartFlowAsync(string[] canvasGuids)
public Task StartRemoteServerAsync(int port = 7525)
{
return await currentFlowEnvironment.FlowControl.StartFlowAsync(canvasGuids);
throw new NotImplementedException();
}
/// <inheritdoc/>
public async Task<TResult> StartFlowAsync<TResult>(string startNodeGuid)
{
return await currentFlowEnvironment.FlowControl.StartFlowAsync<TResult>(startNodeGuid);
}*/
/* /// <inheritdoc/>
public async Task StartRemoteServerAsync(int port = 7525)
{
await currentFlowEnvironment.StartRemoteServerAsync(port);
}
/// <inheritdoc/>
public void StopRemoteServer()
{
currentFlowEnvironment.StopRemoteServer();
}*/
/*
/// <inheritdoc/>
public void TerminateFlipflopNode(string nodeGuid)
{
currentFlowEnvironment.FlowControl.TerminateFlipflopNode(nodeGuid);
}
/// <inheritdoc/>
public void TriggerInterrupt(string nodeGuid, string expression, InterruptTriggerEventArgs.InterruptTriggerType type)
{
currentFlowEnvironment.FlowControl.TriggerInterrupt(nodeGuid, expression, type);
}*/
/// <inheritdoc/>
public void SetUIContextOperation(UIContextOperation uiContextOperation)
@@ -356,12 +320,6 @@ namespace Serein.NodeFlow.Env
currentFlowEnvironment.SetUIContextOperation(uiContextOperation);
}
/*
/// <inheritdoc/>
public void UseExternalIOC(ISereinIOC ioc)
{
currentFlowEnvironment.FlowControl.UseExternalIOC(ioc);
}*/
/// <inheritdoc/>
public bool TryGetNodeModel(string nodeGuid, out IFlowNode nodeModel)
@@ -408,6 +366,7 @@ namespace Serein.NodeFlow.Env
{
currentFlowEnvironment.LoadAllNativeLibraryOfRuning(path,isRecurrence);
}
#endregion

View File

@@ -18,14 +18,14 @@ namespace Serein.NodeFlow.Services
public class FlowCoreGenerateService
{
private readonly FlowModelService flowModelService;
private readonly FlowLibraryService flowLibraryService;
private readonly IFlowLibraryService flowLibraryService;
/// <summary>
/// 流程代码生成服务
/// </summary>
/// <param name="flowModelService"></param>
/// <param name="flowLibraryService"></param>
public FlowCoreGenerateService(FlowModelService flowModelService ,FlowLibraryService flowLibraryService )
public FlowCoreGenerateService(FlowModelService flowModelService ,IFlowLibraryService flowLibraryService )
{
this.flowModelService = flowModelService;
this.flowLibraryService = flowLibraryService;

View File

@@ -9,10 +9,11 @@ using System.Xml.Linq;
namespace Serein.NodeFlow.Services
{
/// <summary>
/// 管理加载在运行环境中的外部程序集
/// </summary>
public class FlowLibraryService
public class FlowLibraryService : IFlowLibraryService
{
/// <summary>
/// 是否加载过基础依赖
@@ -49,7 +50,7 @@ namespace Serein.NodeFlow.Services
private bool CheckBaseLibrary(string libraryfilePath, out string baseLibraryPath)
{
var dir = Path.GetDirectoryName(libraryfilePath); // 获取目录路径
ArgumentNullException.ThrowIfNullOrWhiteSpace(dir);
ArgumentException.ThrowIfNullOrWhiteSpace(dir);
var sereinFlowBaseLibraryPath = Path.Combine(dir, SereinBaseLibrary);
if (!Path.Exists(sereinFlowBaseLibraryPath))
{

View File

@@ -7,19 +7,19 @@ using System.Diagnostics.CodeAnalysis;
namespace Serein.NodeFlow.Services
{
/// <summary>
/// 流程模型服务
/// 流程画布/节点数据实体服务
/// </summary>
public class FlowModelService
{
private readonly IFlowEnvironment environment;
private readonly FlowLibraryService flowLibraryService;
private readonly IFlowLibraryService flowLibraryService;
/// <summary>
/// 流程模型服务构造函数
/// </summary>
/// <param name="environment"></param>
/// <param name="flowLibraryService"></param>
public FlowModelService(IFlowEnvironment environment, FlowLibraryService flowLibraryService)
public FlowModelService(IFlowEnvironment environment, IFlowLibraryService flowLibraryService)
{
this.environment = environment;
this.flowLibraryService = flowLibraryService;

View File

@@ -8,6 +8,9 @@ using System.Threading.Tasks;
namespace Serein.NodeFlow.Services
{
/// <summary>
/// 流程操作
/// </summary>
internal class FlowOperationService
{
private readonly ISereinIOC sereinIOC;

View File

@@ -65,6 +65,7 @@ namespace Serein.Workbench
#endif
var projectService = App.GetService<FlowProjectService>();
if (e.Args.Length == 1)
{
string filePath = e.Args[0];

View File

@@ -54,28 +54,48 @@ namespace Serein.Workbench
/// <param name="collection"></param>
public static void AddFlowServices(this IServiceCollection collection)
{
#region
JsonHelper.UseJsonProvider(new NewtonsoftJsonProvider());
Func<SynchronizationContext>? getSyncContext = null;
UIContextOperation? uIContextOperation = new(getSyncContext); // 封装一个调用UI线程的工具类
IFlowEnvironment flowEnvironment = new FlowEnvironment(uIContextOperation);
Dispatcher.CurrentDispatcher.Invoke(() =>
{
var uiContext = SynchronizationContext.Current; // 在UI线程上获取UI线程上下文信息
if (uiContext is not null)
if (SynchronizationContext.Current is { } uiContext)
{
// 在UI线程上获取UI线程上下文信息
getSyncContext = () => uiContext;
flowEnvironment.UIContextOperation.GetUiContext = () => uiContext;
}
});
UIContextOperation? uIContextOperation = new (getSyncContext); // 封装一个调用UI线程的工具类
IFlowEnvironment flowEnvironment = new FlowEnvironment();
flowEnvironment.SetUIContextOperation(uIContextOperation);
collection.AddSingleton<UIContextOperation>(uIContextOperation); // 注册UI线程操作上下文
collection.AddSingleton<IFlowEnvironment>(flowEnvironment); // 注册运行环境
collection.AddSingleton<IFlowEnvironmentEvent>(flowEnvironment.Event); // 注册运行环境事件
collection.AddSingleton<IFlowEEForwardingService, FlowEEForwardingService>(); // 注册工作台环境事件
#endregion
//#region 创建实例
//Func<SynchronizationContext>? getSyncContext = null;
//Dispatcher.CurrentDispatcher.Invoke(() =>
//{
// var uiContext = SynchronizationContext.Current; // 在UI线程上获取UI线程上下文信息
// if (uiContext is not null)
// {
// getSyncContext = () => uiContext;
// }
//});
//UIContextOperation? uIContextOperation = new (getSyncContext); // 封装一个调用UI线程的工具类
//IFlowEnvironment flowEnvironment = new FlowEnvironment();
//flowEnvironment.SetUIContextOperation(uIContextOperation);
//collection.AddSingleton<UIContextOperation>(uIContextOperation); // 注册UI线程操作上下文
//collection.AddSingleton<IFlowEnvironment>(flowEnvironment); // 注册运行环境
//collection.AddSingleton<IFlowEnvironmentEvent>(flowEnvironment.Event); // 注册运行环境事件
//#endregion
}
}