gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:40:47 26 4
gpt4 key购买 nike

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

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

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

通过这种方式,如果你有一些依赖,你也可以通过统一来解决你的行为。

关于c# - 使用 Unity 时配置 WCF 客户端的 MaxItemsInObjectGraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1860099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com