共用方式為


Sessions Python 插件遷移指南 - 2025 年 5 月

SessionsPythonPlugin已更新為使用最新版的 Azure 程式代碼解釋器動態會話 API(2024-10-02-preview)。 新的 API 引進了重大變更,這些變更會反映在外掛程式的公用 API 介面中。

此移轉指南將協助您將現有的程式代碼移轉至最新版本的外掛程式。

外掛程式初始化

外掛程式建構函式簽章已變更為接受 CancellationToken 做為函式的 authTokenProvider 自變數。 這項變更可讓您視需要取消令牌產生程式:

// Before
static async Task<string> GetAuthTokenAsync()
{
    return tokenProvider.GetToken();
}

// After
static async Task<string> GetAuthTokenAsync(CancellationToken ct)
{
    return tokenProvider.GetToken(ct);
}

var plugin = new SessionsPythonPlugin(settings, httpClientFactory, GetAuthTokenAsync);

UploadFileAsync 方法

方法 UploadFileAsync 簽章已變更為更能代表參數的目的:

// Before
string remoteFilePath = "your_file.txt";
string? localFilePath = "C:\documents\your_file.txt";

await plugin.UploadFileAsync(remoteFilePath: remoteFilePath, localFilePath: localFilePath);

// After
string remoteFileName = "your_file.txt";
string localFilePath = "C:\documents\your_file.txt";

await plugin.UploadFileAsync(remoteFileName: remoteFileName, localFilePath: localFilePath);

DownloadFileAsync 方法

同樣地, DownloadFileAsync 方法簽章已變更為更能代表參數的目的:

// Before
string remoteFilePath = "your_file.txt";
await plugin.DownloadFileAsync(remoteFilePath: remoteFilePath);

// After
string remoteFileName = "your_file.txt";
await plugin.DownloadFileAsync(remoteFileName: remoteFileName);

ExecuteCodeAsync 方法

方法 ExecuteCodeAsync 簽章已變更為提供結構化的方式來處理執行結果:

// Before
string result = await plugin.ExecuteCodeAsync(code: "print('Hello, world!')");

// After
SessionsPythonCodeExecutionResult result = await plugin.ExecuteCodeAsync(code: "print('Hello, world!')");
string status = result.Status;
string? executionResult = result.Result?.ExecutionResult;
string? stdout = result.Result?.StdOut;
string? stderr = result.Result?.StdErr;

「SessionsRemoteFileMetadata」類別

SessionsRemoteFileMetadataListFilesAsync 方法所使用的UploadFileAsync模型類別已更新,以更妥善地表示遠端檔案和目錄的元數據:

// Before
SessionsRemoteFileMetadata file = await plugin.UploadFileAsync(...);
string fileName = file.Filename;
long fileSize = file.Size;
DateTime? lastModified = file.LastModifiedTime;

// After
SessionsRemoteFileMetadata file = await plugin.UploadFileAsync(...);
string name = file.Name;
long? size = file.SizeInBytes;
DateTime lastModified = file.LastModifiedAt;