gpt4 book ai didi

c# - 带有 WCF 服务的 Microsoft 垫片

转载 作者:行者123 更新时间:2023-11-30 12:10:39 25 4
gpt4 key购买 nike

我正在尝试关注 this example并利用 shim 删除对 WCF 服务调用的外部依赖,该服务调用是从我正在执行单元测试的方法中调用的。与示例不同,我使用类似于以下的代码即时生成我的 WCF 客户端:

ChannelFactory<IReportBroker> factory = new ChannelFactory<IReportBroker>("ReportBrokerBasicHttpStreamed", new EndpointAddress(this.CurrentSecurityZoneConfigurationManager.ConfigurationSettings[Constants.ConfigurationKeys.ReportBrokerServiceUrl]));
IReportBroker proxy = factory.CreateChannel();
proxy.Execute(requestMessage))

我如何调整该示例以填充由 CreateChannel 方法返回的代理?我假设在 ShimWCFService 类中,我需要添加类似....

ShimChannelFactory<TService>.AllInstances.CreateChannel = (var1) => { return [instance of a mock object]};

但是,我不确定如何关联 <TService> 的模拟对象以该垫片作为返回值。

最佳答案

您需要为每个类型参数填充工厂。假设您拥有三个服务契约(Contract)“IService0”、“IService1”和“IService2”。

然后你需要像这样设置垫片:

ShimChannelFactory<IService0>.AllInstances.CreateChannel = (_) => { return new Service0Mock(); }
ShimChannelFactory<IService1>.AllInstances.CreateChannel = (_) => { return new Service1Mock(); }
ShimChannelFactory<IService2>.AllInstances.CreateChannel = (_) => { return new Service2Mock(); }

关于c# - 带有 WCF 服务的 Microsoft 垫片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17560014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com