Gratzy
Gratzy

Reputation: 2908

Azure functions / Durable Functions / global.asax

I'm attempting to move a web app that I have been working on for some years. I load up a list of configs in the global.asax for future access. I see that the global.asax doesn't have an equivalent in durable functions so that's what made me start looking at durable functions; but I'm not sure this is the best way to handle the loads either. There could be well over 10,000 records that get loaded in my current environment so loading them on the fly wouldn't work well either.

This is a sample of what my global.asax configuration looks like; not that it really matters...:

            if (query.Count() > 0)
            {
                foreach (var item in query)
                {
                    bool bActive = false;
                    bool.TryParse(item.IsActive.ToString(), out bActive);

                    if (item.ProviderName != string.Empty && bActive)
                    {
                        try
                        {
                            bool bEncrypt = false;
                            bool bSign = false;

                            // yes, I know these are bools anyway; this is to catch possible nulls...
                            bool.TryParse(item.Sign.ToString(), out bSign);
                            bool.TryParse(item.Encrypt.ToString(), out bEncrypt);

                            SConfiguration.AddServiceProvider(
                                new ServiceProvider()
                                {
                                    Name = item.PartnerEntity,
                                    NameIDFormat = item.NameFormat,
                                    SignSAMLResponse = bSign,
                                    Sign = bSign,
                                    Encrypt = bEncrypt

                                });
                        }
                   }

So this config needs to persist through multiple http sendoffs (it's SAML).

Is there a way to preload a configuration and have it available to all future azure functions calls?

Upvotes: 0

Views: 117

Answers (1)

Thiago Custodio
Thiago Custodio

Reputation: 18387

You can use Dependency Injection with Service lifetime Singleton:

using System;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Logging;

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {

            builder.Services.AddSingleton<ILoggerProvider, MyLoggerProvider>();
        }
    }
}

source: https://learn.microsoft.com/bs-cyrl-ba/azure/azure-functions/functions-dotnet-dependency-injection

Upvotes: 1

Related Questions