0

I am observing that the hashCode of _serviceProvider is the same throughout all the execution of the below function at an interval of 1 min.

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;

namespace TestFunction
{
    public class Function2
    {
        private readonly IServiceProvider _serviceProvider;
        public Function2(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        [FunctionName("Function2")]
        public void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            var hashCode = _serviceProvider.GetHashCode();
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        }
    }
}

Version:

<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />

FYI: I also compared the hashcode of serviceProvider in an API for two requests at the same endpoint but they are different.

Question: What is the lifetime of IServiceProvider in a Time Trigger Azure Functions?

akash
  • 727
  • 5
  • 13
  • As you are using timer trigger `TimerTrigger("0 */1 * * * *")` it should return in 1 minute of interval time . Also could you please refer this [MS DOC](https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#use-injected-dependencies) – AjayKumarGhose Dec 30 '21 at 08:27
  • @AjayKumarGhose-MT My main purpose is to figure out the lifetime of the service provider here. – akash Dec 30 '21 at 08:34
  • There are different types of service provider like Based on the above given document: Transient,scoped&Singleton . Could you please confirm me is this the same you are looking for [MS DOC](https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#service-lifetimes) – AjayKumarGhose Dec 30 '21 at 08:48
  • 1
    Yes. @AjayKumarGhose-MT – akash Dec 30 '21 at 09:45
  • Sorry, your answer doesn't confirm with proof that what is the lifetime of the IServiceProvider in respect to azure function. @AjayKumarGhose-MT – akash Dec 30 '21 at 10:38
  • Please complete your question: you start by saying what you are trying, but you don't report your findings. And the question should also be stated in the body, not only in the title. – JHBonarius Dec 30 '21 at 10:53

2 Answers2

0

Thank you @akash, For the update.

Based on this MS DOC:

For a Functions app, the different service lifetimes behave as follows:

Transient: Transient services are created upon each resolution of the service.

Scoped: The scoped service lifetime matches a function execution lifetime. Scoped services are created once per function execution. Later requests for that service during the execution reuse the existing service instance.

Singleton: The singleton service lifetime matches the host lifetime and is reused across function executions on that instance. Singleton lifetime services are recommended for connections and clients, for example DocumentClient or HttpClient instances.

View or download a sample of different service lifetimes on GitHub.

For more information please refer the below links:

SO THREAD : Azure Function V3 Dependency Injection Lifetime

BLOG: Dependency Injection in Azure Functions

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15
  • Really sorry that your answer doesn't confirm with proof that what is the lifetime of the IServiceProvider in respect to azure function. @AjayKumarGhose-MT. – akash Dec 30 '21 at 10:40
0

IServiceProvider is just an interface for the service collection. But it's implemented by e.g. HttpContext used in ASP.NET. HttpContext has a lifetime of a request, i.e. it is Scoped.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41