ServiceDescription.Behaviors Property
Gets the behaviors associated with the service.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
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); 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();
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.