2011-08-23 1 views
1

Je souhaite m'assurer que toutes nos opérations qui renvoient des jeux de données dans notre WCF ont la valeur .ExcludedSchema définie dans Property SchemaSerializationMode. Est-ce que je peux faire ceci avec un CustomComportement?Modifier la valeur de retour dans l'opération WCF

J'ai essayé d'implémenter un CustomDispatchBehavior et d'ajouter un MessageInspector, mais les méthodes AfterReceiveRequest et BeforeSendReply ne me permettent pas de faire quoi que ce soit avec la valeur de retour. Dans BeforeSendreply, la valeur de retour a déjà été sérialisée. Où puis-je brancher mon code?

public class CustomDispatchBehavior : BehaviorExtensionElement, IServiceBehavior 
    { 

     public override Type BehaviorType 
     { 
      get { return typeof(CustomDispatchBehavior); } 
     } 

     protected override object CreateBehavior() 
     { 
      return new CustomDispatchBehavior(); 
     } 

     void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) 
     { 
      //throw new NotImplementedException(); 
     } 

     void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
     { 
      //throw new NotImplementedException(); 
      foreach (ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers) 
      { 
       foreach (EndpointDispatcher ed in chanDisp.Endpoints) 
       { 
        ed.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector()); 

       } 
      } 
     } 

     void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
     { 
      //throw new NotImplementedException(); 
     } 

    } 

Répondre

1

Je l'ai résolu en utilisant la IParametorInspector

void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    {  

     foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers) 
     { 
      foreach (EndpointDispatcher epDisp in chDisp.Endpoints) 
      {     
       foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations) 
        op.ParameterInspectors.Add(new DataSetParameterInspector()); 
      } 
     } 

    } 

et l'inspecteur se présente comme suit

public class DataSetParameterInspector : IParameterInspector 
{ 

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) 
    { 
     Type t =returnValue.GetType(); 
     if (t.IsSubclassOf(typeof(GlobalUtils.RR.Response))) 
     { 
      foreach (var pi in t.GetProperties()) 
      { 
       if (pi.PropertyType.IsSubclassOf(typeof(System.Data.DataSet))) 
       { 
        object parameter = pi.GetValue(returnValue, null); 
        if (parameter != null) 
         ((System.Data.DataSet)parameter).SchemaSerializationMode = System.Data.SchemaSerializationMode.ExcludeSchema; 
       } 
      } 
     } 
    } 
1

Regardez l'interface IDispatchMessageFormatter. Il définit des méthodes qui désérialisent les messages de demande et sérialisent les messages de réponse dans une application de service.