admin管理员组

文章数量:1355535

I'm trying to upload a Symantic Model in a PowreBi Workspace, using the c# PowerBiClient. I'm using the following code:

public async Task<Import> UpsertDataSet(Guid groupId, string path, string displayName)
{
    var pbiClient = await GetPowerBIClient() ?? throw new PowerBiException(PowerBiClientIsNull);

    using var pbixStream = new FileStream(path, FileMode.Open, FileAccess.Read);
    var import = await pbiClient.Imports.PostImportWithFileAsyncInGroup(groupId, pbixStream, displayName, nameConflict: ImportConflictHandlerMode.CreateOrOverwrite);

    return await pbiClient.Imports.GetImportInGroupAsync(groupId, import.Id);
}

There are cases where the import.Datasets are not filled while the Model is uploaded.

I added and extra GetImportInGroupAsync to check if the model is updated. How come that the result is empty which the Model is updated

I'm trying to upload a Symantic Model in a PowreBi Workspace, using the c# PowerBiClient. I'm using the following code:

public async Task<Import> UpsertDataSet(Guid groupId, string path, string displayName)
{
    var pbiClient = await GetPowerBIClient() ?? throw new PowerBiException(PowerBiClientIsNull);

    using var pbixStream = new FileStream(path, FileMode.Open, FileAccess.Read);
    var import = await pbiClient.Imports.PostImportWithFileAsyncInGroup(groupId, pbixStream, displayName, nameConflict: ImportConflictHandlerMode.CreateOrOverwrite);

    return await pbiClient.Imports.GetImportInGroupAsync(groupId, import.Id);
}

There are cases where the import.Datasets are not filled while the Model is uploaded.

I added and extra GetImportInGroupAsync to check if the model is updated. How come that the result is empty which the Model is updated

Share Improve this question asked Mar 31 at 14:26 BartBart 3253 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Apperently the PostImportWithFileAsyncInGroup returns directly and there is a need for polling the importstate

public async Task<Import> UpsertDataSet(Guid groupId, string path, string displayName)
{
    var pbiClient = await GetPowerBIClient() ?? throw new PowerBiException(PowerBiClientIsNull);

    using var pbixStream = new FileStream(path, FileMode.Open, FileAccess.Read);
    var import = await pbiClient.Imports.PostImportWithFileAsyncInGroup(groupId, pbixStream,
        datasetDisplayName: displayName,
        nameConflict: ImportConflictHandlerMode.CreateOrOverwrite,
        overrideModelLabel: true,
        overrideReportLabel: true);

    var importId = import.Id;
    var retry = 0;

    while (retry++ < MaxRetries)
    {
        var importState = ParseImportState(import.ImportState);

        if (importState is ImportState.Succeeded)
        {
            logger.LogInformation("Dataset updated: - {Name} (Id: {Id})", import.Name, import.Id);
            return import;
        }

        if (importState is ImportState.Failed)
        {
            throw new PowerBiException(ImportFailed);
        }

        await Task.Delay(500);

        import = await pbiClient.Imports.GetImportInGroupAsync(groupId, importId);
    }

    throw new PowerBiException(ImportFailed);
}
private static ImportState ParseImportState(string state)
{
    return state switch
    {
        "Succeeded" => ImportState.Succeeded,
        "Failed" => ImportState.Failed,
        "Publishing" => ImportState.Publishing,
        _ => ImportState.Unknown
    };
}

本文标签: