dependency injection - Custom IServicePovider is not always used -
in startup.cs, want define extended serviceprovider: wraps default iserviceprovider implementation.
public iserviceprovider configureservices(iservicecollection services) { [...] var servicesprovider = services.buildextendedserviceprovider(); return servicesprovider; }
here core extended service provider implementation
/// <summary> /// extends native asp.net service provider /// </summary> public class extendedservicesprovider : iserviceprovider { private readonly iserviceprovider _serviceprovider; /// <summary> /// creates new instance of <see cref="extendedservicesprovider"/> provider based on native mvc <see cref="iserviceprovider"/> /// </summary> /// <param name="serviceprovider"></param> public extendedservicesprovider(iserviceprovider serviceprovider) { _serviceprovider = serviceprovider; } /// <inheritdoc /> public object getservice(type servicetype) { var resolvedservice = _serviceprovider.getservice(servicetype); [...] return resolvedservice; } }
at startup, can see getservice called each service "singleton" life time (great!!) however, not called anymore after that, , default serviceprovider called instead... way, if request resolution of iserviceprovider, native 1 get.
i serviceprovider replace native 1 , called every time.
did miss something??
ok, checked source code (i should have checked before posting question sorry). https://github.com/aspnet/dependencyinjection/blob/master/src/microsoft.extensions.dependencyinjection/serviceprovider.cs
the build method adds iserviceprovider type native instance. there seem have no way modify (without reflection on "_table" field mean :-). seems me approach not good.
i think i'll try other dependency injection engines such autofac, wanted keep native engine seemed me light weight.
Comments
Post a Comment