ChannelFactory
如果我为它提供正确类型的动态对象,则会挂起。
dynamic src = "MSFT";
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://www.restfulwebservices.net/wcf/StockQuoteService.svc");
var channel = new ChannelFactory<IStockQuoteService>(binding, endpoint).CreateChannel();
// this will print just fine
Console.WriteLine(channel.GetStockQuote(src as string));
// this will print just fine
Console.WriteLine(new StockQuoteServiceClient().GetStockQuote(src));
// this will never print and the application will hang with no exceptions
Console.WriteLine(channel.GetStockQuote(src));
- 上面的服务是公开的,不是我的,你可以自己测试这段代码,只要在代码中提供的endpoint添加服务引用即可;
StockQuoteServiceClient
由 Add Service Reference 菜单项创建,并采用动态对象就好了;- 当我在调试时使用 F5 启动应用程序时,这神奇地不会发生,所有行都打印并且程序正确退出;
- 如果我运行它,然后在执行期间附加调试器,我可以看到它挂在调用
channel.GetStockQuote(src)
; - 如果我不管它,程序会吃掉我所有的记忆;
- 仅当我将自己
ChannelFactory
的对象与动态对象一起使用时,它才会挂起,如评论中所述。
ChannelFactory
当添加服务引用创建的对象运行良好时,为什么当它以动态对象作为参数时我的挂起?