共用方式為


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 方法之前,將自訂服務行為 OpenServiceHost

可從描述階層存取的行為的型別會限定在特定層級的範圍內。 ServiceDescription 可從 IServiceBehavior 存取。

如果您希望改為存取與端點有關聯的 IEndpointBehavior,可以使用 Endpoints 屬性取得服務的端點。 ServiceEndpoint然後使用採用相關搜尋準則的方法從集合 Find 擷取 ,並呼叫 Behaviors 屬性以取得 物件的集合 IEndpointBehavior

適用於