简体   繁体   中英

When deploy Azure function to Azure how did it know that it should read the AzureFunctionSettings from App settings instead of from local.setting.json

I created my first Azure Function which integrate with SharePoint Online list, using those main points:-

1-I created an Azure App with self-sign certificate to authorize my Azure function.

在此处输入图像描述

2-I created a new Azure Function project using Visual Studio 2019. here are the main components - Function.cs :-

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using PnP.Core.Services;
using PnP.Core.Model.SharePoint;
using System.Collections.Generic;

namespace FunctionApp1
{
    public  class Function1
        
    {
        private readonly IPnPContextFactory pnpContextFactory;
        public Function1(IPnPContextFactory pnpContextFactory)
        {
            this.pnpContextFactory = pnpContextFactory;
        
        }
        [FunctionName("Function1")]
        public  void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            using (var context = pnpContextFactory.Create("Default"))
            {
                var myList = context.Web.Lists.GetByTitle("SubFolders");
                Dictionary<string, object> values = new Dictionary<string, object>
    {
        { "Title", System.DateTime.Now }
    };

                // Use the AddBatch method to add the request to the current batch
                myList.Items.AddBatch(values);
                context.Execute();
            }
        }
    }
}

- Startup.cs :-

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PnP.Core.Auth;
using System.Security.Cryptography.X509Certificates;

[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))]
namespace FunctionApp1
{
    class Startup :FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {

            var config = builder.GetContext().Configuration;
            var azureFunctionSettings = new AzureFunctionSettings();
            config.Bind(azureFunctionSettings);
            builder.Services.AddPnPCore(options =>
            {
                options.DisableTelemetry = true;
                var authProvider = new X509CertificateAuthenticationProvider(azureFunctionSettings.ClientId,
                    azureFunctionSettings.TenantId,
                    StoreName.My,
                    StoreLocation.CurrentUser,
                    azureFunctionSettings.CertificateThumbprint);
                options.DefaultAuthenticationProvider = authProvider;

                options.Sites.Add("Default", new PnP.Core.Services.Builder.Configuration.PnPCoreSiteOptions

                {
                    SiteUrl = azureFunctionSettings.SiteUrl,
                    AuthenticationProvider = authProvider


                });

            });
        
        }

    }
}

- local.setting.json :-

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "SiteUrl": "https://***.sharepoint.com/",
    "TenantId": "0b***",
    "ClientId": "92***",
    "CertificateThumbPrint": "EB***",
    "WEBSITE_LOAD_CERTIFICATES": "EB***"
  }
}

then i deploy it to Azure and it is working well, where each 5 minutes it adds a new list item.

But what i am unable to understand, is that when i test the function locally, the function reads its setting from the local.settings.json file, but after deploying it to Azure it start reading its settings from the online Azure App settings.. so how it did this behind the senses?

在此处输入图像描述

This is by design.

App settings in a function app contain configuration options that affect all functions for that function app. When you run locally, these settings are accessed as local environment variables.

and

You can use application settings to override host.json setting values without having to change the host.json file itself. This is helpful for scenarios where you need to configure or modify specific host.json settings for a specific environment. This also lets you change host.json settings without having to republish your project.

Taken from App settings reference for Azure Functions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM