You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When registering a scoped service with an implementationFactory set, multiple instances of the service are being created per run of a function. To recreate this issue, I used the following Startup in a sample project:
usingMicrosoft.Azure.Functions.Extensions.DependencyInjection;usingMicrosoft.Extensions.DependencyInjection;usingServices;usingSystem;usingSystem.Collections.Generic;usingSystem.Text;[assembly:FunctionsStartup(typeof(CakeOrdersFunctions.Startup))]namespaceCakeOrdersFunctions{publicclassStartup:FunctionsStartup{publicoverridevoidConfigure(IFunctionsHostBuilderbuilder){builder.Services.AddScoped<ISupplier,Supplier>();builder.Services.AddScoped<IStoreFront>(x =>newStoreFront());builder.Services.AddScoped<IBakery,Bakery>();//Ingredients/registers added in the ISupplier/IStoreFront services will not be reflected in this service:builder.Services.AddScoped<IManualBakery>(x =>newManualBakery(x.GetRequiredService<ISupplier>(),x.GetRequiredService<IStoreFront>()));}}}
Azure Function Code
usingMicrosoft.AspNetCore.Http;usingMicrosoft.AspNetCore.Mvc;usingMicrosoft.Azure.WebJobs;usingMicrosoft.Azure.WebJobs.Extensions.Http;usingMicrosoft.Extensions.Logging;usingServices;usingSystem.Threading.Tasks;namespaceCakeOrdersFunctions{publicclassSetupBakery{publicreadonlyIStoreFront_storeFront;publicreadonlyISupplier_supplier;publicreadonlyIBakery_bakery;publicreadonlyIManualBakery_manualBakery;publicSetupBakery(IStoreFrontstoreFront,ISuppliersupplier,IBakerybakery,IManualBakerymanualBakery){_storeFront=storeFront;_supplier=supplier;_bakery=bakery;_manualBakery=manualBakery;}[FunctionName("SetupBakery")]publicasyncTask<IActionResult>Run([HttpTrigger(AuthorizationLevel.Function,"get","post",Route=null)]HttpRequestreq,ILoggerlog){log.LogInformation("C# HTTP trigger function processed a request.");_storeFront.AddRegister("emp1");_storeFront.AddRegister("emp2");_supplier.AddIngredient("sugar");_supplier.AddIngredient("love");varstats=new{StoreFrontRegisters=_storeFront.GetAvailableRegisters(),SupplierIngedients=_supplier.GetIngredientCount(),BakerRegisters=_bakery.GetAvailableRegisters(),BakeryIngredients=_bakery.GetSupplierIngredientCount(),ManualBakeryRegisters=_manualBakery.GetAvailableRegisters(),ManualBakeryIngredients=_manualBakery.GetSupplierIngredientCount()};returnnewOkObjectResult(stats);}}}
I also replicated the service registration in an ASP.NET Core project to verify that this is an Azure Functions-specific issue.
The text was updated successfully, but these errors were encountered:
ScotlandBard
changed the title
Scoped Services Instantiated Multiple Times
Scoped Services Instantiated Multiple Times on Single Function Run
May 24, 2019
When registering a scoped service with an implementationFactory set, multiple instances of the service are being created per run of a function. To recreate this issue, I used the following Startup in a sample project:
Azure Function Code
Services:
Results (all values should be 2):
I also replicated the service registration in an ASP.NET Core project to verify that this is an Azure Functions-specific issue.
The text was updated successfully, but these errors were encountered: