【问题标题】:EndpointDispatcher and ClientRuntime missing缺少 EndpointDispatcher 和 ClientRuntime
【发布时间】:2021-11-01 05:35:02
【问题描述】:

除了从 EndpointDispatcher 访问 ClientRuntime 之外,还有其他方法吗?

似乎在 .NET 5.0 中,使用 System.ServiceModel 4.8.1,EndpointDispatcher 类是完全空的,只包含一个空构造函数。

我们曾经进行过一些测试来检查 IEndpointBehavior 是否已使用 WCF 正确添加到客户端。

var myEndpointBehavior = new MyEndpointBehavior();
var serviceEndpoint = new ServiceEndpoint(new ContractDescription("localhost"));
var dispatcher = new EndpointDispatcher(new EndpointAddress("http://localhost"), "", ""); // <--- Error because EndpointDispatcher class is totally empty
var clientRuntime = dispatcher.DispatchRuntime.CallbackClientRuntime; // <---- does not exist

clientRuntime.ClientMessageInspectors.Should().HaveCount(0);
myEndpointBehavior.ApplyClientBehavior(serviceEndpoint, clientRuntime);
clientRuntime.ClientMessageInspectors.Should().HaveCount(1);
clientRuntime.ClientMessageInspectors.First().Should().BeOfType<MyEndpointBehaviorMessageInspector>();

有没有办法在 .NET 5.0 中测试同样的行为?

【问题讨论】:

标签: c# wcf .net-core .net-5


【解决方案1】:

由于ClientRuntime 的构造函数被隐藏起来,我无法真正访问它,因此我最终使用Activator 来访问ClientRuntime 的私​​有/内部构造函数。这不是一个优雅的解决方案,但它允许我们测试行为。

var myEndpointBehavior = new MyEndpointBehavior();
var serviceEndpoint = new ServiceEndpoint(new ContractDescription("localhost"));
var clientRuntime = (ClientRuntime) Activator.CreateInstance(typeof(ClientRuntime), BindingFlags.Instance | BindingFlags.NonPublic,
                null, new[] { serviceEndpoint.Contract.Name, serviceEndpoint.Contract.Namespace }, null, null);

clientRuntime.ClientMessageInspectors.Should().HaveCount(0);
myEndpointBehavior.ApplyClientBehavior(serviceEndpoint, clientRuntime);
clientRuntime.ClientMessageInspectors.Should().HaveCount(1);
clientRuntime.ClientMessageInspectors.First().Should().BeOfType<MyEndpointBehaviorMessageInspector>();

【讨论】:

    猜你喜欢
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2013-03-09
    • 1970-01-01
    • 2011-12-10
    • 2012-04-27
    • 2018-01-15
    相关资源
    最近更新 更多