的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