这是一个处理.net框架内部的问题。在使用Java服务时,我必须更改端点行为,以添加自定义SOAP头( web服务需要一个nonce)和操作应答,以便WCF能够正确地反序列化响应。
在查看代码示例时,一个使用ServiceEndpoint.EndpointBehaviors,另一个使用ServiceEndpoint.Behaviors。经过一些测试,这两种属性似乎都有相同的影响,可以互换,这就引出了一个问题,这两者之间的区别是什么?
示例代码:
自定义端点行为:
public class ApplyClientEndpointBehaviour<T> : IEndpointBehavior where T : IClientMessageInspector
{
public ApplyClientEndpointBehaviour(T item)
{
_item = item;
}
private readonly T _item;
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(_item);
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
// Nothing special here
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// Nothing special here
}
public void Validate(ServiceEndpoint endpoint)
{
// Nothing special here
}
}然后
var client = new <wcf client child class of System.ServiceModel.ClientBase generated through Add Service Reference>
client.Endpoint.EndpointBehaviors.Add(new ApplyClientEndpointBehaviour<SomeIClientMessageInspector>(new SomeIClientMessageInspector()));
client.Endpoint.Behaviors.Add(new ApplyClientEndpointBehaviour<AnotherIClientMessageInspector>(new AnotherIClientMessageInspector()));将SomeIClientMessageInspector和AnotherIClientMessageInspector分配给Endpoint.Behaviors或Endpoint.EndpointBehaviors将不会导致实际功能的更改。两者都可以互换性地分配给一个或另一个,而服务端点的行为没有变化。这就引出了这样的问题:如果这两个属性之间有什么不同,或者是添加了一个属性,而另一个属性是为了向后的可比性而被添加的呢?
发布于 2016-07-12 16:48:10
查看反编译的源代码,EndpointBehaviors只返回Behaviors集合。
[__DynamicallyInvokable]
public KeyedCollection<System.Type, IEndpointBehavior> EndpointBehaviors
{
[__DynamicallyInvokable] get
{
return (KeyedCollection<System.Type, IEndpointBehavior>) this.Behaviors;
}
}EndpointBehaviors可以从.NET Framework4.5和便携平台中获得,而Behaviors从3.0起就可以使用了。我猜想这是桌面和便携平台的API合并的结果。
https://stackoverflow.com/questions/38333824
复制相似问题