首页 > 解决方案 > 缺少 EndpointDispatcher 和 ClientRuntime

问题描述

是否有任何替代方法可以从 EndpointDispatcher 访问 ClientRuntime。

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

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

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

解决方案


由于ClientRuntime's 的构造函数被隐藏起来,我无法以任何方式真正访问它,我最终使用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>();

推荐阅读