Tableau Migration SDK 5.1.1
  • Articles
  • Code Samples
  • Python API Reference
  • C# API Reference
Show / Hide Table of Contents
  • Set Custom Migration Scoped Context

Sample: Set Custom Migration Scoped Context

This example demonstrates how to set custom context in the migration scoped dependency injection container using an initialize migration hook. This is useful when other hooks like filters are registered with dependency injection and rely on migration scoped services.

  • Python
  • C#

Custom Context Service Class


public class CustomContext
{
    public Guid CustomerId { get; set; }
}

Custom Context Service Class Dependency Injection

Learn more.

services.AddScoped<CustomContext>();

Initialize Migration Hook Class

from tableau_migration import(
    InitializeMigrationHookBase,
    IInitailizeMigrationHookResult
)

from Csharp.ExampleApplication.Hooks.InitializeMigration import CustomContext

class SetMigrationContextHook(InitializeMigrationHookBase):
    def execute(self, ctx: IInitailizeMigrationHookResult) -> IInitailizeMigrationHookResult:
        ctx.scoped_services._get_service(CustomContext)

Registration

Learn more.

plan_builder.hooks.add(SetMigrationContextHook)

Custom Context Service Class


public class CustomContext
{
    public Guid CustomerId { get; set; }
}

Custom Context Service Class Dependency Injection

Learn more.

services.AddScoped<CustomContext>();

Initialize Migration Hook Class


internal class SetMigrationContextHook : IInitializeMigrationHook
{
    public Task<IInitializeMigrationHookResult?> ExecuteAsync(IInitializeMigrationHookResult ctx, CancellationToken cancel)
    {
        var customContext = ctx.ScopedServices.GetRequiredService<CustomContext>();
        customContext.CustomerId = Guid.NewGuid();

        return Task.FromResult<IInitializeMigrationHookResult?>(ctx);
    }
}

Registration

Learn more.

_planBuilder.Hooks.Add<SetMigrationContextHook>();

Dependency Injection

Learn more.

services.AddScoped<SetMigrationContextHook>();
  • Edit this page
In this article