ChannelFactory error with dynamic arguments

This question is related to Error in a dynamic language environment in conjunction with IIS 7.5

ChannelFactory freezes if I provide it with a properly typed dynamic object.

 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)); 
  • The service above is public, not mine, and you can verify this code yourself by simply adding a link to the service to the endpoint provided in the code;
  • StockQuoteServiceClient was created using the menu item "Add service link" and performs dynamic objects very well;
  • This magic does not happen when I run the application from F5 to Debug, all lines are printed and the program crashes;
  • If I run it and then attach the debugger at runtime, I see that it hangs when calling channel.GetStockQuote(src) ;
  • If I leave this, the program will use all my memory;
  • It freezes when I use my own ChannelFactory with dynamic objects, as described in the comments.

Why ChannelFactory my ChannelFactory freeze when it accepts dynamic objects as parameters, when created using Add Service Reference works fine?

+8
wcf channelfactory wcf-client
source share
1 answer

When you use a dynamic keyword, each code associated with a dynamic variable will be compiled at runtime DLR. When you call a method using a dynamic variable, the actual signature of the method is unknown at compile time, as well as the type of method returned and everything connected with it, creating something that Eric Lippert called "Dynamic infection" :

"As I pointed out last time, when the call argument is dynamic, the coefficients are pretty good that the compiler will classify the result of the call is also dynamic; smoothing. In fact, when you use almost any operator in a dynamic expression, the result is dynamic type, with some exceptions. ( "is", for example, always returns bool.) You can "cure" an expression to prevent its dynamism from spreading by dropping it onto an object or any other non-dynamic type that you like; ix dynamic object in an object is the identity transformation ".

WCF internal components use many interfaces and abstractions and the well-known DLR restriction on abstractions and interfaces, where DLR does not allow the correct type. (Also see this discussion )

I was able to properly invoke ChannelFactory by reflecting and discarding the parameter to other types (and also trying to call the service using the wrong type). The problem should be related to DLR.

I cannot debug DLR compilation, but the problem may be related to the "dynamic infection" and the interface resolution error. When β€œinfected”, each part of the WCF call can be compiled at run time, and a type resolution error can create some contours of contours in some cases with corners, for example, implementing an overridden method that calls the base method, and the base class was incorrectly allowed to do the same same child class.

Some WCF internal elements follow additional instructions when connecting a debugger ( Debugger.IsAttached ), which usually includes statements, checks, and attributes. Additional instructions may provide some information that kills "dynamic infection" and avoids a fictitious infinite loop.

+3
source share

All Articles