IServiceProvider in ASP.NET Core

后端 未结 7 1827
被撕碎了的回忆
被撕碎了的回忆 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:35

    I don't think it is a good idea for an entity (or a model) to have access to any service.

    Controllers, on the other hand, do have access to any registered service in their constructors, and you don't have to worry about it.

    public class NotifyController : Controller
    {
        private static IEmailSender emailSender = null;
        protected static ISessionService session = null;
        protected static IMyContext dbContext = null;
        protected static IHostingEnvironment hostingEnvironment = null;
    
        public NotifyController(
                    IEmailSender mailSenderService,
                    IMyContext context,
                    IHostingEnvironment env,
                    ISessionService sessionContext)
        {
            emailSender = mailSenderService;
            dbContext = context;
            hostingEnvironment = env;
            session = sessionContext;
        }
    }
    

提交回复
热议问题