Elsa Core
Elsa Core 之 Tenant
Tenant 租户,用于Server API中,
样例:获取定义列表接口源码
public async Task<ActionResult<PagedList<WorkflowDefinitionSummaryModel>>> Handle(
[FromQuery] string? ids,
int? page = default,
int? pageSize = default,
VersionOptions? version = default,
CancellationToken cancellationToken = default)
{
var tenantId = await _tenantAccessor.GetTenantIdAsync(cancellationToken);
version ??= VersionOptions.Latest;
var specification = GetSpecification(ids, version.Value).And(new TenantSpecification<WorkflowDefinition>(tenantId));
var totalCount = await _workflowDefinitionStore.CountAsync(specification, cancellationToken);
var paging = page == null || pageSize == null ? default : Paging.Page(page.Value, pageSize.Value);
var items = await _workflowDefinitionStore.FindManyAsync(specification, paging: paging, cancellationToken: cancellationToken);
var summaries = _mapper.Map<IList<WorkflowDefinitionSummaryModel>>(items);
var pagedList = new PagedList<WorkflowDefinitionSummaryModel>(summaries, page, pageSize, totalCount);
return Json(pagedList, SerializationHelper.GetSettingsForWorkflowDefinition());
}
如上可见,源码中通过_tenantAccessor.GetTenantIdAsync获得TenantId
所以可以通过自定义自己的ITenantAccessor,来对接自己的租户
1、自定义TenantAccessor
public class CustomTenantAccessor : ITenantAccessor
{
private readonly IHttpContextAccessor _accessor;
public CustomTenantAccessor(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
public Task<string> GetTenantIdAsync(CancellationToken cancellationToken = default)
{
//You can customize the data
var httpContext = _accessor.HttpContext;
var tenantId = httpContext.Request.Headers["x-tenant"].ToString();
// Or you can get tenantid from claim
//var tenantId = httpContext.User.FindFirstValue("x-tenant");
return Task.FromResult(tenantId);
}
}
2、使用TenantAccessor
services.AddHttpContextAccessor();
services
.AddElsa(options => options
.UseEntityFrameworkPersistence(ef => ef.UseSqlite())
.AddConsoleActivities()
.AddActivitiesFrom<Startup>()
.AddCustomTenantAccessor<CustomTenantAccessor>()
);
关键字: elsa