IServiceProvider in ASP.NET Core

后端 未结 7 1811
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 08:02

I starting to learn changes in ASP.NET 5(vNext) and cannot find how to get IServiceProvider, for example in \"Model\"\'s method

public class Entity 
{
     publ         


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 08:28

    use GetRequiredService instead of GetService, like the example on ASP.NET Core tutorials ( https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/working-with-sql )

    documentation on the method:

    https://docs.microsoft.com/en-us/aspnet/core/api/microsoft.extensions.dependencyinjection.serviceproviderserviceextensions#Microsoft_Extensions_DependencyInjection_ServiceProviderServiceExtensions_GetRequiredService__1_System_IServiceProvider_

    using Microsoft.Extensions.DependencyInjection;
    
          using (var context = new ApplicationDbContext(serviceProvicer.GetRequiredService>()))
    

提交回复
热议问题