TableClient GetOrCreateTableClient()

in src/WebJobs.Extensions.OpenAI/Assistants/AssistantService.cs [531:577]


    TableClient GetOrCreateTableClient(string? chatStorageConnectionSetting, string? collectionName)
    {
        if (this.tableClient is not null)
        {
            return this.tableClient;
        }

        string connectionStringName = chatStorageConnectionSetting ?? string.Empty;
        IConfigurationSection tableConfigSection = this.configuration.GetSection(connectionStringName);
        string storageAccountUri = string.Empty;
        if (tableConfigSection.Exists())
        {
            storageAccountUri = tableConfigSection["tableServiceUri"];
        }

        // Check if URI for table storage is present
        if (!string.IsNullOrEmpty(storageAccountUri))
        {
            this.logger.LogInformation("Using Managed Identity");

            // Create an instance of TablesBindingOptions and set its properties
            TableBindingOptions tableOptions = new()
            {
                ServiceUri = new Uri(storageAccountUri),
                Credential = this.azureComponentFactory.CreateTokenCredential(tableConfigSection)
            };

            // Now call CreateClient without any arguments
            this.tableServiceClient = tableOptions.CreateClient();

        }
        else
        {
            // Else, will use the connection string
            connectionStringName = chatStorageConnectionSetting ?? DefaultChatStorage;
            string connectionString = this.configuration.GetValue<string>(connectionStringName);
            
            this.logger.LogInformation("using connection string for table service client");

            this.tableServiceClient = new TableServiceClient(connectionString);
        }

        this.logger.LogInformation("Using {CollectionName} for table storage collection name", collectionName);
        this.tableClient = this.tableServiceClient.GetTableClient(collectionName);

        return this.tableClient;
    }