使用Unity时配置WCF客户端的MaxItemsInObjectGraph

vee*_*ien 7 .net c# wcf unity-container

对于使用远程WCF服务的工具包,我ChannelFactory<IMyService>在UnityContainer中配置了一个.

现在我想通过代码(使用Unity)配置此通道的端点行为以应用此行为:

<behaviors>
    <endpointBehaviors>
        <behavior name="BigGraph">
            <dataContractSerializer maxItemsInObjectGraph="1000000" />
        </behavior>
        </endpointBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

我在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();
Run Code Online (Sandbox Code Playgroud)

但现在我被困在Unity配置中试图这样做.我应该调查拦截吗?

Mic*_*ael 1

我们正在统一使用构建策略扩展来在服务主机上添加行为。在客户端我们有一个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
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我们配置 unityInjectionFactory

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

通过这样做,如果您有一些依赖性,您还可以通过统一解决您的行为。