ServiceCreatorCallback Delegate

Definition

Provides a callback mechanism that can create an instance of a service on demand.

C#
public delegate object ServiceCreatorCallback(IServiceContainer container, Type serviceType);
C#
public delegate object? ServiceCreatorCallback(IServiceContainer container, Type serviceType);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public delegate object ServiceCreatorCallback(IServiceContainer container, Type serviceType);

Parameters

container
IServiceContainer

The service container that requested the creation of the service.

serviceType
Type

The type of service to create.

Return Value

The service specified by serviceType, or null if the service could not be created.

Attributes

Examples

The following code example shows how to publish a service using a callback function.

C#
// The following code shows how to publish a service using a callback function.

// Creates a service creator callback.
ServiceCreatorCallback callback1 = 
new ServiceCreatorCallback(myCallBackMethod);

// Adds the service using its type and the service creator callback.
serviceContainer.AddService(typeof(myService), callback1);

Remarks

ServiceCreatorCallback provides a mechanism to publish services that you can request to have created when needed, rather than the service being created immediately when the designer loads. You can use a callback function if the service is not essential and may not be used. A service published by using a ServiceCreatorCallback does not use as many additional resources if it is not requested and created. To use a callback function to publish your service, pass a ServiceCreatorCallback to the AddService method of an IServiceContainer.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also