.NET 5下EndpointDispatcher为空无法获取ClientRuntime的测试替代方案咨询
.NET 5+ 下EndpointDispatcher相关测试替代方案
背景
.NET 5+ 中的 System.ServiceModel 包为客户端专属移植版本,裁剪了全部WCF服务端侧的API实现。EndpointDispatcher 原本是服务端的调度核心类,在客户端包中仅保留了空壳定义,仅存在无参构造函数,没有DispatchRuntime、CallbackClientRuntime等成员,原有依赖该类获取ClientRuntime的测试逻辑无法直接运行。
替代测试实现
验证IEndpointBehavior.ApplyClientBehavior方法的逻辑正确性,本质仅需构造合法的ClientRuntime实例即可,完全不需要依赖服务端专属的EndpointDispatcher类。修改后的测试代码如下:
var myEndpointBehavior = new MyEndpointBehavior(); var serviceEndpoint = new ServiceEndpoint(new ContractDescription("localhost")); // 直接构造ClientRuntime实例,替换原有EndpointDispatcher相关逻辑 var clientRuntime = new ClientRuntime(serviceEndpoint.Contract.Name, serviceEndpoint.Contract.Namespace); clientRuntime.ClientMessageInspectors.Should().HaveCount(0); myEndpointBehavior.ApplyClientBehavior(serviceEndpoint, clientRuntime); clientRuntime.ClientMessageInspectors.Should().HaveCount(1); clientRuntime.ClientMessageInspectors.First().Should().BeOfType<MyEndpointBehaviorMessageInspector>();
双工场景适配
如果测试场景涉及双工契约的回调客户端运行时,可直接构造DuplexClientRuntime实例,原有断言逻辑不需要做任何调整,即可覆盖行为注入的验证需求。
内容的提问来源于stack exchange,提问作者tapizquent
相关产品推荐
相关产品推荐

