【问题标题】:Configure a WCF client's MaxItemsInObjectGraph when using Unity使用 Unity 时配置 WCF 客户端的 MaxItemsInObjectGraph
【发布时间】:2012-06-23 02:07:06
【问题描述】:

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

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

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

我在 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();

但现在我被困在 Unity 配置中尝试执行此操作。我应该调查拦截吗?

【问题讨论】:

  • 现在我只是构建工厂,应用行为并将其作为实例添加到容器中。

标签: c# .net wcf unity-container


【解决方案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
    }
}

然后我们用InjectionFactory配置unity

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

通过这种方式,如果您有一些依赖关系,您还可以通过统一解决您的行为。

【讨论】:

    【解决方案2】:

    我认为你应该再增加一层间接性,这样你就不需要搞乱拦截或类似的事情了。通过创建一个用于包装 WCF 通道的新类可以轻松解决此问题。例如,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2012-02-07
      • 2011-06-24
      • 2010-10-24
      • 1970-01-01
      相关资源
      最近更新 更多