ServiceDescription.GetService Method

Definition

Returns a service description initialized with a specified service object or type.

Overloads

GetService(Object)

Returns a service description initialized with a specified service object.

GetService(Type)

Returns a service description initialized with a specified service type.

GetService(Object)

Returns a service description initialized with a specified service object.

C#
public static System.ServiceModel.Description.ServiceDescription GetService(object serviceImplementation);

Parameters

serviceImplementation
Object

The Object that implements the service.

Returns

The ServiceDescription for the service object provided.

Exceptions

serviceImplementation is null.

Examples

The following sample shows how to iterate through the service behaviors contained in the service description.

C#
// Instantiate a service description specifying a service object
// Note: Endpoints collection and other properties will be null since
// we have not specified them
CalculatorService svcObj = new CalculatorService();
ServiceDescription sd3 = ServiceDescription.GetService(svcObj);
String serviceName = sd3.Name;
Console.WriteLine("Service name: {0}", serviceName);
C#
ServiceDescription d = ServiceDescription.GetService(typeof(CalculatorService));
foreach (IServiceBehavior isb in d.Behaviors)
{
    Console.WriteLine(isb.GetType());
}
Console.WriteLine();

Remarks

Use this method to reflect on behaviors using the Windows Communication Foundation (WCF) programming model when replacing ServiceHostBase with you own hosting mechanism.

GetService(Object) initializes the Name, Namespace, and ConfigurationName properties, and ensures that the ServiceBehaviorAttribute is attached to the service and adds any relevant service behaviors to Behaviors.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 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

GetService(Type)

Returns a service description initialized with a specified service type.

C#
public static System.ServiceModel.Description.ServiceDescription GetService(Type serviceType);

Parameters

serviceType
Type

The Type of the service.

Returns

The ServiceDescription for the service type provided.

Exceptions

serviceType is null.

Examples

C#
ServiceDescription d = ServiceDescription.GetService(new CalculatorService());
foreach (IServiceBehavior isb in d.Behaviors)
{
    Console.WriteLine(isb.GetType());
}
Console.WriteLine();

Remarks

Use this method to reflect on behaviors using the Windows Communication Foundation (WCF) programming model when replacing ServiceHostBase with you own hosting mechanism.

GetService(Type) initializes the Name, Namespace, and ConfigurationName properties, reflects on the type for any the ServiceBehaviorAttribute objects and adds them to Behaviors, and ensures that the ServiceBehaviorAttribute is attached to the service.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 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