azure-sdk-for-net icon indicating copy to clipboard operation
azure-sdk-for-net copied to clipboard

[BUG] Function [XYZ]: cannot find value named 'ServiceBusConnection' in local.settings.json that matches 'connection' property set on 'serviceBusTrigger'

Open kristinannabel opened this issue 2 years ago • 9 comments

Library name and version

microsoft.azure.webjobs.extensions.servicebus 5.8.1

Describe the bug

I have enabled Managed Identity together with ServiceBusTrigger in my Azure Function according to the official guide here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-identity-based-connections-tutorial-2.

Expected behavior

There is no MSBuild warning regarding missing setting in local.settings.json when using Managed Identity for Azure Functions.

Actual behavior

I get a warning: Function [X]: cannot find value named 'ServiceBusConnection' in local.settings.json that matches 'connection' property set on 'serviceBusTrigger' C:....nuget\packages\microsoft.net.sdk.functions\4.1.3\build\Microsoft.NET.Sdk.Functions.Build.targets 32

I get this error both locally and on the build server.

Reproduction Steps

Prepare an Azure Function based on the official page here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-identity-based-connections-tutorial-2.

Content in local.settings.json: "Values": { ..., "ServiceBusConnection:ConnectionStringWithoutSecret": "Endpoint=sb://namespace.servicebus.windows.net/;Authentication=ManagedIdentity", "ServiceBusConnection:fullyQualifiedNamespace": "namespace.servicebus.windows.net", "ServiceBusConnection:HostAddress": "sb://namespace.servicebus.windows.net" }

Environment

.NET 6 Windows 10 IDE: Visual Studio 17.4.4

Microsoft.NET.Sdk.Functions 4.1.3

Build agent running WS19

kristinannabel avatar Feb 13 '23 13:02 kristinannabel

Thank you for your feedback. Tagging and routing to the team member best able to assist.

jsquire avatar Feb 13 '23 13:02 jsquire

Hi @kristinannabel,

If you are attempting to use managed identity, you should not be specifying a connection string. Instead, you should have something like this in your "Values":

"MyConnection__fullyQualifiedNamespace": "namespace.servicebus.windows.net",
"MyConnection__credential": "managedidentity"

More details can be found here.

JoshLove-msft avatar Feb 16 '23 01:02 JoshLove-msft

Hi @kristinannabel. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

ghost avatar Feb 16 '23 01:02 ghost

/unresolve The warning is that there are no setting in local.settings.json that is named ServiceBusConnection, since my settings are ServiceBusConnection:Something...

Also, I need the connectionsStringWithoutSecret for this purpose:

.AddMassTransitForAzureFunctions(cfg =>
{
      cfg.AddServiceBusMessageScheduler();
     cfg.AddConsumer<MyConsumer>(typeof(MyConsumerDefinition));
}, ServicebusSettingsConst.ConnectionStringWithoutSecretName, ConfigureBus);

kristinannabel avatar Feb 16 '23 07:02 kristinannabel

Please be aware that the connection string that you're specifying is invalid. The token Authentication=ManagedIdentity is not valid and will be ignored. As a result, that connection string will be rejected as malformed since there is no shared key name/value or SAS associated with it.

jsquire avatar Feb 16 '23 14:02 jsquire

The warning is that there are no setting in local.settings.json that is named ServiceBusConnection, since my settings are ServiceBusConnection:Something...

The syntax you are using is not right - you need to use double underscore rather than colon as I showed in the snippet above. Also, as @jsquire mentions, the connection string you are using will not work because the Authentication token is not supported in the connection string.

JoshLove-msft avatar Feb 16 '23 18:02 JoshLove-msft

@JoshLove-msft Ok, but how should my code-snippet look like? My code is actually working, so it cant be that far off. I just wanted to get ridd of the warning. I will try double underscore.

kristinannabel avatar Feb 17 '23 07:02 kristinannabel

With these values, I still get the same error; "ServiceBusConnection__fullyQualifiedNamespace": "namespace.servicebus.windows.net", "ServiceBusConnection__credential": "managedidentity",

kristinannabel avatar Feb 17 '23 07:02 kristinannabel

Can you make sure you are using the latest Core Tools version?

JoshLove-msft avatar Feb 17 '23 16:02 JoshLove-msft

Hi, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!

ghost avatar Feb 24 '23 20:02 ghost

@JoshLove-msft Yes, I am using the latest Core Tools version.

kristinannabel avatar Feb 27 '23 08:02 kristinannabel

The connection name you use in your settings needs to match the connection name used in your function. Can you confirm that they match?

JoshLove-msft avatar Feb 27 '23 17:02 JoshLove-msft

Yes. From Function: Connection = "ServiceBusConnection" In local.settings.json:

"ServiceBusConnection__ConnectionStringWithoutSecret": "Endpoint=sb://namespace.servicebus.windows.net",
    "ServiceBusConnection__HostAddress": "sb://namespace.servicebus.windows.net",
"ServiceBusConnection__fullyQualifiedNamespace": "namespace.servicebus.windows.net", 
"ServiceBusConnection__credential": "managedidentity",

kristinannabel avatar Feb 28 '23 07:02 kristinannabel

ServiceBusConnection__ConnectionStringWithoutSecret

You can't include both a connection string and the managed identity config. The extension has no knowledge of this settings "ConnectionStringWithoutSecret" and "HostAddress". If you want to use connection string config you would just set the connection string like this:

"ServiceBusConnection": "<connection string>"

If you need that other setting for some other integration, it would need to live under a different config, e.g. "MassTransitConnectionInfo". If you want to use managed identity config you would include only this:

"ServiceBusConnection__fullyQualifiedNamespace": "namespace.servicebus.windows.net", 
"ServiceBusConnection__credential": "managedidentity",

JoshLove-msft avatar Feb 28 '23 17:02 JoshLove-msft

Hi @kristinannabel. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

ghost avatar Feb 28 '23 17:02 ghost

/unresolve Ok, even with "ServiceBusConnection__fullyQualifiedNamespace": "namespace.servicebus.windows.net", "ServiceBusConnection__credential": "managedidentity", in local.settings.json, I still get the same error

Function [MyFunction]: cannot find value named 'ServiceBusConnection' in local.settings.json that matches 'connection' property set on 'serviceBusTrigger'
C:\Users\kristintf\.nuget\packages\microsoft.net.sdk.functions\4.1.3\build\Microsoft.NET.Sdk.Functions.Build.targets	32	

kristinannabel avatar Mar 01 '23 08:03 kristinannabel

Can you please share your function app project so that I can repro locally?

JoshLove-msft avatar Mar 01 '23 18:03 JoshLove-msft

@JoshLove-msft I'm not allowed to share any of our code. But can give you some bits, if thats helpful.

`

    public class MyFunction {
    private readonly string _queueName;
    private readonly IMessageReceiver _receiver;
    
    public MyFunction(IMessageReceiver receiver, ConnectionSettings settings)
    {
        _receiver = receiver ?? throw new ArgumentNullException(nameof(receiver));
        _queueName = settings.QueueName;
    }

    [Disable("DisableServiceBusTriggerFunctions")]
    [FunctionName("MyFunction")]
    public async Task MyFunction([ServiceBusTrigger("%ConnectionSettings:QueueName%", Connection = "ServiceBusConnection")] ServiceBusReceivedMessage message, CancellationToken cancellationToken)
    {
      await _receiver.HandleConsumer<MyConsumer>(_queueName, message, cancellationToken);
    }
}`
public class MyConsumer : IConsumer<IMyEvent> {
        private readonly IMyAdapter _myAdapter;
        private readonly ITelemetryLogger _telemetryLogger;
        public MyConsumer(IMyAdapter myAdapter,
           ITelemetryLogger telemetryLogger)
        {
            _myAdapter = myAdapter 
                ?? throw new ArgumentNullException(nameof(myAdapter));
            _telemetryLogger = telemetryLogger ?? throw new ArgumentNullException(nameof(telemetryLogger));
        }
        
        public async Task Consume(ConsumeContext<IMyEvent> context)
        {
            var message = context.Message;

            _telemetryLogger.LoggId(message.Id);
            await _myAdapter.Arkiver(message.Id);
        }
    }

Startup.cs:

services.AddHttpClient<IMyAdapter, MyAdapter>()
services.AddScoped<MyFunction>()
services.AddMassTransitForAzureFunctions(cfg =>
{
   cfg.AddServiceBusMessageScheduler();
   cfg.AddConsumer<MyConsumer>();
 }, "Endpoint=sb://namespace.servicebus.windows.net/;Authentication=ManagedIdentity", ConfigureBus);

private void ConfigureBus(IBusRegistrationContext busRegistrationContext, IServiceBusBusFactoryConfigurator serviceBusBusFactory)
        {
            serviceBusBusFactory.ConfigureJsonSerializerOptions(settings =>
            {
                settings.NumberHandling = JsonNumberHandling.AllowReadingFromString;
                return settings;
            });

            var config = busRegistrationContext.GetRequiredService<IConfiguration>();
            serviceBusBusFactory.Host(new Uri(config["sb://namespace.servicebus.windows.net"]), conf =>
            {
                if (EnvironmentIsDevelopment())
                {
                    conf.TransportType = Azure.Messaging.ServiceBus.ServiceBusTransportType.AmqpWebSockets;
                }
                conf.TokenCredential = new DefaultAzureCredential();
            });
        }

kristinannabel avatar Mar 02 '23 09:03 kristinannabel

Can you please also include your complete local.settings.json file (redacting any secrets) as well as your csproj?

JoshLove-msft avatar Mar 02 '23 17:03 JoshLove-msft

Hi, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!

ghost avatar Mar 09 '23 20:03 ghost

Having the same issue - here's my local.settings.json (redacted):

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    ...
    "ServiceBusConnection:fullyQualifiedNamespace": "xxx.servicebus.windows.net",
    ...
    "AZURE_TENANT_ID": "xxx",
    "AzureFunctionsJobHost:Serilog:MinimumLevel:Default": "Debug"
  },
  "Host": {
    "CORS": "http://localhost:3000"
  }
}

and csproj (also redacted)

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<TargetFramework>net6.0</TargetFramework>
		<AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
	</PropertyGroup>
	<PropertyGroup>
		<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
		<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
		<UserSecretsId>f84e8ce6-e906-4a4a-b228-932d79a41e8f</UserSecretsId>
	</PropertyGroup>
	<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
	  <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
	</PropertyGroup>
	<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
	  <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Azure.Identity" Version="1.8.2" />
		<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.13.1" />
		<PackageReference Include="Azure.Storage.Queues" Version="12.13.0" />
		<PackageReference Include="CommunityToolkit.Diagnostics" Version="8.1.0" />
		<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.5.1" />
		<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
		<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.2.0" />
		<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.5.1" />
		<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.9.0" />
		<PackageReference Include="Microsoft.CodeAnalysis" Version="4.5.0" />
		<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.5.0" />
		<PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.3" />
		<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
		<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
		<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
		<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
		<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="6.0.0" />
		<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
		<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
		<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
		<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
		<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
		<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.27.0" />
	</ItemGroup>
	<ItemGroup>
		<ProjectReference Include="..\xxx.csproj" />
		<ProjectReference Include="..\yyy.csproj" />
		<ProjectReference Include="..\zzz.csproj" />
	</ItemGroup>
	<ItemGroup>
		<None Update="host.json">
			<CopyToOutputDirectory>Always</CopyToOutputDirectory>
		</None>
		<None Update="local.settings.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
		</None>
	</ItemGroup>
</Project>

cmclellen avatar Mar 19 '23 01:03 cmclellen

Hi @kristinannabel, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!

github-actions[bot] avatar Mar 26 '23 03:03 github-actions[bot]