Behaviors Property
Collapse the table of content
Expand the table of content

ServiceDescription.Behaviors Property

Gets the behaviors associated with the service.

Namespace:  System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public KeyedByTypeCollection<IServiceBehavior> Behaviors { get; }

Property Value

Type: System.Collections.Generic.KeyedByTypeCollection<IServiceBehavior>
The KeyedByTypeCollection<TItem> of type IServiceBehavior that contains the behaviors associated with the service.

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.

			'// Iterate through the list of behaviors in the ServiceDescription
			Dim svcDesc As ServiceDescription = serviceHost.Description
			Dim sbCol As KeyedByTypeCollection(Of IServiceBehavior) = svcDesc.Behaviors
			For Each behavior As IServiceBehavior In sbCol
				Console.WriteLine("Behavior: {0}", CType(behavior, Object).ToString())
			Next behavior

//// 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());

Uri baseAddress = new Uri("http://localhost:8001/Simple");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

    new WSHttpBinding(),

// Enable Mex
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;

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


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2016 Microsoft