ServiceDescription.Behaviors 属性

定义

获取与该服务关联的行为。

public:
 property System::Collections::Generic::KeyedByTypeCollection<System::ServiceModel::Description::IServiceBehavior ^> ^ Behaviors { System::Collections::Generic::KeyedByTypeCollection<System::ServiceModel::Description::IServiceBehavior ^> ^ get(); };
public System.Collections.Generic.KeyedByTypeCollection<System.ServiceModel.Description.IServiceBehavior> Behaviors { get; }
member this.Behaviors : System.Collections.Generic.KeyedByTypeCollection<System.ServiceModel.Description.IServiceBehavior>
Public ReadOnly Property Behaviors As KeyedByTypeCollection(Of IServiceBehavior)

属性值

KeyedByTypeCollection<IServiceBehavior>

包含服务关联行为的类型为 KeyedByTypeCollection<TItem>IServiceBehavior

示例

// 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();
' 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
Dim baseAddress As New Uri("http://localhost:8001/Simple")
Dim serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress)

serviceHost.AddServiceEndpoint(GetType(ICalculator), New WSHttpBinding(), "CalculatorServiceObject")

' Enable Mex
Dim smb As New ServiceMetadataBehavior()
smb.HttpGetEnabled = True
serviceHost.Description.Behaviors.Add(smb)

Dim sd As ServiceDescription = serviceHost.Description
sd.Behaviors.Add(New MyCustomBehavior())

serviceHost.Open()

注解

为了扩展 ServiceHost 而添加自定义行为时,可使用此方法。 您必须先以编程方式,将自定义服务行为 Add(T)Behaviors,然后对 Open 对象调用 ServiceHost 方法。

可从说明层次结构访问的行为类型限定在特定级别。 从 ServiceDescription 可以访问 IServiceBehavior

如果要访问与终结点关联的 IEndpointBehavior,则您可使用 Endpoints 属性来获取服务的终结点。 然后使用使用相关搜索条件的方法从集合中检索 ServiceEndpoint 该集合 Find ,并调用该 Behaviors 属性以获取对象的集合 IEndpointBehavior

适用于