IEndpointBehavior.ApplyDispatchBehavior Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Implémente une modification ou une extension du service sur un point de terminaison.
public:
void ApplyDispatchBehavior(System::ServiceModel::Description::ServiceEndpoint ^ endpoint, System::ServiceModel::Dispatcher::EndpointDispatcher ^ endpointDispatcher);
public void ApplyDispatchBehavior(System.ServiceModel.Description.ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher);
abstract member ApplyDispatchBehavior : System.ServiceModel.Description.ServiceEndpoint * System.ServiceModel.Dispatcher.EndpointDispatcher -> unit
Public Sub ApplyDispatchBehavior (endpoint As ServiceEndpoint, endpointDispatcher As EndpointDispatcher)
Paramètres
- endpoint
- ServiceEndpoint
Point de terminaison qui expose le contrat.
- endpointDispatcher
- EndpointDispatcher
Répartiteur de point de terminaison à modifier ou à étendre.
Exemples
L’exemple de code suivant montre l’implémentation d’un comportement de point de terminaison qui ajoute un System.ServiceModel.Dispatcher.IDispatchMessageInspector objet dans une application de service. Dans ce cas, la EndpointBehaviorMessageInspector classe implémente System.ServiceModel.Dispatcher.IDispatchMessageInspector pour inspecter le message entrant et sortant, l’interface IEndpointBehavior permettant d’insérer la classe d’inspecteur dans le système d’inspection pour tous les points de terminaison auxquels le comportement s’applique, ainsi que pour System.ServiceModel.Configuration.BehaviorExtensionElement activer le comportement de l’inspecteur de message à l’aide d’un fichier de configuration d’application.
La première étape consiste à implémenter l’inspecteur de message.
// IDispatchMessageInspector Members
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine("AfterReceiveRequest called.");
return null;
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
Console.WriteLine("BeforeSendReply called.");
}
L’exemple de code suivant montre l’utilisation de la ApplyDispatchBehavior méthode pour ajouter l’inspecteur de message à la DispatchRuntime.MessageInspectors propriété.
// IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
{
behavior.MessageInspectors.Add(new EndpointBehaviorMessageInspector());
}
public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new EndpointBehaviorMessageInspector());
}
public void Validate(ServiceEndpoint serviceEndpoint)
{
return;
}
L’exemple de code suivant montre l’implémentation pour System.ServiceModel.Configuration.BehaviorExtensionElement permettre l’utilisation du comportement de l’inspecteur de message à partir d’un fichier de configuration.
// BehaviorExtensionElement members
public override Type BehaviorType
{
get { return typeof(EndpointBehaviorMessageInspector); }
}
protected override object CreateBehavior()
{
return new EndpointBehaviorMessageInspector();
}
Enfin, le fichier de configuration suivant montre comment l’exemple précédent peut être utilisé à partir de la configuration.
<configuration>
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.SampleService"
behaviorConfiguration="metadataSupport"
>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/ServiceMetadata" />
</baseAddresses>
</host>
<endpoint
address="/SampleService"
binding="wsHttpBinding"
behaviorConfiguration="withMessageInspector"
contract="Microsoft.WCF.Documentation.ISampleService"
/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataSupport">
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="withMessageInspector">
<endpointMessageInspector />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="endpointMessageInspector"
type="Microsoft.WCF.Documentation.EndpointBehaviorMessageInspector, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>
Remarques
Implémentez la ApplyDispatchBehavior méthode pour afficher, modifier ou étendre le runtime de service sur tous les messages ou pour des opérations spécifiques dans un point de terminaison. Pour plus d’informations sur les personnalisations que vous pouvez effectuer dans une application de service, consultez System.ServiceModel.Dispatcher.DispatchRuntime et System.ServiceModel.Dispatcher.DispatchOperation.
Il est recommandé que la ApplyDispatchBehavior méthode lève une NotImplementedException exception si le comportement est destiné uniquement à être utilisé dans une application cliente.
Notez qu’il peut y avoir deux opérations portant le même nom dans la description lors de l’utilisation d’un contrat de rappel (une opération dans chaque direction). Si vous effectuez une itération via des opérations, vous devez mettre en corrélation la direction du message entre le point de terminaison System.ServiceModel.Dispatcher.DispatchRuntime et ce qui est retourné par la DispatchRuntime.CallbackClientRuntime propriété.
En outre, étant donné que d’autres comportements ont peut-être déjà ajouté ou supprimé certaines opérations du runtime, il n’existe aucune garantie qu’il existe le même nombre d’opérations dans la description qu’il y System.ServiceModel.Dispatcher.DispatchOperation a des objets dans la DispatchRuntime.Operations propriété.