簡體   English   中英

使用Unity時配置WCF客戶端的MaxItemsInObjectGraph

[英]Configure a WCF client's MaxItemsInObjectGraph when using Unity

對於使用遠程WCF服務的工具包,我在ChannelFactory<IMyService>配置了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配置中試圖這樣做。 我應該調查攔截嗎?

我們使用統一的構建策略擴展來在服務主機上添加行為。 在客戶端,我們有一個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);
            });

通過這種方式,如果你有一些依賴,你也可以通過統一解決你的行為。

我認為你應該添加一個更多級別的間接,這樣你就不需要亂用攔截或類似的東西了。 通過創建用於封裝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
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM