的ChannelFactory错误这个问题是关系到Bug in the dynamic language runtime in combination with IIS 7.5动态参数
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));
- 上面的服务是公共的,不是我的,你可以自己测试此代码,如果你只需要添加的服务参考代码中提供的端点;
StockQuoteServiceClient
由添加服务引用菜单项创建并采用动态对象就好了;- 当我使用F5在Debug上启动应用程序时,这奇迹般地不会发生,所有行都会打印并且程序正常退出;
- 如果我运行它,然后在执行过程中附加调试器,我可以看到它挂在
channel.GetStockQuote(src)
的调用上; - 如果我离开它,程序会吃掉我所有的记忆;
- 只有当我使用我自己的
ChannelFactory
和动态对象时才会挂起,如注释中所述。
为什么我的ChannelFactory
以动态对象作为参数时挂起,当添加服务引用创建的运行正常时?
使用反射也可以。 var method = channel.GetType()。GetMethod(“GetStockQuote”); var value =(StockQuote)method.Invoke(channel,new object [] {src}); – lstern 2013-05-19 20:20:43