IServiceContainer.AddService Method

Definition

Adds the specified service to the service container.

Overloads

AddService(Type, ServiceCreatorCallback)

Adds the specified service to the service container.

AddService(Type, Object)

Adds the specified service to the service container.

AddService(Type, ServiceCreatorCallback, Boolean)

Adds the specified service to the service container, and optionally promotes the service to parent service containers.

AddService(Type, Object, Boolean)

Adds the specified service to the service container, and optionally promotes the service to any parent service containers.

AddService(Type, ServiceCreatorCallback)

Adds the specified service to the service container.

C#
public void AddService(Type serviceType, System.ComponentModel.Design.ServiceCreatorCallback callback);

Parameters

serviceType
Type

The type of service to add.

callback
ServiceCreatorCallback

A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested.

Examples

The following example illustrates how to add a service to an IServiceContainer.

C#
m_MyServiceContainer.AddService(typeof(Control),
                new ServiceCreatorCallback(this.CreateNewControl));

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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

AddService(Type, Object)

Adds the specified service to the service container.

C#
public void AddService(Type serviceType, object serviceInstance);

Parameters

serviceType
Type

The type of service to add.

serviceInstance
Object

An instance of the service type to add. This object must implement or inherit from the type indicated by the serviceType parameter.

Examples

The following example illustrates how to add a service to an IServiceContainer.

C#
m_MyServiceContainer.AddService(typeof(Control), sender);

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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

AddService(Type, ServiceCreatorCallback, Boolean)

Adds the specified service to the service container, and optionally promotes the service to parent service containers.

C#
public void AddService(Type serviceType, System.ComponentModel.Design.ServiceCreatorCallback callback, bool promote);

Parameters

serviceType
Type

The type of service to add.

callback
ServiceCreatorCallback

A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested.

promote
Boolean

true to promote this request to any parent service containers; otherwise, false.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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

AddService(Type, Object, Boolean)

Adds the specified service to the service container, and optionally promotes the service to any parent service containers.

C#
public void AddService(Type serviceType, object serviceInstance, bool promote);

Parameters

serviceType
Type

The type of service to add.

serviceInstance
Object

An instance of the service type to add. This object must implement or inherit from the type indicated by the serviceType parameter.

promote
Boolean

true to promote this request to any parent service containers; otherwise, false.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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