Custom Data Converter (Encryption) With Dependency Injection Worker

I was reading through: https://docs.temporal.io/develop/dotnet/data-encryption and want to implement custom encryption in our workflows, we are currently using AddHostedTemporalWorker to allow for DI but cannot see how we can add our own custom Data Converter like you can in the samples which all use TemporalClient.ConnectAsync.

I hope this isn’t too obvious of a question!

You can use the .NET options builder. So it’d be something like

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(ctx =>
        ctx.
            // Add your codec to the container
            AddSingleton<MyEncryptionCodec>().
            // Add the worker
            AddHostedTemporalWorker(
                clientTargetHost: "localhost:7233",
                clientNamespace: "default",
                taskQueue: "my-task-queue").
            // Configure options with codec injected
            ConfigureOptions().Configure<MyEncryptionCodec>((opts, codec) =>
                opts.ClientOptions!.DataConverter =
                    DataConverter.Default with { PayloadCodec = codec }));

Didn’t test this code specifically (just typed here in forum), but that’s the idea how to get injected items into the client options using the .NET OptionsBuilder.