c# - Getting IConfiguration from ServiceCollection -
i´m writing own extension method servicecollection
registered types of module , need access iconfiguration
instance collection register options.
extension method
public static iservicecollection addapi(this iservicecollection services) { // configuration collection var configuration = (iconfiguration) services.firstordefault(p => p.servicetype == typeof(iconfiguration)).implementationinstance; services.configure<databaseoptions>(configuration.getsection("database")); }
is right way iconfiguration
instance collection or there more elegant solution? not want add iconfiguration instance parameter method.
i created own "service collection" type wrapped iservicecollection
, iconfiguration
, modules use type register services. example:
public interface imyservicecollection { public iservicecollection services { get; set; } public iconfiguration configuration { get; set; } } public static void addfoomodule(this imyservicecollection myservices) { var services = myservices.services; var config = myservices.configuration; }
then have create extension method configuration instance parameter creates implementation imyservicecollection
, such as:
public static imyservicecollection createservicecollection(this iservicecollection services, iconfiguration config) { return new myservicecollection { services = services, configuration = config }; }
note using in modular framework. simple applications overkill.
i think solution fine well. if need access iconfiguration
instance might find searching in service collection on , on bit tedious.
Comments
Post a Comment