开发者

Configure a WCF client's MaxItemsInObjectGraph when using Unity

For a toolkit that uses a remote WCF service, I have configured a ChannelFactory<IMyService> in a UnityContainer.

Now I want to configure this channel's endpoint behavior through code (using Unity) to apply this behavior:

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

I found this example on 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();

but now I am stuck trying to do this in a Unity configuration. Should I look into Interception?


We are using a build policy extension in unity to add behaviors on the service host. On the client we have a 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
    }
}

Then we configure unity with an InjectionFactory

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

By doing it this way you can also resolve your behavior through unity if you have some dependencies.


I think you should add one more level of indirection so you don't need to mess with interception or something like that. This problem can be easily solved by creating a new class for wraping up the WCF channel. For instance,

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
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜