2009-12-07 18 views
7

Para un kit de herramientas que usa un servicio WCF remoto, he configurado un ChannelFactory<IMyService> en UnityContainer.Configure un MaxItemsInObjectGraph de un cliente WCF al usar Unity

Ahora quiero configurar el comportamiento de punto final de este canal a través de código (Unity) para aplicar este comportamiento:

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

me encontré con este ejemplo en 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(); 

pero ahora estoy atascado tratando de hacer esto en una configuración de Unity. ¿Debería investigar Intercepción?

+0

Por ahora acabo de construir la fábrica, aplicar el comportamiento y agregarlo como una instancia al contenedor. – veertien

Respuesta

1

Estamos utilizando una extensión de política de compilación en unity para agregar comportamientos en el host del servicio. En el cliente tenemos 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 
    } 
} 

Luego configure la unidad con un InjectionFactory

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

Al hacerlo de esta manera también se puede resolver su comportamiento a través de la unidad si tiene algunas dependencias.

0

Creo que deberías agregar un nivel más de indirección para que no tengas que jugar con la intercepción o algo así. Este problema se puede resolver fácilmente creando una nueva clase para envolver el canal WCF. Por ejemplo,

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 
    } 
} 
Cuestiones relacionadas