2009-12-07 11 views
7

Per un toolkit che utilizza un servizio WCF remoto, ho configurato un ChannelFactory<IMyService> in UnityContainer.Configurare un MaxItemsInObjectGraph del client WCF quando si utilizza Unity

Ora voglio configurare il comportamento di endpoint di questo canale tramite il codice (con Unity) per applicare questo comportamento:

<behaviors> 
    <endpointBehaviors> 
     <behavior name="BigGraph"> 
      <dataContractSerializer maxItemsInObjectGraph="1000000" /> 
     </behavior> 
     </endpointBehaviors> 
</behaviors> 

Ho trovato questo esempio su MSDN (http://msdn.microsoft.com/en-us/library/ms732038.aspx)

ChannelFactory<IDataService> factory = new ChannelFactory<IDataService>(binding, address); 
foreach (OperationDescription op in factory.Endpoint.Contract.Operations) 
{ 
    vardataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; 
    if (dataContractBehavior != null) 
    { 
     dataContractBehavior.MaxItemsInObjectGraph = 100000; 
    } 
} 
IDataService client = factory.CreateChannel(); 

ma ora Sono bloccato cercando di farlo in una configurazione di Unity. Devo esaminare Interception?

+0

Per ora ho appena creato la fabbrica, applicare il comportamento e aggiungerlo come istanza al contenitore. – veertien

risposta

1

Stiamo utilizzando un'estensione della politica di generazione in unità per aggiungere comportamenti sull'host del servizio. Sul client abbiamo un ServiceFactory.

/// <summary> 
/// Factory for creating application service proxies used on the workstation 
/// </summary> 
/// <typeparam name="TInterface">Interface for the service contract</typeparam> 
public class ServiceFactory<TInterface> where TInterface : class 
{ 
    private readonly List<IEndpointBehavior> m_Behaviors = new List<IEndpointBehavior>(); 

    /// <summary> 
    /// Add a behavior that is added to the proxy endpoint when the channel is created. 
    /// </summary> 
    /// <param name="behavior">An <see cref="IEndpointBehavior"/> that should be added</param>. 
    public void AddBehavior(IEndpointBehavior behavior) 
    { 
     m_Behaviors.Add(behavior); 
    } 

    /// <summary> 
    /// Creates a channel of type <see cref="CommunicationObjectInterceptor{TInterface}"/> given the endpoint address which 
    /// will recreate its "inner channel" if it becomes in a faulted state. 
    /// </summary> 
    /// <param name="url">The endpoint address for the given channel to connect to</param>. 
    public TInterface CreateChannel(string url) 
    { 
     // create the channel using channelfactory adding the behaviors in m_Behaviors 
    } 
} 

Poi abbiamo configurare l'unità con un InjectionFactory

new InjectionFactory(c => 
      { 
       var factory = new ServiceFactory<TInterface>(); 
       factory.AddBehavior(c.Resolve<IClientTokenBehavior>()); 
       return factory.CreateChannel(url); 
      }); 

Facendo in questo modo si può anche risolvere il comportamento attraverso l'unità se si dispone di alcune dipendenze.

0

Penso che dovresti aggiungere un altro livello di riferimento indiretto in modo da non dover fare casino con l'intercettazione o qualcosa del genere. Questo problema può essere facilmente risolto creando una nuova classe per il wraping del canale WCF. Ad esempio,

public class MyServiceClient : IMyService 
{ 
    public MyServiceClient(IChannelFactory<IMyService> channel) 
    { 
    } 

    public void DoSomething() //DoSomething is the implementation of IMyService 
    { 
    //Initialize the behavior in the channel 
    //Calls channel.DoSomething 
    } 
} 
Problemi correlati