Class IServiceCollectionBasicExtensions

Defines basic extensions for the IServiceCollection interface.

Inheritance
System.Object
IServiceCollectionBasicExtensions
Namespace: Solti.Utils.DI.Interfaces
Assembly: Solti.Utils.DI.Interfaces.dll
Syntax
public static class IServiceCollectionBasicExtensions : Object

Methods

Decorate(IServiceCollection, Expression<DecoratorDelegate>)

Hooks into the instantiating process of last registered service. Useful when you want to add additional functionality (e.g. parameter validation):

using Solti.Utils.Proxy.Generators;
...
ScopeFactory.Create
(
    svcs => svcs
        .Service<IMyService, MyService>()
        .Decorate((scope, type, instance) => ProxyGenerator<IMyService, ParameterValidatorInterceptor<IMyService>>.Activate(Tuple.Create(instance))),
    ...
)
Declaration
public static IServiceCollection Decorate(this IServiceCollection self, Expression<DecoratorDelegate> decorator)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Linq.Expressions.Expression<DecoratorDelegate> decorator

The decorator funtion. It must return the decorated instance. The original instance can be accessed via the 3rd parameter of decorator function.

Returns
Type Description
IServiceCollection
Remarks
Exceptions
Type Condition
System.InvalidOperationException

When proxying not allowed (see above).

Decorate(IServiceCollection, Type, Expression<DecoratorDelegate>)

Hooks into the instantiating process of a registered service. Useful when you want to add additional functionality (e.g. parameter validation):

using Solti.Utils.Proxy.Generators;
...
ScopeFactory.Create
(
    svcs => svcs
        .Service<IMyService, MyService>()
        .Decorate(typeof(IMyService), (scope, type, instance) => ProxyGenerator<IMyService, ParameterValidatorInterceptor<IMyService>>.Activate(Tuple.Create(instance))),
    ...
)
Declaration
public static IServiceCollection Decorate(this IServiceCollection self, Type type, Expression<DecoratorDelegate> decorator)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type.

System.Linq.Expressions.Expression<DecoratorDelegate> decorator

The decorator funtion. It must return the decorated instance. The original instance can be accessed via the 3rd parameter of the decorator function.

Returns
Type Description
IServiceCollection
Remarks
Exceptions
Type Condition
System.InvalidOperationException

When proxying not allowed (see above).

Decorate(IServiceCollection, Type, Object, Expression<DecoratorDelegate>)

Hooks into the instantiating process of a registered service. Useful when you want to add additional functionality (e.g. parameter validation):

using Solti.Utils.Proxy.Generators;
...
ScopeFactory.Create
(
    svcs => svcs
        .Service<IMyService, MyService>("svcName")
        .Decorate(typeof(IMyService), "svcName", (scope, type, instance) => ProxyGenerator<IMyService, ParameterValidatorInterceptor<IMyService>>.Activate(Tuple.Create(instance))),
    ...
)
Declaration
public static IServiceCollection Decorate(this IServiceCollection self, Type type, object key, Expression<DecoratorDelegate> decorator)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type.

System.Object key

The (optional) service key.

System.Linq.Expressions.Expression<DecoratorDelegate> decorator

The decorator funtion. It must return the decorated instance. The original instance can be accessed via the 3rd parameter of decorator function.

Returns
Type Description
IServiceCollection
Remarks
Exceptions
Type Condition
System.InvalidOperationException

When proxying not allowed (see above).

Decorate<TType>(IServiceCollection, Expression<DecoratorDelegate<TType>>)

Hooks into the instantiating process of a registered service. Useful when you want to add additional functionality (e.g. parameter validation):

using Solti.Utils.Proxy.Generators;
...
ScopeFactory.Create
(
    svcs => svcs
        .Service<IMyService, MyService>()
        .Decorate<IMyService>((scope, instance) => ProxyGenerator<IMyService, ParameterValidatorInterceptor<IMyService>>.Activate(Tuple.Create(instance))),
    ...
)
Declaration
public static IServiceCollection Decorate<TType>(this IServiceCollection self, Expression<DecoratorDelegate<TType>> decorator)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Linq.Expressions.Expression<DecoratorDelegate<TType>> decorator

The decorator funtion. It must return the decorated instance. The original instance can be accessed via the 3rd parameter of the decorator function.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType
Remarks
Exceptions
Type Condition
System.InvalidOperationException

When proxying not allowed (see above).

Decorate<TType>(IServiceCollection, Object, Expression<DecoratorDelegate<TType>>)

Hooks into the instantiating process of a registered service. Useful when you want to add additional functionality (e.g. parameter validation):

using Solti.Utils.Proxy.Generators;
...
ScopeFactory.Create
(
    svcs => svcs
        .Service<IMyService, MyService>("svcName")
        .Decorate<IMyService>("svcName", (scope, instance) => ProxyGenerator<IMyService, ParameterValidatorInterceptor<IMyService>>.Activate(Tuple.Create(instance))),
    ...
)
Declaration
public static IServiceCollection Decorate<TType>(this IServiceCollection self, object key, Expression<DecoratorDelegate<TType>> decorator)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) service name.

System.Linq.Expressions.Expression<DecoratorDelegate<TType>> decorator

The decorator funtion. It must return the decorated instance. The original instance can be accessed via the 3rd parameter of the decorator function.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType
Remarks
Exceptions
Type Condition
System.InvalidOperationException

When proxying not allowed (see above).

Factory(IServiceCollection, Type, FactoryDelegate, LifetimeBase, ServiceOptions)

Registers a new service factory with the given type. Factories are also services except that the instantiating process is delegated to the caller. Useful if a service has more than one constructor:

ScopeFactory.Create
(
    svcs => svcs.Factory(typeof(IMyService), (scope, type) => new MyService(...), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Factory(this IServiceCollection self, Type type, FactoryDelegate factory, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once.

FactoryDelegate factory

The factory function that is responsible for the instantiation. Its call count depends on the value of the lifetime parameter. Note that the second parameter of the factory is never generic, even if you registered the factory for an open generic interface.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

Factory(IServiceCollection, Type, Expression<FactoryDelegate>, LifetimeBase, ServiceOptions)

Registers a new service factory with the given type. Factories are also services except that the instantiating process is delegated to the caller. Useful if a service has more than one constructor:

ScopeFactory.Create
(
    svcs => svcs.Factory(typeof(IMyService), (scope, type) => new MyService(...), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Factory(this IServiceCollection self, Type type, Expression<FactoryDelegate> factoryExpr, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once.

System.Linq.Expressions.Expression<FactoryDelegate> factoryExpr

The factory function that is responsible for the instantiation. Its call count depends on the value of the lifetime parameter. Note that the second parameter of the factoryExpr is never generic, even if you registered the factory for an open generic interface.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You can register generic services (where the type parameter is an open generic type).

Factory(IServiceCollection, Type, Object, FactoryDelegate, LifetimeBase, ServiceOptions)

Registers a new service factory with the given type. Factories are also services except that the instantiating process is delegated to the caller. Useful if a service has more than one constructor:

ScopeFactory.Create
(
    svcs => svcs.Factory(typeof(IMyService), "serviceName", (scope, type) => new MyService(...), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Factory(this IServiceCollection self, Type type, object key, FactoryDelegate factory, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once (with the given key).

System.Object key

The (optional) service key (usually a name).

FactoryDelegate factory

The factory function that is responsible for the instantiation. Its call count depends on the value of the lifetime parameter. Note that the second parameter of the factory is never generic, even if you registered the factory for an open generic interface.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

Factory(IServiceCollection, Type, Object, Expression<FactoryDelegate>, LifetimeBase, ServiceOptions)

Registers a new service factory with the given type. Factories are also services except that the instantiating process is delegated to the caller. Useful if a service has more than one constructor:

ScopeFactory.Create
(
    svcs => svcs.Factory(typeof(IMyService), "serviceName", (scope, type) => new MyService(...), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Factory(this IServiceCollection self, Type type, object key, Expression<FactoryDelegate> factoryExpr, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once (with the given key).

System.Object key

The (optional) service key (usually a name).

System.Linq.Expressions.Expression<FactoryDelegate> factoryExpr

The factory function that is responsible for the instantiation. Its call count depends on the value of the lifetime parameter. Note that the second parameter of the factoryExpr is never generic, even if you registered the factory for an open generic interface.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You can register generic services (where the type parameter is an open generic type).

Factory<TType>(IServiceCollection, FactoryDelegate<TType>, LifetimeBase, ServiceOptions)

Registers a new service factory with the given type. Factories are also services except that the instantiating process is delegated to the caller. Useful if a service has more than one constructor:

ScopeFactory.Create
(
    svcs => svcs.Factory<IMyService>(scope => new MyService(...), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Factory<TType>(this IServiceCollection self, FactoryDelegate<TType> factory, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

FactoryDelegate<TType> factory

The factory function that is responsible for the instantiation. Its call count depends on the value of the lifetime parameter.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once.

Remarks

Factory<TType>(IServiceCollection, Expression<FactoryDelegate<TType>>, LifetimeBase, ServiceOptions)

Registers a new service factory with the given type. Factories are also services except that the instantiating process is delegated to the caller. Useful if a service has more than one constructor:

ScopeFactory.Create
(
    svcs => svcs.Factory<IMyService>(scope => new MyService(...), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Factory<TType>(this IServiceCollection self, Expression<FactoryDelegate<TType>> factoryExpr, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Linq.Expressions.Expression<FactoryDelegate<TType>> factoryExpr

The factory function that is responsible for the instantiation. Its call count depends on the value of the lifetime parameter.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once.

Factory<TType>(IServiceCollection, Object, FactoryDelegate<TType>, LifetimeBase, ServiceOptions)

Registers a new service factory with the given type. Factories are also services except that the instantiating process is delegated to the caller. Useful if a service has more than one constructor:

ScopeFactory.Create
(
    svcs => svcs.Factory<IMyService>("serviceName", scope => new MyService(...), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Factory<TType>(this IServiceCollection self, object key, FactoryDelegate<TType> factory, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) service key (usually a name).

FactoryDelegate<TType> factory

The factory function that is responsible for the instantiation. Its call count depends on the value of the lifetime parameter.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once (with the given key).

Remarks

Factory<TType>(IServiceCollection, Object, Expression<FactoryDelegate<TType>>, LifetimeBase, ServiceOptions)

Registers a new service factory with the given type. Factories are also services except that the instantiating process is delegated to the caller. Useful if a service has more than one constructor:

ScopeFactory.Create
(
    svcs => svcs.Factory<IMyService>("serviceName", scope => new MyService(...), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Factory<TType>(this IServiceCollection self, object key, Expression<FactoryDelegate<TType>> factoryExpr, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) service key (usually a name).

System.Linq.Expressions.Expression<FactoryDelegate<TType>> factoryExpr

The factory function that is responsible for the instantiation. Its call count depends on the value of the lifetime parameter.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once (with the given key).

Find(IServiceCollection, Type)

Tries to find a service descriptor (AbstractServiceEntry) in the given collection.

Declaration
public static AbstractServiceEntry Find(this IServiceCollection self, Type type)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type.

Returns
Type Description
AbstractServiceEntry

The service descriptor.

Exceptions
Type Condition
System.ArgumentNullException

Some of the passed arguments is null.

ServiceNotFoundException

When the requested service could not be found.

Find(IServiceCollection, Type, Object)

Tries to find a service descriptor (AbstractServiceEntry) in the given collection.

Declaration
public static AbstractServiceEntry Find(this IServiceCollection self, Type type, object key)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type.

System.Object key

The (optional) service key (usually a name).

Returns
Type Description
AbstractServiceEntry

The service descriptor.

Exceptions
Type Condition
System.ArgumentNullException

Some of the passed arguments is null.

ServiceNotFoundException

The requested service could not be found.

Find<TType>(IServiceCollection)

Tries to find a service descriptor (AbstractServiceEntry) in the given collection.

Declaration
public static AbstractServiceEntry Find<TType>(this IServiceCollection self)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

Returns
Type Description
AbstractServiceEntry

The service descriptor.

Type Parameters
Name Description
TType
Exceptions
Type Condition
System.ArgumentNullException

Some of the passed arguments is null.

ServiceNotFoundException

When the requested service could not be found.

Find<TType>(IServiceCollection, Object)

Tries to find a service descriptor (AbstractServiceEntry) in the given collection.

Declaration
public static AbstractServiceEntry Find<TType>(this IServiceCollection self, object key)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) service key (usually a name).

Returns
Type Description
AbstractServiceEntry

The service descriptor.

Type Parameters
Name Description
TType
Exceptions
Type Condition
System.ArgumentNullException

Some of the passed arguments is null.

ServiceNotFoundException

When the requested service could not be found.

Provider(IServiceCollection, Type, Object, Type, LifetimeBase, ServiceOptions)

Registers a new service provider. Providers are "factory services" responsible for creating the concrete service.

class MyServiceProvider: IServiceProvider
{
    [Inject]
    public IDependency Depdendency { get; init; }
    public object GetService(Type type) => ...;
}
...
ScopeFactory.Create
(
    svcs => svcs.Provider(typeof(IMyService), "serviceName", typeof(MyServiceProvider), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Provider(this IServiceCollection self, Type type, object key, Type provider, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type. This parameter will be forwarded to the provider.

System.Object key

The (optional) service key (usually a name).

System.Type provider

The type of the provider. It may have dependencies and must implement the System.IServiceProvider interface.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

The provided System.IServiceProvider implementation won't be disposed even if it implements the System.IDisposable interface.

Provider(IServiceCollection, Type, Object, Type, Object, LifetimeBase, ServiceOptions)

Registers a new service provider. Providers are "factory services" responsible for creating the concrete service:

class MyServiceProvider: IServiceProvider
{
    public int Depdendency { get; init; }
    public object GetService(Type type) => ...;
}
...
ScopeFactory.Create
(
    svcs => svcs.Provider(typeof(IMyService), "serviceName", typeof(MyServiceProvider), new { Depdendency = 1986 }, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Provider(this IServiceCollection self, Type type, object key, Type provider, object explicitArgs, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type. This parameter will be forwarded to the provider.

System.Object key

The (optional) service key (usually a name).

System.Type provider

The type of the provider. It may have dependencies and must implement the System.IServiceProvider interface.

System.Object explicitArgs

Explicit arguments, provided by the user.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

The provided System.IServiceProvider implementation won't be disposed even if it implements the System.IDisposable interface.

Provider(IServiceCollection, Type, Type, LifetimeBase, ServiceOptions)

Registers a new service provider. Providers are "factory services" responsible for creating the concrete service:

class MyServiceProvider: IServiceProvider
{
    [Inject]
    public IDependency Depdendency { get; init; }
    public object GetService(Type type) => ...;
}
...
ScopeFactory.Create
(
    svcs => svcs.Provider(typeof(IMyService), typeof(MyServiceProvider), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Provider(this IServiceCollection self, Type type, Type provider, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type. This parameter will be forwarded to the provider.

System.Type provider

The type of the provider. It may have dependencies and must implement the System.IServiceProvider interface.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

The provided System.IServiceProvider implementation won't be disposed even if it implements the System.IDisposable interface.

Provider(IServiceCollection, Type, Type, Object, LifetimeBase, ServiceOptions)

Registers a new service provider having non-interface dependency. Providers are "factory services" responsible for creating the concrete service:

class MyServiceProvider: IServiceProvider
{
    public int Depdendency { get; init; }
    public object GetService(Type type) => ...;
}
...
ScopeFactory.Create
(
    svcs => svcs.Provider(typeof(IMyService), typeof(MyServiceProvider), new { Depdendency = 1986 }, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Provider(this IServiceCollection self, Type type, Type provider, object explicitArgs, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type. This parameter will be forwarded to the provider.

System.Type provider

The type of the provider. It may have dependencies and must implement the System.IServiceProvider interface.

System.Object explicitArgs

Explicit arguments, provided by the user.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

The provided System.IServiceProvider implementation won't be disposed even if it implements the System.IDisposable interface.

Provider<TType, TProvider>(IServiceCollection, LifetimeBase, ServiceOptions)

Registers a new service provider. Providers are "factory services" responsible for creating the concrete service:

class MyServiceProvider: IServiceProvider
{
    [Inject]
    public IDependency Depdendency { get; init; }
    public object GetService(Type type) => ...;
}
...
ScopeFactory.Create
(
    svcs => svcs.Provider<IMyService, MyServiceProvider>(Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Provider<TType, TProvider>(this IServiceCollection self, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class where TProvider : class, IServiceProvider
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type. It will be forwarded to the provider.

TProvider

The type of the provider. It may have dependencies and must implement the System.IServiceProvider interface.

Remarks

The provided System.IServiceProvider implementation won't be disposed even if it implements the System.IDisposable interface.

Provider<TType, TProvider>(IServiceCollection, Object, LifetimeBase, ServiceOptions)

Registers a new service provider. Providers are "factory services" responsible for creating the concrete service:

class MyServiceProvider: IServiceProvider
{
    [Inject]
    public IDependency Depdendency { get; init; }
    public object GetService(Type type) => ...;
}
...
ScopeFactory.Create
(
    svcs => svcs.Provider<IMyService, MyServiceProvider>("serviceName", Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Provider<TType, TProvider>(this IServiceCollection self, object key, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class where TProvider : class, IServiceProvider
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) service key (usually a name)

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type. It will be forwarded to the provider.

TProvider

The type of the provider. It may have dependencies and must implement the System.IServiceProvider interface.

Remarks

The provided System.IServiceProvider implementation won't be disposed even if it implements the System.IDisposable interface.

Provider<TType, TProvider>(IServiceCollection, Object, Object, LifetimeBase, ServiceOptions)

Registers a new service provider having non-interface dependency. Providers are "factory services" responsible for creating the concrete service:

class MyServiceProvider: IServiceProvider
{
    public int Depdendency { get; init; }
    public object GetService(Type type) => ...;
}
...
ScopeFactory.Create
(
    svcs => svcs.Provider<IMyService, MyServiceProvider>("serviceName", new { Depdendency = 1986 }, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Provider<TType, TProvider>(this IServiceCollection self, object key, object explicitArgs, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class where TProvider : class, IServiceProvider
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) service key (usually a name)

System.Object explicitArgs

Explicit arguments, provided by the user.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type. It will be forwarded to the provider.

TProvider

The type of the provider. It may have dependencies and must implement the System.IServiceProvider interface.

Remarks

The provided System.IServiceProvider implementation won't be disposed even if it implements the System.IDisposable interface.

Register(IServiceCollection, AbstractServiceEntry[])

Registers a set of services.

ScopeFactory.Create
(
    svcs => svcs.Register
    (
        new SingletonServiceEntry(...)
    ),
    ...
)
Declaration
public static IServiceCollection Register(this IServiceCollection self, params AbstractServiceEntry[] entries)
Parameters
Type Name Description
IServiceCollection self
AbstractServiceEntry[] entries
Returns
Type Description
IServiceCollection
Remarks

Using the recepie methods (Service, Factory, etc) is more convenient than registering services directly.

Register(IServiceCollection, IEnumerable<AbstractServiceEntry>)

Registers a set of services.

ScopeFactory.Create
(
    svcs => svcs.Register
    (
        new List<AbstractServiceEntry>
        {
            new SingletonServiceEntry(...)
        }
    ),
    ...
)
Declaration
public static IServiceCollection Register(this IServiceCollection self, IEnumerable<AbstractServiceEntry> entries)
Parameters
Type Name Description
IServiceCollection self
System.Collections.Generic.IEnumerable<AbstractServiceEntry> entries
Returns
Type Description
IServiceCollection
Remarks

Using the recepie methods (Service, Factory, etc) is more convenient than registering services directly.

Remove(IServiceCollection, Type)

Removes the AbstractServiceEntry associated with the given type.

Declaration
public static IServiceCollection Remove(this IServiceCollection self, Type type)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type.

Returns
Type Description
IServiceCollection
Remarks

This method uses linear search so should be avoided in perfomance critical places.

Exceptions
Type Condition
ServiceNotFoundException

The service could not be found.

Remove(IServiceCollection, Type, Object)

Removes the AbstractServiceEntry associated with the given type and (optional) key.

Declaration
public static IServiceCollection Remove(this IServiceCollection self, Type type, object key)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type.

System.Object key

The (optional) service key (usually a name).

Returns
Type Description
IServiceCollection
Remarks

This method uses linear search so should be avoided in perfomance critical places.

Exceptions
Type Condition
ServiceNotFoundException

The service could not be found.

Remove<TType>(IServiceCollection)

Removes the AbstractServiceEntry associated with the given TType.

Declaration
public static IServiceCollection Remove<TType>(this IServiceCollection self)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type.

Remarks

This method uses linear search so should be avoided in perfomance critical places.

Exceptions
Type Condition
ServiceNotFoundException

The service could not be found.

Remove<TType>(IServiceCollection, Object)

Removes the AbstractServiceEntry associated with the given TType and (optional) key.

Declaration
public static IServiceCollection Remove<TType>(this IServiceCollection self, object key)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) service key (usually a name).

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type.

Remarks

This method uses linear search so should be avoided in perfomance critical places.

Exceptions
Type Condition
ServiceNotFoundException

The service could not be found.

Service(IServiceCollection, Type, LifetimeBase, ServiceOptions)

Registers a new service:

ScopeFactory.Create
(
    svcs => svcs.Service(typeof(MyService), "serviceName", Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service(this IServiceCollection self, Type type, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once. Must be a class.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You may register generic services. The system will specialize the implementation if you request the concrete service.

Service(IServiceCollection, Type, Object, LifetimeBase, ServiceOptions)

Registers a new named service:

ScopeFactory.Create
(
    svcs => svcs.Service(typeof(MyService), "serviceName", Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service(this IServiceCollection self, Type type, object key, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once (with the given key). Must be a class.

System.Object key

The (optional) key of the service (usually a name).

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You may register generic services. The system will specialize the implementation if you request the concrete service.

Service(IServiceCollection, Type, Object, Object, LifetimeBase, ServiceOptions)

Registers a new named service using arbitrary constructor arguments:

ScopeFactory.Create
(
    svcs => svcs.Service(typeof(MyService), "serviceName", new {ctorParam = ...}, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service(this IServiceCollection self, Type type, object key, object explicitArgs, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once (with the given key). Must be a class.

System.Object key

The (optional) key of the service (usually a name).

System.Object explicitArgs

Explicit arguments, provided by the user (may be an anonym object or a System.Collections.Generic.IReadOnlyDictionary`2 where the key is System.String and value is System.Object).

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You may register generic services. The system will specialize the implementation if you request the concrete service.

Service(IServiceCollection, Type, Object, Type, LifetimeBase, ServiceOptions)

Registers a new named service with the given implementation:

ScopeFactory.Create
(
    svcs => svcs.Service(typeof(IMyService), "serviceName", typeof(MyService), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service(this IServiceCollection self, Type type, object key, Type implementation, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once (with the given key).

System.Object key

The (optional) key of the service (usually a name).

System.Type implementation

The service implementation to be registered. It can not be null and must implement the service type. Additionally it must have only null or one constructor (that may request another dependecies). In case of multiple constructors you can use the Factory(IServiceCollection, Type, Object, Expression<FactoryDelegate>, LifetimeBase, ServiceOptions) method or the ServiceActivatorAttribute.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You may register generic services (where both the interface and the implementation are open generic types). The system will specialize the implementation if you request the concrete service.

Service(IServiceCollection, Type, Object, Type, Object, LifetimeBase, ServiceOptions)

Registers a new named service using arbitrary constructor arguments:

ScopeFactory.Create
(
    svcs => svcs.Service(typeof(IMyService), "serviceName", typeof(MyService), new {ctorParam = ...}, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service(this IServiceCollection self, Type type, object key, Type implementation, object explicitArgs, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once (with the given key).

System.Object key

The (optional) key of the service (usually a name).

System.Type implementation

The service implementation to be registered. It can not be null and must implement the service type. Additionally it should have only one public constructor (that may request another dependecies). In case of multiple constructors you can use the Factory(IServiceCollection, Type, Expression<FactoryDelegate>, LifetimeBase, ServiceOptions) method or the ServiceActivatorAttribute.

System.Object explicitArgs

Explicit arguments, provided by the user (may be an anonym object or a System.Collections.Generic.IReadOnlyDictionary`2 where the key is System.String and value is System.Object).

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You may register generic services (where both the interface and the implementation are open generic types). The system will specialize the implementation if you request the concrete service.

Service(IServiceCollection, Type, Type, LifetimeBase, ServiceOptions)

Registers a new service with the given implementation:

ScopeFactory.Create
(
    svcs => svcs.Service(typeof(IMyService), typeof(MyService), Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service(this IServiceCollection self, Type type, Type implementation, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once.

System.Type implementation

The service implementation to be registered. It can not be null and must implement the service type. Additionally it should have only one constructor (that may request another dependecies). In case of multiple constructors you can use the Factory(IServiceCollection, Type, Expression<FactoryDelegate>, LifetimeBase, ServiceOptions) method or the ServiceActivatorAttribute.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You may register generic services (where both the interface and the implementation are open generic types). The system will specialize the implementation if you request the concrete service.

Service(IServiceCollection, Type, Type, Object, LifetimeBase, ServiceOptions)

Registers a new service using arbitrary constructor arguments:

ScopeFactory.Create
(
    svcs => svcs.Service(typeof(IMyService), typeof(MyService), new {ctorParam = ...}, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service(this IServiceCollection self, Type type, Type implementation, object explicitArgs, LifetimeBase lifetime, ServiceOptions options = null)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type to be registered. It can not be null and can be registered only once.

System.Type implementation

The service implementation to be registered. It can not be null and must implement the service type. Additionally it should have only one constructor (that may request another dependecies). In case of multiple constructors you can use the Factory(IServiceCollection, Type, Expression<FactoryDelegate>, LifetimeBase, ServiceOptions) method or the ServiceActivatorAttribute.

System.Object explicitArgs

Explicit arguments, provided by the user (may be an anonym object or a System.Collections.Generic.IReadOnlyDictionary`2 where the key is System.String and value is System.Object).

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Remarks

You may register generic services (where both the interface and the implementation are open generic types). The system will specialize the implementation if you request the concrete service.

Service<TType>(IServiceCollection, LifetimeBase, ServiceOptions)

Registers a new service:

ScopeFactory.Create
(
    svcs => svcs.Service<MyService>(new {ctorParam = ...}, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service<TType>(this IServiceCollection self, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once. Must be a class.

Service<TType>(IServiceCollection, Object, LifetimeBase, ServiceOptions)

Registers a new named service:

ScopeFactory.Create
(
    svcs => svcs.Service<MyService>("serviceName", Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service<TType>(this IServiceCollection self, object key, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) key of the service (usually a name).

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once (with the given key). Must be a class.

Service<TType>(IServiceCollection, Object, Object, LifetimeBase, ServiceOptions)

Registers a new named service using arbitrary constructor arguments:

ScopeFactory.Create
(
    svcs => svcs.Service<MyService>("serviceName", new {ctorParam = ...}, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service<TType>(this IServiceCollection self, object key, object explicitArgs, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) key of the service (usually a name).

System.Object explicitArgs

Explicit arguments, provided by the user (may be an anonym object or a System.Collections.Generic.IReadOnlyDictionary`2 where the key is System.String and value is System.Object).

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once (with the given key). Must be a class.

Service<TType, TImplementation>(IServiceCollection, LifetimeBase, ServiceOptions)

Registers a new service with the given implementation:

ScopeFactory.Create
(
    svcs => svcs.Service<IMyService, MyService>(Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service<TType, TImplementation>(this IServiceCollection self, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class where TImplementation : TType
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once.

TImplementation

The service implementation to be registered. It must implement the service TType and should have only one public constructor (that may request another dependecies). In case of multiple constructors you can use the Factory<TType>(IServiceCollection, Expression<FactoryDelegate<TType>>, LifetimeBase, ServiceOptions) method or the ServiceActivatorAttribute.

Service<TType, TImplementation>(IServiceCollection, Object, LifetimeBase, ServiceOptions)

Registers a new named service with the given implementation:

ScopeFactory.Create
(
    svcs => svcs.Service<IMyService, MyService>("serviceName", Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service<TType, TImplementation>(this IServiceCollection self, object key, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class where TImplementation : TType
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) key of the service (usually a name).

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once (with the given key).

TImplementation

The service implementation to be registered. It must implement the service TType and must have only null or one constructor (that may request another dependecies). In case of multiple constructors you can use the Factory<TType>(IServiceCollection, Expression<FactoryDelegate<TType>>, LifetimeBase, ServiceOptions) method or the ServiceActivatorAttribute.

Service<TType, TImplementation>(IServiceCollection, Object, Object, LifetimeBase, ServiceOptions)

Registers a new named service using arbitrary constructor arguments:

ScopeFactory.Create
(
    svcs => svcs.Service<IMyService, MyService>("serviceName", new {ctorParam = ...}, Lifetime.Singleton),
    ...
)
Declaration
public static IServiceCollection Service<TType, TImplementation>(this IServiceCollection self, object key, object explicitArgs, LifetimeBase lifetime, ServiceOptions options = null)
    where TType : class where TImplementation : TType
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) key of the service (usually a name).

System.Object explicitArgs

Explicit arguments, provided by the user (may be an anonym object or a System.Collections.Generic.IReadOnlyDictionary`2 where the key is System.String and value is System.Object).

LifetimeBase lifetime

The lifetime of service.

ServiceOptions options

Options to be assigned to the service being registered.

Returns
Type Description
IServiceCollection
Type Parameters
Name Description
TType

The service type to be registered. It can be registered only once (with the given key).

TImplementation

The service implementation to be registered. It must implement the service TType and must have only null or one constructor (that may request another dependecies). In case of multiple constructors you can use the Factory<TType>(IServiceCollection, Expression<FactoryDelegate<TType>>, LifetimeBase, ServiceOptions) method or the ServiceActivatorAttribute.

TryFind(IServiceCollection, Type)

Tries to find a service descriptor (AbstractServiceEntry) in the given collection.

Declaration
public static AbstractServiceEntry TryFind(this IServiceCollection self, Type type)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type.

Returns
Type Description
AbstractServiceEntry

The service descriptor.

Exceptions
Type Condition
System.ArgumentNullException

Some of the passed arguments is null.

TryFind(IServiceCollection, Type, Object)

Tries to find a service descriptor (AbstractServiceEntry) in the given collection.

Declaration
public static AbstractServiceEntry TryFind(this IServiceCollection self, Type type, object key)
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Type type

The service type.

System.Object key

The (optional) service key (usually a name).

Returns
Type Description
AbstractServiceEntry

The service descriptor.

Exceptions
Type Condition
System.ArgumentNullException

Some of the passed arguments is null.

TryFind<TType>(IServiceCollection)

Tries to find a service descriptor (AbstractServiceEntry) in the given collection.

Declaration
public static AbstractServiceEntry TryFind<TType>(this IServiceCollection self)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

Returns
Type Description
AbstractServiceEntry

The service descriptor.

Type Parameters
Name Description
TType
Exceptions
Type Condition
System.ArgumentNullException

Some of the passed arguments is null.

ServiceNotFoundException

When the requested service could not be found.

TryFind<TType>(IServiceCollection, Object)

Tries to find a service descriptor (AbstractServiceEntry) in the given collection.

Declaration
public static AbstractServiceEntry TryFind<TType>(this IServiceCollection self, object key)
    where TType : class
Parameters
Type Name Description
IServiceCollection self

The target IServiceCollection.

System.Object key

The (optional) service key (usually a name).

Returns
Type Description
AbstractServiceEntry

The service descriptor.

Type Parameters
Name Description
TType
Exceptions
Type Condition
System.ArgumentNullException

Some of the passed arguments is null.

In This Article
Back to top Generated by DocFX