Azure Function v3 - "Azure Functions runtime is unreachable" when add Identity in Startup

Viewed 5837

I have an Azure Function (v3) that must interact with DB and also with user management. It has as a dependency a project that also contains the DAL so all the context configuration. When in the Startup of the function I add the dependency to the DbContext and deploy it on Azure I have no problems. When in addition to the DbContext I also add Identity and re-deploy, the Portal says "Azure Functions runtime is unreachable".

This is the function Startup.cs:

[assembly: FunctionsStartup(typeof(Directio.PeopleSee.OrderSynchronizer.Startup))]

namespace Directio.PeopleSee.OrderSynchronizer
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();

            builder.Services.AddDbContext<DBContext>(options1 =>
            {
                options1.UseLazyLoadingProxies(false);
                options1.UseSqlServer(Environment.GetEnvironmentVariable("DBConnectionString"), builder =>
                {
                    builder.CommandTimeout(10);
                }
                );
            })
            .AddIdentity<AspNetUser, AspNetRole>(opt =>
             {
                 opt.Password.RequireDigit = false;
                 opt.Password.RequireLowercase = false;
                 opt.Password.RequireNonAlphanumeric = false;
                 opt.Password.RequireUppercase = false;
                 opt.Password.RequiredLength = 0;
                 opt.Password.RequiredUniqueChars = 0;
                 opt.User.RequireUniqueEmail = false;
                 opt.SignIn.RequireConfirmedEmail = false;
                 opt.SignIn.RequireConfirmedAccount = false;
                 opt.SignIn.RequireConfirmedPhoneNumber = false;
             })
            .AddEntityFrameworkStores<DBContext>()
            .AddDefaultTokenProviders();

            builder.Services.AddOptions<FunctionAppSettings>().Configure<IConfiguration>((settings, configuration) => configuration.GetSection("FunctionAppSettings").Bind(settings));

            builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
            builder.Services.AddScoped<IUserService, UserServiceImpl>();
            builder.Services.AddScoped<IRoleService, RoleServiceImpl>();
            builder.Services.AddScoped<ISubscribersService, SubscriberServiceImpl>();
            builder.Services.AddScoped<IOrdersService, OrdersService>();
        }
    }
}

All services registrations come from the project the dependency is linked to. The function is under netcore3.1 as a framework, and is deployed in an Azure Func App with a pay-as-you-go plan, Windows server and it's not a docker container.

What could be the problem?

1 Answers

When you add Identity you are also adding the AspNetCore Authentication dependencies. For some reason Functions host checks if these dependencies exist in your applications and stops the Host module from adding the built-in authentication schemes that allows the portal to have access to the functions in your project.

If you still want to load Identity components at startup time I encourage you to look at https://www.nuget.org/packages/DarkLoop.Azure.Functions.Authorize

Here is a post describing how it works: https://blog.darkloop.com/post/functionauthorize-for-azure-functions-v3

you can call

builder.AddAuthentication();
builder.AddAuthorization();
// or
builder.Services.AddFunctionsAuthentication();
builder.Services.AddFunctionsAuthorization();

This call forces the functions built-in authentication to be loaded and allows you to add more authentication dependencies and schemes.

After this call you can make your calls AddDbContext<... and AddIdentity<...

Azure portal should be able to get all the information it needs about your functions.

Related