ServiceDescription.Behaviors Property

Definition

Gets the behaviors associated with the service.

C#
public System.Collections.Generic.KeyedByTypeCollection<System.ServiceModel.Description.IServiceBehavior> Behaviors { get; }

Property Value

The KeyedByTypeCollection<TItem> of type IServiceBehavior that contains the behaviors associated with the service.

Examples

C#
// Iterate through the list of behaviors in the ServiceDescription
ServiceDescription svcDesc = serviceHost.Description;
KeyedByTypeCollection<IServiceBehavior> sbCol = svcDesc.Behaviors;
foreach (IServiceBehavior behavior in sbCol)
{
    Console.WriteLine("Behavior: {0}", behavior.ToString());
}
C#
Uri baseAddress = new Uri("http://localhost:8001/Simple");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

serviceHost.AddServiceEndpoint(
    typeof(ICalculator),
    new WSHttpBinding(),
    "CalculatorServiceObject");

// Enable Mex
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);

ServiceDescription sd = serviceHost.Description;
sd.Behaviors.Add(new MyCustomBehavior());

serviceHost.Open();

Remarks

Use this method when adding custom behaviors to extend ServiceHost. Programmatically, you must Add(T) the custom service behavior to the Behaviors prior to when you call the Open method on the ServiceHost object.

The type of behavior that is accessible from the description hierarchy is scoped to the specific level. From the ServiceDescription the IServiceBehavior is accessible.

If you want access to the IEndpointBehavior associated with an endpoint instead, you can obtain the endpoints for the service using the Endpoints property. Then retrieve the ServiceEndpoint from the collection with the Find method that employs the relevant search criteria, and call the Behaviors property to obtain the collection of the IEndpointBehavior objects.

Applies to

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