Skip to content

[BUG] AddSearchClient(IConfiguration) doesn't add an instance to DI Container #47843

Open
@bh3605

Description

@bh3605

Library name and version

Microsoft.Extensions.Azure

Describe the bug

An instance of SearchClient should be registered with dependency injection when using the Microsoft.Azure.Extenstions library to register the SearchClient via AddAzureClients()

Expected behavior

An instance of SearchClient is registered in dependency injection container.

Actual behavior

[2025-01-15T14:53:35.675Z] Function 'SearchEndpoint', Invocation id 'cd5a6130-359e-4583-a966-8883a15e7777': An exception was thrown by the invocation.
[2025-01-15T14:53:35.676Z] Result: Function 'SearchEndpoint', Invocation id 'cd5a6130-359e-4583-a966-8883a15e7777': An exception was thrown by the invocation.
Exception: System.InvalidOperationException: Unable to find matching constructor while trying to create an instance of SearchClient.
[2025-01-15T14:53:35.679Z] Expected one of the follow sets of configuration parameters:
[2025-01-15T14:53:35.679Z] 1. endpoint, indexName, credential:key
[2025-01-15T14:53:35.680Z] 2. endpoint, indexName
[2025-01-15T14:53:35.681Z]
[2025-01-15T14:53:35.681Z] Found the following configuration keys:
[2025-01-15T14:53:35.682Z]    at Microsoft.Extensions.Azure.ClientFactory.CreateClient(Type clientType, Type optionsType, Object options, IConfiguration configuration, TokenCredential credential)
[2025-01-15T14:53:35.683Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass4_0`2.<Azure.Core.Extensions.IAzureClientFactoryBuilderWithConfiguration<Microsoft.Extensions.Configuration.IConfiguration>.RegisterClientFactory>b__0(TOptions options, TokenCredential credentials)
[2025-01-15T14:53:35.684Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass8_0`2.<Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential.RegisterClientFactory>b__0(TOptions options, TokenCredential credential, IServiceProvider _)
[2025-01-15T14:53:35.684Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass15_0`2.<RegisterClientFactory>b__0(IServiceProvider provider, Object options, TokenCredential credential)
[2025-01-15T14:53:35.685Z]    at Microsoft.Extensions.Azure.ClientRegistration`1.GetClient(IServiceProvider serviceProvider, Object options, TokenCredential tokenCredential)
[2025-01-15T14:53:35.686Z]    at Microsoft.Extensions.Azure.AzureClientFactory`2.CreateClient(String name)
[2025-01-15T14:53:35.686Z]    at TUFSearchEndpoint.Function1..ctor(ILogger`1 logger, IAzureClientFactory`1 client) in C:\Users\hairb3\source\repos\Consumer Technology\TUFSearchEndpoint\TUFSearchEndpoint\Function1.cs:line 18
[2025-01-15T14:53:35.687Z]    at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
[2025-01-15T14:53:35.688Z]    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
[2025-01-15T14:53:35.688Z]    at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[2025-01-15T14:53:35.689Z]    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[2025-01-15T14:53:35.690Z]    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
[2025-01-15T14:53:35.690Z]    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
[2025-01-15T14:53:35.691Z]    at Microsoft.Azure.Functions.Worker.DefaultFunctionActivator.CreateInstance(Type instanceType, FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\Invocation\DefaultFunctionActivator.cs:line 23
[2025-01-15T14:53:35.692Z]    at TUFSearchEndpoint.DirectFunctionExecutor.ExecuteAsync(FunctionContext context) in C:\Users\hairb3\source\repos\Consumer Technology\TUFSearchEndpoint\TUFSearchEndpoint\obj\Debug\net8.0\Microsoft.Azure.Functions.Worker.Sdk.Generators\Microsoft.Azure.Functions.Worker.Sdk.Generators.FunctionExecutorGenerator\GeneratedFunctionExecutor.g.cs:line 37
[2025-01-15T14:53:35.693Z]    at Microsoft.Azure.Functions.Worker.OutputBindings.OutputBindingsMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\src\DotNetWorker.Core\OutputBindings\OutputBindingsMiddleware.cs:line 13
[2025-01-15T14:53:35.693Z]    at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\FunctionsMiddleware\FunctionsHttpProxyingMiddleware.cs:line 54
[2025-01-15T14:53:35.694Z]    at Microsoft.Azure.Functions.Worker.FunctionsApplication.InvokeFunctionAsync(FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\FunctionsApplication.cs:line 96
Stack:    at Microsoft.Extensions.Azure.ClientFactory.CreateClient(Type clientType, Type optionsType, Object options, IConfiguration configuration, TokenCredential credential)
[2025-01-15T14:53:35.695Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass4_0`2.<Azure.Core.Extensions.IAzureClientFactoryBuilderWithConfiguration<Microsoft.Extensions.Configuration.IConfiguration>.RegisterClientFactory>b__0(TOptions options, TokenCredential credentials)
[2025-01-15T14:53:35.696Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass8_0`2.<Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential.RegisterClientFactory>b__0(TOptions options, TokenCredential credential, IServiceProvider _)
[2025-01-15T14:53:35.696Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass15_0`2.<RegisterClientFactory>b__0(IServiceProvider provider, Object options, TokenCredential credential)
[2025-01-15T14:53:35.697Z]    at Microsoft.Extensions.Azure.ClientRegistration`1.GetClient(IServiceProvider serviceProvider, Object options, TokenCredential tokenCredential)
[2025-01-15T14:53:35.698Z]    at Microsoft.Extensions.Azure.AzureClientFactory`2.CreateClient(String name)
[2025-01-15T14:53:35.699Z]    at TUFSearchEndpoint.Function1..ctor(ILogger`1 logger, IAzureClientFactory`1 client) in C:\Users\hairb3\source\repos\Consumer Technology\TUFSearchEndpoint\TUFSearchEndpoint\Function1.cs:line 18
[2025-01-15T14:53:35.699Z]    at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
[2025-01-15T14:53:35.700Z]    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
[2025-01-15T14:53:35.701Z]    at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[2025-01-15T14:53:35.701Z]    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[2025-01-15T14:53:35.702Z]    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
[2025-01-15T14:53:35.703Z]    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
[2025-01-15T14:53:35.704Z]    at Microsoft.Azure.Functions.Worker.DefaultFunctionActivator.CreateInstance(Type instanceType, FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\Invocation\DefaultFunctionActivator.cs:line 23
[2025-01-15T14:53:35.704Z]    at TUFSearchEndpoint.DirectFunctionExecutor.ExecuteAsync(FunctionContext context) in C:\Users\hairb3\source\repos\Consumer Technology\TUFSearchEndpoint\TUFSearchEndpoint\obj\Debug\net8.0\Microsoft.Azure.Functions.Worker.Sdk.Generators\Microsoft.Azure.Functions.Worker.Sdk.Generators.FunctionExecutorGenerator\GeneratedFunctionExecutor.g.cs:line 37
[2025-01-15T14:53:35.705Z]    at Microsoft.Azure.Functions.Worker.OutputBindings.OutputBindingsMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\src\DotNetWorker.Core\OutputBindings\OutputBindingsMiddleware.cs:line 13
[2025-01-15T14:53:35.706Z]    at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\FunctionsMiddleware\FunctionsHttpProxyingMiddleware.cs:line 54
[2025-01-15T14:53:35.706Z]    at Microsoft.Azure.Functions.Worker.FunctionsApplication.InvokeFunctionAsync(FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\FunctionsApplication.cs:line 96.
[2025-01-15T14:53:35.742Z] Executed 'Functions.SearchEndpoint' (Failed, Id=cd5a6130-359e-4583-a966-8883a15e7777, Duration=520ms)
[2025-01-15T14:53:35.743Z] System.Private.CoreLib: Exception while executing function: Functions.SearchEndpoint. System.Private.CoreLib: Result: Failure
Exception: Unable to find matching constructor while trying to create an instance of SearchClient.
[2025-01-15T14:53:35.744Z] Expected one of the follow sets of configuration parameters:
[2025-01-15T14:53:35.745Z] 1. endpoint, indexName, credential:key
[2025-01-15T14:53:35.746Z] 2. endpoint, indexName
[2025-01-15T14:53:35.747Z]
[2025-01-15T14:53:35.747Z] Found the following configuration keys:
Stack:    at Microsoft.Extensions.Azure.ClientFactory.CreateClient(Type clientType, Type optionsType, Object options, IConfiguration configuration, TokenCredential credential)
[2025-01-15T14:53:35.748Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass4_0`2.<Azure.Core.Extensions.IAzureClientFactoryBuilderWithConfiguration<Microsoft.Extensions.Configuration.IConfiguration>.RegisterClientFactory>b__0(TOptions options, TokenCredential credentials)
[2025-01-15T14:53:35.749Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass8_0`2.<Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential.RegisterClientFactory>b__0(TOptions options, TokenCredential credential, IServiceProvider _)
[2025-01-15T14:53:35.750Z]    at Microsoft.Extensions.Azure.AzureClientFactoryBuilder.<>c__DisplayClass15_0`2.<RegisterClientFactory>b__0(IServiceProvider provider, Object options, TokenCredential credential)
[2025-01-15T14:53:35.751Z]    at Microsoft.Extensions.Azure.ClientRegistration`1.GetClient(IServiceProvider serviceProvider, Object options, TokenCredential tokenCredential)
[2025-01-15T14:53:35.751Z]    at Microsoft.Extensions.Azure.AzureClientFactory`2.CreateClient(String name)
[2025-01-15T14:53:35.752Z]    at TUFSearchEndpoint.Function1..ctor(ILogger`1 logger, IAzureClientFactory`1 client) in C:\Users\hairb3\source\repos\Consumer Technology\TUFSearchEndpoint\TUFSearchEndpoint\Function1.cs:line 18
[2025-01-15T14:53:35.753Z]    at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
[2025-01-15T14:53:35.754Z]    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
[2025-01-15T14:53:35.755Z]    at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[2025-01-15T14:53:35.755Z]    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[2025-01-15T14:53:35.756Z]    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
[2025-01-15T14:53:35.757Z]    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
[2025-01-15T14:53:35.758Z]    at Microsoft.Azure.Functions.Worker.DefaultFunctionActivator.CreateInstance(Type instanceType, FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\Invocation\DefaultFunctionActivator.cs:line 23
[2025-01-15T14:53:35.759Z]    at TUFSearchEndpoint.DirectFunctionExecutor.ExecuteAsync(FunctionContext context) in C:\Users\hairb3\source\repos\Consumer Technology\TUFSearchEndpoint\TUFSearchEndpoint\obj\Debug\net8.0\Microsoft.Azure.Functions.Worker.Sdk.Generators\Microsoft.Azure.Functions.Worker.Sdk.Generators.FunctionExecutorGenerator\GeneratedFunctionExecutor.g.cs:line 37
[2025-01-15T14:53:35.760Z]    at Microsoft.Azure.Functions.Worker.OutputBindings.OutputBindingsMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\src\DotNetWorker.Core\OutputBindings\OutputBindingsMiddleware.cs:line 13
[2025-01-15T14:53:35.761Z]    at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\FunctionsMiddleware\FunctionsHttpProxyingMiddleware.cs:line 54
[2025-01-15T14:53:35.762Z]    at Microsoft.Azure.Functions.Worker.FunctionsApplication.InvokeFunctionAsync(FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\FunctionsApplication.cs:line 96
[2025-01-15T14:53:35.764Z]    at Microsoft.Azure.Functions.Worker.Handlers.InvocationHandler.InvokeAsync(InvocationRequest request) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Handlers\InvocationHandler.cs:line 89.

Reproduction Steps

host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      "enableLiveMetricsFilters": true
    }
  },
  "SearchClient": {
    "endpoint": "https://my-search-service.search.windows.net",
    "indexName": "my-index-name"
  }
}

Program.cs

using Azure.Identity;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices((hostContext, services) =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
        services.AddAzureClients(builder =>
        {
            builder.AddSearchClient(hostContext.Configuration.GetSection("SearchClient"))
                .WithName("name").WithCredential(new DefaultAzureCredential());
        });
    })
    .Build();

host.Run();

Function1.cs

using Azure.Search.Documents;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Logging;

namespace MyNameSpace
{
    public class Function1
    {
        private readonly ILogger<Function1> _logger;
        private readonly SearchClient searchClient;

        public Function1(ILogger<Function1> logger, IAzureClientFactory<SearchClient> client)
        {
            _logger = logger;
            searchClient = client.CreateClient("name");
        }

        [Function("SearchEndpoint")]
        public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");
            return new OkObjectResult("Welcome to Azure Functions!");
        }
    }
}

Environment

.NET 8
Windows 11

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Azure.Search.Documents" Version="11.6.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.9.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Searchcustomer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions