<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Potential pitfalls to avoid when passing around async lambdas</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx</link><description>One of the really useful capabilities of the new async methods feature in C# and Visual Basic is the ability to write async lambdas and anonymous methods (from here on in this post, I’ll refer to both of these as async lambdas, since the discussion applies</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Potential pitfalls to avoid when passing around async lambdas</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx#10267457</link><pubDate>Mon, 13 Feb 2012 21:58:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10267457</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Hi Vincent-Philippe-&lt;/p&gt;
&lt;p&gt;re: &amp;quot;What we really need is to have the operation sync on the server side (hence being able to return something) but async on the client side&amp;quot;&lt;/p&gt;
&lt;p&gt;Why does the server-side need to be synchronous? &amp;nbsp;The client-side and the server-side should be entirely independent when it comes to asynchrony. &amp;nbsp;You could have sync or async on the client, and sync or async on the server, and they don&amp;#39;t need to match.&lt;/p&gt;
&lt;p&gt;re: &amp;quot;we are using &amp;#39;ChannelFactory&amp;#39;, hence we&amp;#39;re not using Add Web Service reference&amp;quot;&lt;/p&gt;
&lt;p&gt;Sorry, I misunderstood what you were trying to do. Have you tried the .NET 4.5 Developer Preview? &amp;nbsp;I just tried this out, and using ChannelFactory does work there. &amp;nbsp;My server-side interface looked like:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[ServiceContract]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public interface IService1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[OperationContract]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Task&amp;lt;string&amp;gt; GetDataAsync(int value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;giving me an asynchronous design on the server (but I could have just have easily made that synchronous, returning string instead of Task&amp;lt;string&amp;gt;). &amp;nbsp;Then my client-side interfaces looked like:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public interface IService1Channel : IService1, IClientChannel&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[ServiceContractAttribute(ConfigurationName = &amp;quot;IService1&amp;quot;)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public interface IService1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[OperationContractAttribute(Action = &amp;quot;&lt;a rel="nofollow" target="_new" href="http://tempuri.org/IService1/GetData&amp;quot;"&gt;tempuri.org/.../GetData&amp;quot;&lt;/a&gt;, ReplyAction = &amp;quot;&lt;a rel="nofollow" target="_new" href="http://tempuri.org/IService1/GetDataResponse&amp;quot;"&gt;tempuri.org/.../GetDataResponse&amp;quot;&lt;/a&gt;)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string GetData(int value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[OperationContractAttribute(Action = &amp;quot;&lt;a rel="nofollow" target="_new" href="http://tempuri.org/IService1/GetData&amp;quot;"&gt;tempuri.org/.../GetData&amp;quot;&lt;/a&gt;, ReplyAction = &amp;quot;&lt;a rel="nofollow" target="_new" href="http://tempuri.org/IService1/GetDataResponse&amp;quot;"&gt;tempuri.org/.../GetDataResponse&amp;quot;&lt;/a&gt;)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Task&amp;lt;string&amp;gt; GetDataAsync(int value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;and then after implementing my service:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class Service1 : IService1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public async Task&amp;lt;string&amp;gt; GetDataAsync(int value)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;await Task.Delay(1000);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return string.Format(&amp;quot;After a second, the value was {0}.&amp;quot;, value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;and creating the appropriate configuration in my app.config, I was able to successfully run code that used ChannelFactory, e.g. both:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var factory = new ChannelFactory&amp;lt;IService1Channel&amp;gt;(&amp;quot;BasicHttpBinding_IService1&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var channelClient = factory.CreateChannel();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Console.WriteLine(channelClient.GetData(42));&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var factory = new ChannelFactory&amp;lt;IService1Channel&amp;gt;(&amp;quot;BasicHttpBinding_IService1&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var channelClient = factory.CreateChannel();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Console.WriteLine(channelClient.GetDataAsync(42).Result);&lt;/p&gt;
&lt;p&gt;I hope that helps.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10267457" width="1" height="1"&gt;</description></item><item><title>re: Potential pitfalls to avoid when passing around async lambdas</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx#10266886</link><pubDate>Sat, 11 Feb 2012 19:44:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10266886</guid><dc:creator>Vincent-Philippe Lauzon</dc:creator><description>&lt;p&gt;Hi Stephen,&lt;/p&gt;
&lt;p&gt;Thanks for your answer.&lt;/p&gt;
&lt;p&gt;As I mentionned, we are using &amp;#39;ChannelFactory&amp;#39;, hence we&amp;#39;re not using Add Web Service reference. &amp;nbsp;We are exporting service / data contracts in a shared assembly and simply use ChannelFactory to create a proxy out of the service contract interface. &amp;nbsp;Hence the interface looks sync (no task in sight) and the eventual call to an &amp;#39;asyncable&amp;#39; network connection is buried in the implementation of the proxy generated by ChannelFactory (.NET Fx). &amp;nbsp;It&amp;#39;s a very handy API because you can stay in sync with your contracts without refreshing them all the time (especially useful if you have many services).&lt;/p&gt;
&lt;p&gt;That scenario was supposed to be supported in .NET 4.5 (according to social.msdn.microsoft.com/.../8ec1b6ef-dbd8-46e6-b0b9-db7bfe80db06). &amp;nbsp;I do not know if there&amp;#39;s a twist to do somewhere for it to work?&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve seen service contracts with Task in the Async Fx. &amp;nbsp;Not too sure how that translates into the WSDL? &amp;nbsp;Is it another way to declare an operation async? &amp;nbsp;What we really need is to have the operation sync on the server side (hence being able to return something) but async on the client side, without relying on VS Service Reference code generation.&lt;/p&gt;
&lt;p&gt;Could you point me to something doing that?&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10266886" width="1" height="1"&gt;</description></item><item><title>re: Potential pitfalls to avoid when passing around async lambdas</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx#10266654</link><pubDate>Fri, 10 Feb 2012 18:16:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10266654</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Hi Vincent-Philippe-&lt;/p&gt;
&lt;p&gt;re: can you show how to do that in .NET 4.5&lt;/p&gt;
&lt;p&gt;It happens by default. &amp;nbsp;In Visual Studio 11 Developer Preview, when you use Add Service Reference to create a client proxy, it automatically generates Task-based methods.&lt;/p&gt;
&lt;p&gt;re: can you show how to do that in .NET 4&lt;/p&gt;
&lt;p&gt;In the Async CTP, there is a sample implementation of an extension to the Add Service Reference that will generate Task-based endpoints. &amp;nbsp;But you could also just have it generate Begin/EndXx methods (you need to configure the options in Add Service Reference to spit out async endpoints) which you then wrap with Task.Factory.FromAsync or similar.&lt;/p&gt;
&lt;p&gt;I hope that helps.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10266654" width="1" height="1"&gt;</description></item><item><title>re: Potential pitfalls to avoid when passing around async lambdas</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx#10266638</link><pubDate>Fri, 10 Feb 2012 17:43:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10266638</guid><dc:creator>Vincent-Philippe Lauzon</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;It&amp;#39;s not related to this blog entry, but I&amp;#39;ve got a question for you.&lt;/p&gt;
&lt;p&gt;Some time ago I&amp;#39;ve got an answer I was looking for: &amp;nbsp;i.e. that async / await will be supported for web service calls done through a proxy generated by ChannelFactory (see &lt;a rel="nofollow" target="_new" href="http://social.msdn.microsoft.com/Forums/en-US/async/thread/8ec1b6ef-dbd8-46e6-b0b9-db7bfe80db06"&gt;social.msdn.microsoft.com/.../8ec1b6ef-dbd8-46e6-b0b9-db7bfe80db06&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;My question is: &amp;nbsp;can you show how to do that in .NET 4.5? &amp;nbsp;And... &amp;nbsp;could we do something similar in .NET 4.0?&lt;/p&gt;
&lt;p&gt;I&amp;#39;m on a project right now that would really beneficiate from using async WS calls with IO Ports (i.e. real async, not a thread spinning while waiting for the WS to come back) but we are on .NET 4.0!&lt;/p&gt;
&lt;p&gt;Cheers and keep up the good work!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10266638" width="1" height="1"&gt;</description></item><item><title>re: Potential pitfalls to avoid when passing around async lambdas</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx#10265567</link><pubDate>Wed, 08 Feb 2012 19:26:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10265567</guid><dc:creator>.. Tridex ... </dc:creator><description>&lt;p&gt;Excellent, this it what makes me want to develop more and more and thanks for the last articles ...&lt;/p&gt;
&lt;p&gt;Keep us informed!!!&lt;/p&gt;
&lt;p&gt;Cheers .. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10265567" width="1" height="1"&gt;</description></item></channel></rss>