0
votes

I have the following Startup class in my Azure Function v2 project:

[assembly: FunctionsStartup(typeof(AzureAppDomainRegistration.Startup))]
namespace AzureAppDomainRegistration
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var connString = System.Configuration.ConfigurationManager.AppSettings["ConnectionStrings:DataContext"];

            //var connString = config["ConnectionStrings:DataContext"];
            builder.Services.AddDbContext<DataContext>(options => options
                .UseLazyLoadingProxies()
                .UseSqlServer(connString));

            builder.Services.AddTransient<IActionsRegistrationInfo, EfActionsRegistrationInfo>();
        }
    }
}

and Function:

public class Function100_CheckEmail
{
    readonly IActionsRegistrationInfo _actionsRegistrationInfo;

    public Function100_CheckEmail(IActionsRegistrationInfo actionsRegistrationInfo)
    {
        _actionsRegistrationInfo = actionsRegistrationInfo;
    }

    [FunctionName("Function100_CheckEmail")]
    //public static IActionResult Run(
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        [Queue("email-message-admin-confirmation", Connection = "StorageConnectionString")]CloudQueue outputQueue,
        ExecutionContext context,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

but when this function is being executed, I get the following errors on Azure Portal:

enter image description here

without DI it works fine. What is wrong?

.NET Core 2.2

ADDED: I tried to remote debugger and I see, that Configure method of Startup file has exceptions (logged by App Insights) with ArgumentNullException, but no details. What can be it?

1
What packages you are using? - Bowman Zhu

1 Answers

-1
votes

So in order to get a Environment Variable in Azure functions, you need to use

var connStr =   Environment.GetEnvironmentVariable("ConnectionStrings:SQLConnectionString", EnvironmentVariableTarget.Process);

Also check if you have the value in local.settings.json which looks something like this

{
  "IsEncrypted": false,   
  "Values": {
    "AzureWebJobsStorage": "<connection string>", 
    "AzureWebJobsDashboard": "<connection string>" 
  },
  "Host": {
    "LocalHttpPort": 7071, 
    "CORS": "*" 
  },
  "ConnectionStrings": {
    "SQLConnectionString": "Value"
  }
}

Refereces :