[]
获取全局变量的值
修改全局变量的值
应用程序服务会在活字格启动时注册到Asp.net 的服务容器中,使用时只需要通过GetService方法即可获取。
using GrapeCity.Forguncy.ServerApi;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
namespace MyPlugin.Server
{
internal class MyPluginMiddleware
{
private readonly RequestDelegate _next;
public MyPluginMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Path.Value == "/MyPluginMiddleware")
{
var calcService = context.RequestServices.GetService<ICalcService>();
// 修改全局变量的值
await calcService.SetGlobalVariableValueAsync("MyVar", "test");
// 获取全局变量的值
var appKey = await calcService.GetGlobalVariableValueAsync("MyAppKey");
await context.Response.WriteAsync(appKey);
return;
}
await _next(context);
}
}
}
IApplicationInformationService 同样可以在服务端命令中使用,使用方法和中间件类似。
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.ServerApi;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
namespace MyPlugin
{
public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
{
public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
{
var calcService = dataContext.ServiceProvider.GetService<ICalcService>();
// 修改全局变量的值
await calcService.SetGlobalVariableValueAsync("MyVar", "test");
// 获取全局变量的值
var appKey = await calcService.GetGlobalVariableValueAsync("MyAppKey");
return new ExecuteResult()
{
Message = "Application Key: " + appKey
};
}
public override CommandScope GetCommandScope()
{
return CommandScope.ExecutableInServer;
}
}
}
大于等于活字格9.0.100.0版本。