Skip to content

API Host IScriptSourceResolver

Liu.Yandong.Hanks edited this page Aug 19, 2026 · 3 revisions

IScriptSourceResolver

自定义脚本加载扩展点

Custom script-loading extension point

.NET Host API 目录

.NET Host API Directory

概述

Overview

实现 IScriptSourceResolver 可以从数据库、对象存储、嵌入资源或虚拟目录加载脚本。必须使用稳定的规范化引用,并在解析相对 import/include 时保留 importer 上下文。

Implement IScriptSourceResolver to load scripts from a database, object storage, embedded resources, or virtual directories. Use stable normalized references and retain importer context when resolving relative imports/includes.

Root

Returns

此 Resolver 管理的规范化根。

Normalized root managed by this resolver.

public string Root => "mem://tenant-a/";

ResolveAsync(importer, requestedPath, context, [cancellationToken])

Parameters:

  • importer
    当前导入者或 null(入口)。

    Importing source or null (entry).

  • requestedPath
    原始请求路径。

    Raw requested path.

  • context
    解析上下文。

    Resolution context.

  • cancellationToken
    可选取消令牌。

    Optional cancellation token.

Returns

稳定 ScriptSourceReferencenull

Stable ScriptSourceReference or null.

public ValueTask<ScriptSourceReference?> ResolveAsync(
    ScriptSourceReference? importer,
    string requestedPath,
    ScriptResolveContext context,
    CancellationToken cancellationToken = default)
{
    var fullPath = importer is null ? Root + requestedPath : Root + requestedPath;
    return ValueTask.FromResult<ScriptSourceReference?>(new ScriptSourceReference(Root, fullPath));
}

GetSourceAsync(reference, [cancellationToken])

Parameters:

  • reference
    已解析引用。

    Resolved reference.

  • cancellationToken
    可选取消令牌。

    Optional cancellation token.

Returns

对应 ScriptSource

Matching ScriptSource.

public ValueTask<ScriptSource> GetSourceAsync(
    ScriptSourceReference reference,
    CancellationToken cancellationToken = default) =>
    ValueTask.FromResult<ScriptSource>(new MemorySource(Root, reference.FullPath, "@module(MAIN);"));

GetAllSourcesAsync(query, [cancellationToken])

Parameters:

  • query
    枚举过滤条件。

    Enumeration filter.

  • cancellationToken
    可选取消令牌。

    Optional cancellation token.

Returns

BuildAsync() 可见的异步源码序列。

Async source sequence visible to BuildAsync().

public async IAsyncEnumerable<ScriptSource> GetAllSourcesAsync(
    ScriptSourceQuery query,
    [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
{
    yield return new MemorySource(Root, Root + "main.as", "@module(MAIN);");
    await Task.CompletedTask;
}

示例只展示接口形状;生产 Resolver 必须规范化路径、正确处理取消并确保 GetSourceAsync 根据 reference.BaseDirectory 路由。

the examples show interface shape only; a production resolver must normalize paths, honor cancellation, and route GetSourceAsync by reference.BaseDirectory.

Clone this wiki locally