Microsoft.Azure.WebJobs.Host: Error indexing method

Roberto Lopez Colin 0 Reputation points
2023-09-08T17:47:55.2533333+00:00

Hello, I'm working with an AzureFunction(CosmosDBTrigger), everything was working ok but suddenly the function stop working. When I want to Run the function I got the following errors:

User's image

my host.json:

{  
	"version": "2.0",  
	"logging": 
	{    
		"applicationInsights": 
		{      
			"samplingSettings": 
			{        
				"isEnabled": true,        
				"excludedTypes": "Request"      
			}    
		}  
	}
}

my local.settings.json:

{  
	"IsEncrypted": false,  
	"Values": 
	{    
		"AzureWebJobsStorage": "UseDevelopmentStorage=true",    
		"AzureWebJobsDashboard": "UseDevelopmentStorage=true",    
		"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",    
		"UseDevelopmentStorage": true,    
		"IS_RUNNING_LOCALLY": "true",      
		"DatabaseName": "myDbName",    
		"CosmosDBConnection": "myCosmosConnection",    
		"CosmosDbEndpointUri": "https://localhost:8081",
		...
	}
}

Program.cs:

public static class Program{    
	static void Main()    {        
		var assembly = Assembly.GetExecutingAssembly();        
		var host = new HostBuilder()
			.ConfigureAppConfiguration(builder => { builder.AddUserSecrets(assembly, true); })   	
			.ConfigureFunctionsWorkerDefaults()            
			.ConfigureServices(services => { services.AddMyCollectionServices(assembly); })      
			.Build();        

		host.Run();   
 
		}
}

In my IServiceCollection file:

...

var isRunningLocally = Environment.GetEnvironmentVariable("IS_RUNNING_LOCALLY") == "true";
var configuration = BuildConfiguration(isRunningLocally, assembly);

...

private static IConfigurationRoot BuildConfiguration(bool isRunningLocally, Assembly assembly){
    
	var configurationBuilder = new ConfigurationBuilder();  
  
	if (isRunningLocally){        
		configurationBuilder
			.AddJsonFile("local.settings.json", optional: true)            
			.AddUserSecrets(assembly, true);    
	}    

	var configuration = configurationBuilder.Build();    

	return configuration;
}

My function.csproj

...

...


When I debug the IServiceCollection the line where I intent to get from the "local.settings.json" the value "IS_RUNNING_LOCALLY" it always resolve as false, which means that Environment.GetEnvironmentVariable("IS_RUNNING_LOCALLY") is not able to read the value of that property.

I have spend couple hours trying to figure it out but I can't find what could possibly cause the error. As I said before this function was working just fine and suddenly stop working. Besides that I have another CosmosDbTrigger with the exact same project and configuration structure and it is working fine.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Developer technologies .NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. Mohammed Naveed 5 Reputation points
    2024-10-24T08:04:40.64+00:00

    Hello all,
    Migration from .NET 6 to .NET 8 - Issue Resolved

    The migration issue has been resolved by upgrading the necessary WebJobs packages based on the .NET 8 framework. Using this approach, we can omit the need for the new Program.cs class in Functions and Console applications. Instead, we can continue using the existing Startup class by upgrading only the WebJobs packages.

    Steps to Follow for Migration:

    1. Update the Target Framework Update the project to target .NET 8.
    2. Update the Required Function Packages Update the function packages to the latest versions compatible with .NET 8. For example:
      • Microsoft.NET.Sdk.Functions from 4.1.3 to 4.4.0 (latest)
      • Azure.Messaging.ServiceBus from 7.11.1 to 7.18.1
      • Microsoft.Azure.WebJobs.Extensions.ServiceBus from 5.8.1 to 5.12.0
    3. Update Project References to Latest Versions If your project references default packages, ensure they are updated to the latest versions:
      • Microsoft.EntityFrameworkCore.SqlServer
      • Microsoft.Extensions.Configuration.Abstractions
      • Microsoft.Extensions.Configuration.Json

    Let me know if you have any doubts.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.