0
votes

I am writing an azure webjob which reads events from eventhub using NET Core 3.1. I have a config file as below:

{
  "JobHostConfig": {
    "DashboardConnectionString": "",
    "StorageConnectionString": "xx"
  },
  "EventHubConfig": {
    "EventHubConnectionString": "xx",
    "EventHubName": "xx",
    "EventProcessorHostName": "xx",
    "ConsumerGroupName": "xx",
    "StorageConnectionString": "xx",
    "StorageContainerName": "xx"
  }
}

In the Main method, I call ConfigureServices method which looks something like:

        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory() + $"\\..\\..\\..\\ConfigFiles")
            .AddJsonFile($"applicationConfig.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"applicationConfig.{environment}.json", optional: true, reloadOnChange: true);

        Configuration = builder.AddEnvironmentVariables()
                               .Build();

        services.AddSingleton<IConfigurationRoot>(Configuration);
        services.AddSingleton<IConfiguration>(Configuration);
        services.AddMvcCore();
        services.AddSingleton(GetInstance<EventHubConfig>());
        services.AddSingleton(GetInstance<JobHostConfig>());

I confirmed that at runtime configs are getting populated in Configuration only like this: Configuration["EventHubConfig:EventHubName"]. But I also debugged that environment variables have not been set and its value is null.

So when I do: ProcessEvent([EventHubTrigger("%EventHubName%", ConsumerGroup = "%ConsumerGroupName%", Connection = "%ConnectionString%")] EventData eventData) I get that %EventHubName% is not resolved.

Also, when I hard-code the values of these then I get: No event hub receiver named.

Can someone suggest what is wrong with my registration?

Furthermore, I replaced the values with string in EventHubTrigger, and I get Value cannot be null. Parameter name: receiverConnectionString

2

2 Answers

0
votes

Right click the appsettings.json file -> click propertities -> set the "Copy to output directory" to "copy if newer"

and the code should be

public static void Trigger([EventHubTrigger("my eventhub name",Connection = "EventHubConnectionString")] EventData message, ILogger logger)
{

    string data = Encoding.UTF8.GetString(message.Body);
    Console.WriteLine(data+";;xxx");
}

Make your appsettings.json simpler like

{  
  "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net", 
  "EventHubConnectionString": "Endpoint=sb://xxxx"
}
0
votes

When using %% format, you should use Custom binding expressions.

For more details, please refer to this answer.

Please let me know if you still have more issues about it.