c# - 缺少 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 中测试同样的行为?
解决方案
由于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>();
推荐阅读
- android - 用于牛轧糖的 Android 内核 - 为 android auto 启用 Android Accessory 工作
- javascript - 如何在移动设备上以不同的顺序制作这两个“col”堆栈?引导程序 4
- swift - 创建可重用视图时如何分配 TextField Delegate 和方法
- python - 将百分位数计算为 Pandas 中的一列
- c++ - C++ 将一个字符串的所有元音复制到另一个字符串中 | 如何在字符串中复制char类型?
- view - 在 SQLite3 中创建一个视图,列出所有通过树状数据库结构的唯一路径
- c - 在 C 编程中,for(int i=0; st[i]; i++) 和 for(int i=0; i 有什么区别?
在下面的程序中,在 for 循环条件上使用 st[i] 时:
#include<stdio.h> int main() { int st[] ={1,2,3,4,5,6,7,8}; int i;
- asp.net-core - 信号器核心和基于策略的授权:资源始终为空
- bash - 如何在类似 ANSI C 的字符串中扩展 env var 并使用文字换行符?
- python - 使用 xml 在 python 中添加完整的 xml 作为 xml 节点的子节点