Skip to content

)} ,System.String)}

IServiceLocator.AddService(Func) method

Defined in

Type: IServiceLocator Namespace: Splat Assembly: Splat.Core.dll

Applies to

net10.0, net10.0-browserwasm1.0, net10.0-desktop1.0, net9.0, net9.0-browserwasm1.0, net9.0-desktop1.0, net8.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.2, net8.0-macos14.5, net8.0-tvos17.2, netstandard2.1, net462, net481, net471

Overloads

  • 1. void AddService<T>(Func<T> instanceFactory)
  • 2. void AddService<T>(Func<T> instanceFactory, string contract)

1. Overload

void AddService<T>(Func<T> instanceFactory)

Summary: Registers a transient service of the specified type using the provided factory function.

Type parameters

NameDescription
TThe type of the service to register.

Parameters

NameTypeDescription
instanceFactoryFuncA function that returns an instance of the service type. This function is called each time an instance is requested.

Remarks

Use this method to register services that require custom instantiation logic or dependencies not handled by default constructors. The factory function should not return null.

2. Overload

void AddService<T>(Func<T> instanceFactory, string contract)

Summary: Registers a transient service implementation with the specified contract, using the provided factory to create instances of the service type.

Type parameters

NameDescription
TThe type of the service to register.

Parameters

NameTypeDescription
instanceFactoryFuncA factory function that creates instances of the service type. Cannot be null.
contractstringThe contract name under which the service is registered. Cannot be null or empty.

Remarks

Use this method to associate a service implementation with a specific contract, allowing for multiple implementations of the same service type to be registered under different contracts.