简体   繁体   English

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

[英]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. 对于使用远程WCF服务的工具包,我在ChannelFactory<IMyService>配置了ChannelFactory<IMyService>

Now I want to configure this channel's endpoint behavior through code (using Unity) to apply this behavior: 现在我想通过代码(使用Unity)配置此通道的端点行为以应用此行为:

<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 ) 我在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. 但现在我被困在Unity配置中试图这样做。 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. 在客户端,我们有一个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 然后我们使用InjectionFactory配置unity

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. 通过创建用于封装WCF通道的新类,可以轻松解决此问题。 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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM