<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.microsoft.co.il/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>Zuker On Foundations</title><link>http://blogs.microsoft.co.il/blogs/zuker/</link><description>The realm of MS Technologies</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>WCF – Queryable over SOAP using OData URI’s</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2013/09/03/wcf-queryable-over-soap-using-odata-uri-s.aspx</link><pubDate>Tue, 03 Sep 2013 12:13:07 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:2542749</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=2542749</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2013/09/03/wcf-queryable-over-soap-using-odata-uri-s.aspx#comments</comments><description>&lt;p&gt;OData protocol offers a rich and powerful query API over the data exposed from the data service.   &lt;br /&gt;Usually, the query description is aggregated to the URI of the service operation as the query string, then the service can translate that query back on to the queryable model and return the specific query desired by the client.&lt;/p&gt;  &lt;p&gt;If you used it before, you must have seen the power it brings you in exposing the data to many clients and platforms to support each and every need.&lt;/p&gt;  &lt;p&gt;In addition to WCF Data Services, ASP.NET WebAPI has too adopted this powerful approach. You can simply apply a [Queryable] attribute on your WebAPI service operation and expose the queryable model with support to this rich OData query API.&lt;/p&gt;  &lt;p&gt;I understand the concept of OData which aims to serve and manage data over a standard and interoperable protocol, however I found several projects which revolves around building SOAP services with WS-* standards that wished to have that too.   &lt;br /&gt;For example, I could have a distributed .NET system with WS-* as well as binary encoding, but I simply want to support that powerful query API, not as a goal for interoperability, but as a goal of allowing the consumer to define the query that it wishes.&lt;/p&gt;  &lt;p&gt;Yes, I could implement a generic a way for the client to provide the different parameters (‘top’, ‘skip’, ‘orderby’, etc) and apply it in the service, but that’s exactly what the libraries around OData provide, so naturally I wanted to build over that.&lt;/p&gt;  &lt;p&gt;Basically, I needed a provider to build the standard URI from an expression on the client side and a way to translate it back on the queryable model on the other end.   &lt;br /&gt;Unfortunately, the implementation in the WCF Data Services remains private, and the ODataLib family seems to rely heavily on the http stack.    &lt;br /&gt;Googling a bit further, I found this nifty little project &lt;a href="https://linq2rest.codeplex.com/" target="_blank"&gt;Linq2Rest&lt;/a&gt; which provided me with what I needed.&lt;/p&gt;  &lt;p&gt;So.. how does it look like?&lt;/p&gt;  &lt;h2&gt;Service Side&lt;/h2&gt;  &lt;p&gt;Similar to ASP.NET WebAPI, your service needs to expose the queryable model and decorate it with a simple attribute as follows -&lt;/p&gt;  &lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:21ee78ff-eb0a-44b1-8327-23b014a3d209" class="wlWriterEditableSmartContent" style="float:none;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;display:inline;padding-right:0px;"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ServiceContract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;interface&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IFooService&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;OperationContract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IQueryable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; GetFoos();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;FooService&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IFooService&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;readonly&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IQueryable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; FoosContext =&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Range(0, 100).Select(i =&amp;gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(i)).ToArray().AsQueryable();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Queryable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(MaxResults = 2)]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IQueryable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; GetFoos()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; FoosContext;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;That’s it, you’re done! Now you support OData standard URI requests on your queryable model.&lt;/p&gt;  &lt;p&gt;As shown in the example, you can define the ‘MaxResults’ to ensure the maximum capacity of the result.&lt;/p&gt;  &lt;p&gt;One last thing, you can use ‘IEnumerable&amp;lt;&amp;gt;’ as your return type if that is what you prefer in your case.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Another small feature – applying the query within the operation:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In some cases the need of applying the query request within the service operation might surface because you might want to do something with the end result before exiting.&lt;/p&gt;  &lt;p&gt;If you encounter such a case, I prepared a method you can you use -&lt;/p&gt;  &lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:310fe2a7-8a36-4539-980e-09d6174720e4" class="wlWriterEditableSmartContent" style="float:none;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;display:inline;padding-right:0px;"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Queryable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(MaxResults = 2)]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IQueryable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; GetFoos()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//If you need to apply the query before returning the result &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//because you might want to do something with the end result, you can&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; appliedQuery = FoosContext.TryApplyQueryableGet();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//..do something with the applied query&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; appliedQuery;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h2&gt;&amp;#160;&lt;/h2&gt;  &lt;h2&gt;Client Side&lt;/h2&gt;  &lt;p&gt;Normally, when you wish to call the service, you would create a channel and call the ‘GetFoos’ method.   &lt;br /&gt;I built some infrastructure on the client side to enable you to create query against this operation.&lt;/p&gt;  &lt;h4&gt;Synchronous Invocation&lt;/h4&gt;  &lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8c1331a5-2dd5-4a49-9f30-00333d4e658d" class="wlWriterEditableSmartContent" style="float:none;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;display:inline;padding-right:0px;"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;FooServiceClient&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; CreateChannel()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;FooServiceClient&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SyncCall()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; channel = CreateChannel();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; syncGet = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;QueryableGet&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.CreateSync(channel, channel.GetFoos);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foos = syncGet.Query&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;.Where(f =&amp;gt; f.Id &amp;gt; 3 &amp;amp;&amp;amp; f.Id &amp;lt; 10)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;.Skip(1)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;.Take(3)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;.OrderByDescending(f =&amp;gt; f.Name);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;foreach&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foo &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foos)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(foo.Name);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;channel.Close();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;As you can see, you construct the channel as you normally would. Then, you can create a ‘QueryableGet’ from this channel and the operation you would like to use. Finally, it provides you with an IQueryable of that model which you can use to define your query and just enumerate over the results.&lt;/p&gt;  &lt;h4&gt;Asynchronous Invocation&lt;/h4&gt;  &lt;p&gt;The support for asynchronous invocation relies on the fact that you have asynchronous operations defined on the service contract.   &lt;br /&gt;Once you do, you create the QueryableGet to map against the asynchronous operation, and that gives you a method that you can use to define your query and receive the task-based asynchronous operation that returns the results.&lt;/p&gt;  &lt;p&gt;Both APM (Begin/End) and TPM (Task-based) are supported.&lt;/p&gt;  &lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:60fa1ac7-69d9-44d1-840e-b7c135dc752a" class="wlWriterEditableSmartContent" style="float:none;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;display:inline;padding-right:0px;"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;async&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; AsyncCall()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; channel = CreateChannel();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//We need to cast the result because the generated proxy uses &amp;#39;Foo[]&amp;#39; as the result&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; asyncGet = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;QueryableGet&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.CreateAsync(channel,&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;() =&amp;gt; channel.GetFoosAsync().CastResult&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[], &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;&amp;gt;());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//if we had begin/end, we could do the following -&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//var asyncGet = QueryableGet.CreateAsync(channel,&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//    () =&amp;gt; Task&amp;lt;IEnumerable&amp;lt;Foo&amp;gt;&amp;gt;.Factory.FromAsync(channel.BeginGetFoos, channel.EndGetFoos, null));&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foos = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; asyncGet.ExecuteAsync(q =&amp;gt; q&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;.Where(f =&amp;gt; f.Id &amp;gt; 3 &amp;amp;&amp;amp; f.Id &amp;lt; 10)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;.Skip(1)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;.Take(3)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;.OrderByDescending(f =&amp;gt; f.Name));&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;foreach&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foo &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foos)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(foo.Name);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;channel.Close();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Implementation Bits&lt;/h2&gt;  &lt;p&gt;If you like to read a short description of the underlying implementation then keep reading, otherwise skip this part.&lt;/p&gt;  &lt;p&gt;Basically, when you define the ‘QueryableGet’, it provides you with an IQueryable model that is built using Linq2Rest which its query provider builds a URI out of your expression.   &lt;br /&gt;Linq2Rest allows you to plug-in your own ‘RestClient&amp;#39; and ‘SerializerFactory’ which it then activates to get the data and serialize the results. This enabled me to provide my own implementations where I simply inject the URI query into a message header, call the service operation which the developer defined as part of the ‘QueryableGet’ and return the results.&lt;/p&gt;  &lt;p&gt;On the service end, the ‘QueryableAttribute’ injects an operation invoker which translates the query located in the message header back on to the queryable model returned from the service operation.&lt;/p&gt;  &lt;p&gt;.. And that’s about it.&lt;/p&gt;  &lt;h2&gt;Last Notes&lt;/h2&gt;  &lt;p&gt;You can download the &lt;a href="http://sdrv.ms/17B1X8l" target="_blank"&gt;source code&lt;/a&gt; if you like and experiment with it yourself, it’s quite cool I must say.    &lt;br /&gt;In this sample I created queries against a service with NetTcpBinding.&lt;/p&gt;  &lt;p&gt;Lastly, I’m afraid some of the operators are not yet supported.   &lt;br /&gt;For example, I couldn’t get ‘Expand’ to affect anything, even when working with Entity Framework. Plus, ‘Contains’ as part of a filter expression seems to be unsupported in the current version of Linq2Rest.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=2542749" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/WCF/default.aspx">WCF</category></item><item><title>WCF – Publishing Unreferenced Data Types</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2013/02/21/wcf-publishing-unreferenced-data-types.aspx</link><pubDate>Thu, 21 Feb 2013 13:42:55 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1758080</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=1758080</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2013/02/21/wcf-publishing-unreferenced-data-types.aspx#comments</comments><description>&lt;p&gt;The default behavior of WCF services is to expose schemas in the service metadata of the object data graph that is exposed in the service contract.   &lt;br /&gt;When consuming the metadata, the default behavior is very similar in the sense that it creates the object data graph that is exposed in the service contract.&lt;/p&gt;  &lt;p&gt;In some cases you may want to publish additional data as part of the service metadata even though it isn’t referenced somewhere in the service contract, that is other than the fault contracts, parameters object graph, and the return values.&lt;/p&gt;  &lt;p&gt;How can we do that then?   &lt;br /&gt;First, I searched in Google and came across &lt;a href="http://code.msdn.microsoft.com/windowsdesktop/WCF-WSDL-import-extension-93d3ffab" target="_blank"&gt;this article&lt;/a&gt; which provides a way to do so, but I found it to be far from perfect because of the following reasons -&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;It doesn’t provide a way to dynamically adding unreferenced known types&lt;/li&gt;    &lt;li&gt;It doesn’t support composite object graphs where one of the members could be of a type which is already exposed as part of the service metadata, an exception is thrown in this case&lt;/li&gt;    &lt;li&gt;It requires the client to use a behavior to get these data contracts, though there are ways to override this&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;The solution -&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;My goal was to use the &amp;#39;ServiceKnownTypeAttribute’ to include additional data types which are unreferenced in the service metadata.   &lt;br /&gt;Unfortunately, when you do that, WCF ignores that type because it is unreferenced by the service and it isn’t generated on the client.&lt;/p&gt;  &lt;p&gt;I found a workaround for this though, if you include the type ‘System.Object’ within the referenced data of your service, these additional types would actually be exposed and generated on the client side.   &lt;br /&gt;Obviously, I wouldn’t want to enforce the developer to include ‘System.Object’ anywhere in an explicit way such as adding a stub method, because that would basically suck a big time.&lt;/p&gt;  &lt;p&gt;The workaround I chose to do was to inject a fault contract of type ‘System.Object’ on a single operation, this has the least impact on the service and would be pretty much hidden from the developers in both sides because it is more of a tagging metadata rather than API.&lt;/p&gt;  &lt;p&gt;Doing that, I provide a solution that completes what I was aiming for -&lt;/p&gt;  &lt;p&gt;I can define unreferenced data types for my service that may include existing types within them, I can use the dynamic form of the ‘ServiceKnownTypeAttribute’ to define these types dynamically, and it would be generated on the client side without the need for a special behavior on its side too.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;It looks as the following -&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First we need to add the service behavior which injects the fault contract automatically for us -&lt;/p&gt;  &lt;p&gt;   &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f53bda59-6844-4605-97fd-16587647d5f3" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//The behavior that injects the fault contract&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;host.Description.Behaviors.Add(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;UnreferencedTypePublishingBehavior&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;());&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/p&gt;  &lt;p&gt;Then, we can use the ‘ServiceKnownTypeAttribute’ to include whatever we like -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:38f761c1-9495-42b6-8c6d-3e1068c4e1d8" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ServiceContract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ServiceKnownType&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CommonStatusCode&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;))]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;//[ServiceKnownType(&amp;quot;GetKnownTypes&amp;quot;, typeof(Helper))]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;interface&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IMyService&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;OperationContract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ActionResult&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; DoJob();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;OperationContract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ActionResult&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; DoJobWithNewVersionEnum();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;That’s it, when the client consumes the metadata, the ‘CommonStatusCode’ would be included in his service reference.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;A real-life example&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;You might wonder where you would need such a thing.   &lt;br /&gt;A valuable example would be to use Enum with service versioning in mind to support backward compatibility.    &lt;br /&gt;Enum is pretty problematic in terms of service versioning, &lt;a href="http://blogs.microsoft.co.il/blogs/zuker/archive/2009/03/21/wcf-and-enum-serialization.aspx" target="_blank"&gt;read here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Basically, if you change its members, it is considered to be a breaking change because that would blow up existing clients when they would try to deserialize the data with the new value.&lt;/p&gt;  &lt;p&gt;As the code sample shows, you can use this approach for using Enum in a way that it would be more fair to service versioning.   &lt;br /&gt;Typically, you can use an integer as the value to be transferred but expose the Enum as the additional unreferenced data type.    &lt;br /&gt;That way clients can match the integer with the generated Enum and map non-existing data as a default member that represents such cases.&lt;/p&gt;  &lt;p&gt;I welcome you to check the code example to get a complete notion of this approach.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Where’s the code?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;You can &lt;a href="https://skydrive.live.com/redir?resid=635A6C896115E61B!236" target="_blank"&gt;download the code&lt;/a&gt; if you like.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1758080" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/WCF/default.aspx">WCF</category></item><item><title>WCF – Custom Message Encoder and Operation Message Size</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/12/20/wcf-custom-message-encoder-and-operation-message-size.aspx</link><pubDate>Thu, 20 Dec 2012 17:23:56 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1574913</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=1574913</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/12/20/wcf-custom-message-encoder-and-operation-message-size.aspx#comments</comments><description>&lt;p&gt;You may find the need to determine the request and reply message size when calling a service operation.   &lt;br /&gt;It can be for logging purposes, or a practical one such as deciding whether you need to compress the data or not.&lt;/p&gt;  &lt;p&gt;The key component in WCF that knows about the actual size of the message is the message encoder, and of course, you can plug-in your own encoder as part of WCF’s extensibility point. Ahhh, got to love this framework!&lt;/p&gt;  &lt;p&gt;Lets examine the feature requirements first -&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;We need to have a way to get the incoming message size in our service implementation.&lt;/li&gt;    &lt;li&gt;We need a way to enable the client to get the reply message size.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;You can download the code &lt;a href="https://skydrive.live.com/redir?resid=635A6C896115E61B!231" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Basically, in order to implement a message encoder and to plug it in, you need to implement your own MessageEncoder, MessageEncoderFactory, and MessageEncodingBindingElement.   &lt;br /&gt;Additionally, since this is part of the binding elements, we need to plug it in as part of the binding.&lt;/p&gt;  &lt;p&gt;The implementation is fairly simple, since all I did was wrap the existing ones and added my message headers in the proper place.   &lt;br /&gt;I will not show this code here, it’s pretty straight forward, I recommend you just download the code and take a look at it.&lt;/p&gt;  &lt;p&gt;Finally, I implemented a behavior that you can attach to your service which does all the heavy lifting for you.   &lt;br /&gt;It actually switches the current binding with a wrapper binding that plugs in our custom message encoder, pretty sweet.&lt;/p&gt;  &lt;p&gt;In conclusion, here’s how our service would look like:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:53ebe5ef-2811-4319-86b7-fc8d41ec6481" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;OperationMessageSizeBehavior&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;MyService&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IMyService&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Do(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foo)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;MyService.Do() - Incoming request size: {&lt;/span&gt;&lt;span style="background:#ffffff;color:#3cb371;"&gt;0}&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;KB&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;,&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;OperationMessageSizeExtension&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Current.SizeInBytes / 1024);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;As you can see the usage is very simple.   &lt;br /&gt;You define the behavior on your service, then you can access the extension to retrieve the size of the incoming request and do whatever you want with it.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;One more tip – Keep the meta-data aligned:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I thought I should share a problem I experienced when I coded this – everything looked fine and it all worked.   &lt;br /&gt;However, when a developer tried to add a service reference against the service meta-data, he would get an incorrect binding.&lt;/p&gt;  &lt;p&gt;I was wondering why it happened, my wrappers were delegating every single operation to the existing ones, so what was I missing?&lt;/p&gt;  &lt;p&gt;Ahhhh.. Of course! Don’t forget about the ‘IWsdlExportExtension’ and ‘IPolicyExportExtension.   &lt;br /&gt;Encoding binding elements could be implementing those interfaces to participate in the building of the service meta-data.&lt;/p&gt;  &lt;p&gt;I fixed the problem by implementing these interfaces myself and forward it to the existing binding element if it too has implemented it.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1574913" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/WCF/default.aspx">WCF</category></item><item><title>WCF – Should PerCall Care About Concurrency? (And More..)</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/12/11/wcf-should-percall-care-about-concurrency-and-more.aspx</link><pubDate>Tue, 11 Dec 2012 16:35:17 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1539850</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=1539850</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/12/11/wcf-should-percall-care-about-concurrency-and-more.aspx#comments</comments><description>&lt;p&gt;This post discusses several questions:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Should ‘PerCall’ services care about the concurrency mode? &lt;/li&gt;    &lt;li&gt;Can closing the channel block after calling O/W operation? &lt;/li&gt;    &lt;li&gt;Should you reuse the same proxy instance concurrently? &lt;/li&gt;    &lt;li&gt;Does the Async Pattern matter in concurrent client? &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This is quite a long post, but it should be quite interesting &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/zuker/wlEmoticon-smile_1680C322.png" /&gt;, you should download the example &lt;a href="https://skydrive.live.com/redir?resid=635A6C896115E61B!157" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;In order to discuss the matter, let’s establish the scenarios we’re going to test first&lt;/u&gt;:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;About the contract&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:abe367ea-088f-4180-a427-e1933ef5150c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ServiceContract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;interface&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IMyService&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;OperationContract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Do();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;OperationContract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(IsOneWay = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;true&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;)]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; DoOW();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;About the service&lt;/strong&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The implementation of the service doesn’t matter, let’s make it simulate work using Thread.Sleep for 5 seconds. &lt;/li&gt;    &lt;li&gt;The service is configured as PerCall &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;About the binding&lt;/strong&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Standard NetTcpBinding &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;About the client&lt;/strong&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The client works with a contract with the addition of the asynchronous pattern operations (Begin/End) &lt;/li&gt;    &lt;li&gt;The client makes 2 concurrent requests to the service on the same channel using a certain invocation mode. (see further down) &lt;/li&gt;    &lt;li&gt;It closes the channel once both calls are completed &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;About the invocation modes&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;The findings vary according to how you call the service.    &lt;br /&gt;Let’s define 4 modes – RequestReply, RequestReplyAsync, OneWay, OneWayAsyncPattrn.     &lt;br /&gt;Typically, the implementation describes it pretty straightforward -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3f404cf1-bb91-48a2-b001-7afcf6c5084d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; GenerateTask(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IMyServiceExtended&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; channel, &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CallMode&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; callMode)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;switch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (callMode)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;case&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CallMode&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.RequestReply:&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(channel.Do);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;case&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CallMode&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.RequestReplyAsyncPattern:&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.FromAsync(channel.BeginDo, channel.EndDo, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;case&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CallMode&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.OneWay:&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(channel.DoOW);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;case&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CallMode&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.OneWayAsyncPattern:&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.FromAsync(channel.BeginDoOW, channel.EndDoOW, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;default&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;:&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;NotSupportedException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;Inspected scenarios&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;The variations for the scenarios are made with every combination of the service concurrency level and invocation mode.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;That’s it, so how about some answers?      &lt;br /&gt;&lt;/strong&gt;Note – I suggest you download the example and see the scenarios in action.&lt;/p&gt;  &lt;p&gt;First of all, some of the findings occur due to the selection of the binding.    &lt;br /&gt;NetTcpBinding creates a duplex session channel shape by nature, it doesn’t matter if the service is PerSession or not, that is why other bindings might result with different behavior.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt;: Should ‘PerCall’ services care about concurrency? &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In Short&lt;/strong&gt;: Yes!     &lt;br /&gt;The ConcurrencyMode is often described as a concurrency control in the access to the service instance.     &lt;br /&gt;If that was the case, PerCall services shouldn’t behave differently according to the ‘ConcurrencyMode’ you set on it, since every call gets a dedicated instance.     &lt;br /&gt;So, is that the case? No&lt;/p&gt;  &lt;p&gt;Try using the ‘OneWay’ invocation mode and see the differences between concurrency mode ‘Single’ and ‘Multiple’.    &lt;br /&gt;- Using ‘Single’ ends up with the two calls being invoked sequentially on the service side, causing the channel to be closed after 10 seconds.     &lt;br /&gt;- Using ‘Multiple’ ends up with the two calls being invoked concurrently on the service side, the channel is then closed after 5 seconds.&lt;/p&gt;  &lt;p&gt;There we go, using this scenario we see that you should consider setting the concurrency mode of your services, even if it’s PerCall.&lt;/p&gt;  &lt;p&gt;Another thing to note here is that closing the operation can block, as we see here, and can throw exceptions too.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt;: Can closing the channel block after calling O/W operation?&lt;/p&gt;  &lt;p&gt;As seen in the same scenario as above – Yes!&lt;/p&gt;  &lt;p&gt;Due to the session-ful duplex channel shape the NetTcpBinding creates, closing the channel waits for the operation to be completed to gracefully terminate the connection.    &lt;br /&gt;Another tip here is you can use the OneWayBindingElement which basically removes the session-bound channel shape, and then you can close it without waiting for the O/W operation to complete.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt;: Does the Async Pattern matter in concurrent client?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In Short&lt;/strong&gt;: Yes!     &lt;br /&gt;Try setting the ConcurrencyMode to Multiple and examine the difference between the invocation mode ‘RequestReply’ and ‘RequestReplyAsyncPattern’,     &lt;br /&gt;When calling a R/R operation in its synchronous form from concurrent environments, it isn’t really dispatched concurrently on the service.     &lt;br /&gt;Actually, it is being dispatched sequentially one after another.&lt;/p&gt;  &lt;p&gt;Oddly enough, if you use the asynchronous pattern invocation, it is dispatched concurrently on the service side!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt;: Should you reuse the same proxy instance in a concurrent environment?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In Short&lt;/strong&gt;: It Depends &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/zuker/wlEmoticon-smile_1680C322.png" /&gt;     &lt;br /&gt;First of all, ‘PerSession’ or Duplex services are out of the question in this case, as they may require a specific design of using them.     &lt;br /&gt;As you can see from the example above, when using the same proxy instance concurrently, it doesn’t assure you it is actually processed concurrently on the service.&lt;/p&gt;  &lt;p&gt;This basically means that concurrent calls can be queued up and cause a bottleneck and eventually blow up entirely.    &lt;br /&gt;Additionally, various tests we made at several customers showed that even if you manage to configure it as efficient as possible, where everything is being processed concurrently at both ends, intensive use of the same channel concurrently can cause an overload and turn the performance to the worse.&lt;/p&gt;  &lt;p&gt;The above basically indicates that you shouldn’t use the same instance concurrently.    &lt;br /&gt;However, you might find that using the same channel gets you better performance at specific loads, especially if you build infrastructure to separate the processing layer from the WCF runtime.&lt;/p&gt;  &lt;p&gt;To conclude the matter, you should assess the expected load and choose the right approach for your needs.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1539850" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/WCF/default.aspx">WCF</category></item><item><title>MS DTC – Can’t get it to work? Try Reinstalling – The Proper Way</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/12/06/ms-dtc-can-t-get-it-to-work-try-reinstalling-the-proper-way.aspx</link><pubDate>Thu, 06 Dec 2012 08:26:46 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1522180</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=1522180</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/12/06/ms-dtc-can-t-get-it-to-work-try-reinstalling-the-proper-way.aspx#comments</comments><description>&lt;p&gt;Boy that MS-DTC, huh? &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/zuker/wlEmoticon-smile_601DE8E2.png" /&gt; It can give much trouble, no doubt.    &lt;br /&gt;If you can’t communicate between two parties and keep failing with errors such as “The flowed transaction could not be unmarshaled”, try the following:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Use DNS instead of IP addresses&lt;/li&gt;    &lt;li&gt;Disable Firewall&lt;/li&gt;    &lt;li&gt;Remove security and enable the options in the MS-DTC security (in the Component Services shell)&lt;/li&gt;    &lt;li&gt;Try &lt;a href="http://www.microsoft.com/en-us/download/details.aspx?id=2868" target="_blank"&gt;DTCPing&lt;/a&gt; to &lt;a href="http://blogs.msdn.com/b/distributedservices/archive/2008/11/12/troubleshooting-msdtc-issues-with-the-dtcping-tool.aspx" target="_blank"&gt;troubleshoot&lt;/a&gt; the case&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;If everything seems to be set properly and you’re still experiencing connectivity issues, you should try reinstalling MS-DTC.   &lt;br /&gt;This is specifically necessary if the OS was created from an image, the MS-DTC relies on a unique ID, and if you use the same image, this could cause the problems.&lt;/p&gt;  &lt;p&gt;I strongly recommend you follow &lt;a href="http://community.newdawn.com/knowledgebase/articles/72759-how-to-uninstall-and-reinstall-the-ms-dtc" target="_blank"&gt;these instructions&lt;/a&gt; for removing MS-DTC and reinstalling it. In some cases, I had to do this complete wipe and install to solve the case.&lt;/p&gt;  &lt;p&gt;Good luck.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1522180" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category></item><item><title>.NET 4.5 Async “ConfigureAwait” – Keep the Synchronization Context in Mind</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/12/01/net-4-5-async-configureawait-keep-the-synchronization-context-in-mind.aspx</link><pubDate>Sat, 01 Dec 2012 15:12:54 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1502609</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=1502609</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/12/01/net-4-5-async-configureawait-keep-the-synchronization-context-in-mind.aspx#comments</comments><description>&lt;p&gt;I assume you have heard about the great addition in .NET 4.5 – The Async Keyword.&lt;/p&gt;  &lt;p&gt;It is truly one of the key changes made to the .NET language, and it is awesome.    &lt;br /&gt;Asynchrony should be part of our code, there’s no escape from that.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;If you write UI applications – you need asynchrony to avoid blocking the UI thread. &lt;/li&gt;    &lt;li&gt;If you make I/O operations – you should use its asynchronous model that utilizes the I/O completion threads instead of occupying a worker thread. &lt;/li&gt;    &lt;li&gt;If you build a service – you may want to implement the service using the asynchronous approach if you’re doing mainly I/O work. &lt;/li&gt;    &lt;li&gt;If you want to parallelize your code – you may want to use the TPL and invoke operations asynchronously and in parallel. &lt;/li&gt;    &lt;li&gt;And more.. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Prior to .NET 4.5, the code looks quite awful in terms of all the nested lambda expressions, error handling, and the use of the synchronization context.    &lt;br /&gt;It was also easy to make mistakes writing in this fairly complex paradigm, but I guess it comes down to the developer’s maturity and habits. I specifically got quite used to that by now, so it is all readable to me &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/zuker/wlEmoticon-smile_3D906BA3.png" /&gt;.&lt;/p&gt;  &lt;p&gt;In .NET 4.5, Microsoft introduced us with the new ‘async’ and the ‘await’ keywords.    &lt;br /&gt;The goal of this post isn’t to explain what it does, there are enough resources out there already that you can follow up on if you like to catch up.&lt;/p&gt;  &lt;p&gt;I would like to discuss a specific feature of the ‘await’ keyword, which is continuing the execution on the captured synchronization context.    &lt;br /&gt;When you write the following code -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dcd32607-1d5b-480a-bea9-972be167e29f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;async&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Do()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Thread Id: {&lt;/span&gt;&lt;span style="background:#ffffff;color:#3cb371;"&gt;0}&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.CurrentThread.ManagedThreadId);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; DoSomethingAsync();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Thread Id: {&lt;/span&gt;&lt;span style="background:#ffffff;color:#3cb371;"&gt;0}&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.CurrentThread.ManagedThreadId);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; DoSomethingAsync()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Run(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Sleep(1000);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Thread Id: {&lt;/span&gt;&lt;span style="background:#ffffff;color:#3cb371;"&gt;0}&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.CurrentThread.ManagedThreadId);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;Q: What would be printed in line 7? Would it be the same as in line 3?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;That’s a trick question &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/zuker/wlEmoticon-smile_3D906BA3.png" /&gt;     &lt;br /&gt;Using the ‘await’ keyword at its default form results in the continuation to be posted to the synchronization context available before beginning the asynchronous work.     &lt;br /&gt;This means that the output of line 7 relies on the implementation of the synchronization context.     &lt;br /&gt;If you’re inside a WPF application, the DispatcherSynchronizationContext will post it to the same thread, so line 7 would end up printing the same Id as in line 3.     &lt;br /&gt;However, if you’re in environment where there isn’t such synchronization implementation, such as the default .NET ‘SynchronizationContext’ which simply queues the work to the thread pool, you should end up with something else.&lt;/p&gt;  &lt;p&gt;This question was just for fun though, the key thing I wanted to emphasize is -    &lt;br /&gt;&lt;strong&gt;Posting the continuation to the captured synchronization context is wasteful in case you don’t care about it. &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;That is especially true if you’re in a UI application, where the continuations are made on the UI thread, and you may not even need access to UI resources at all.&lt;/p&gt;  &lt;p&gt;Furthermore, even in case your code executes inside a synchronization implementation where it just queues it to the thread pool, there’s still some performance hit, and though it is fairly minimal, if you can modify your code to be context agnostic and basically work as efficient as possible, you should do it.&lt;/p&gt;  &lt;p&gt;In conclusion, in case you don’t care about the synchronization context, you should use the ‘ConfigureAwait’ method on the await and switch it off.    &lt;br /&gt;The above code could be fixed as following – (The change is at line 5)&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9bb49885-2058-4dcf-816c-be86288dea37" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;async&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Do()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Thread Id: {&lt;/span&gt;&lt;span style="background:#ffffff;color:#3cb371;"&gt;0}&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.CurrentThread.ManagedThreadId);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; DoSomethingAsync().ConfigureAwait(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;false&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Thread Id: {&lt;/span&gt;&lt;span style="background:#ffffff;color:#3cb371;"&gt;0}&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.CurrentThread.ManagedThreadId);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; DoSomethingAsync()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Run(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Sleep(1000);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Thread Id: {&lt;/span&gt;&lt;span style="background:#ffffff;color:#3cb371;"&gt;0}&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.CurrentThread.ManagedThreadId);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;There’s a demo project if you like to &lt;a href="https://skydrive.live.com/redir?resid=635A6C896115E61B!229" target="_blank"&gt;download&lt;/a&gt;.     &lt;br /&gt;Just make sure you use the WPF application in case you wish to see that the thread ID is the same as line 3 when not switching the feature off.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1502609" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/.NET/default.aspx">.NET</category></item><item><title>WPF PRISM DelegateCommand Wrappers in .NET 4.5</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/11/27/wpf-prism-delegatecommand-wrappers-in-net-4-5.aspx</link><pubDate>Tue, 27 Nov 2012 13:42:06 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1487531</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=1487531</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/11/27/wpf-prism-delegatecommand-wrappers-in-net-4-5.aspx#comments</comments><description>&lt;p&gt;If you happened to implement an ‘ICommand’ for your presentation logic in WPF, and decided to contain PRISM’s DelegateCommand within your class and use its own mechanism of the ‘CanExecuteChanged’ event – Beware!&lt;/p&gt;  &lt;p&gt;Consider the following example -&lt;/p&gt;  &lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:650px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; CommandWrapper : ICommand
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;{
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; DelegateCommand _command;
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; CommandWrapper(Action execute, Func&amp;lt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt;&amp;gt; canExecute)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;        _command = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DelegateCommand(execute, canExecute);
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; CanExecute(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; parameter)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _command.CanExecute(parameter);
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Execute(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; parameter)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;        _command.Execute(parameter);
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; RaiseCanExecuteChanged()
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;        _command.RaiseCanExecuteChanged();
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;event&lt;/span&gt; EventHandler CanExecuteChanged
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;        add { _command.CanExecuteChanged += &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;        remove { _command.CanExecuteChanged -= &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;
.NET 4.5 isn’t compatible with this approach! (With wrappers to any ICommand implementation) 

&lt;br /&gt;If you upgrade to .NET 4.5, you will see that the UI doesn’t respond in case you raise the ‘CanExecuteChanged’ event in this scenario. 

&lt;br /&gt;There’s actually an opened support ticket on that - &lt;a href="http://connect.microsoft.com/VisualStudio/feedback/details/771394/backward-compatibility-problem-in-net-4-5-wpf-icommand-sender-weakeventmanager" target="_blank"&gt;Backward Compatibility Problem in .NET 4.5: WPF / ICommand sender / WeakEventManager&lt;/a&gt; 

&lt;p&gt;I fixed it in our products by simply inheriting from DelegateCommand, instead of containing it.&lt;/p&gt;

&lt;p&gt;You can download a &lt;a href="https://skydrive.live.com/redir?resid=635A6C896115E61B!228" target="_blank"&gt;sample project&lt;/a&gt; which demonstrates the problem. You can see it happens when the project is compiled to .NET 4.0 or 4,5, but it is actually working well when it is compiled to .NET 3.5.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: Luckily Microsoft identified it as a bug and provided a &lt;a href="http://support.microsoft.com/kb/2750149" target="_blank"&gt;global update&lt;/a&gt; that fixes this issue and many others.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1487531" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/WPF/default.aspx">WPF</category></item><item><title>Blog.BringBackToLife();</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/11/25/blog-bringbacktolife.aspx</link><pubDate>Sun, 25 Nov 2012 10:07:24 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1478417</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=1478417</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2012/11/25/blog-bringbacktolife.aspx#comments</comments><description>&lt;p&gt;Yes, I am aware I didn’t post anything for a long time, sorry about that.&lt;/p&gt;  &lt;p&gt;I can try and excuse myself by saying that I had plenty on my hands, including bringing my house together, raising a new baby girl, and much more.. but I won’t &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/zuker/wlEmoticon-smile_10BBE9FB.png" /&gt;&lt;/p&gt;  &lt;p&gt;Putting everything aside, I plan to get right back on the horse and start posting some interesting stuff real soon, so stay tuned!&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1478417" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/OFFTOPIC/default.aspx">OFFTOPIC</category></item><item><title>Expert Days 2011 – Here I Come!</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2011/06/11/expert-days-2011-here-i-come.aspx</link><pubDate>Sun, 12 Jun 2011 00:48:29 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:842216</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=842216</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2011/06/11/expert-days-2011-here-i-come.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://www.expertdays.co.il"&gt;&lt;img border="0" alt="Expert Days" src="http://blogs.microsoft.co.il/blogs/stiller/image_7A65CB9D.png" width="628" height="181" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.expertdays.co.il"&gt;Expert Days&lt;/a&gt; is an annual software development conference held by E4D in Israel. This year, for the first time, some of &lt;a href="http://codevalue.net"&gt;CodeValue’s&lt;/a&gt; experts have joined forces with E4D and are participating in order to give you, the attendee, the best possible value. If you are a software developer, software architect, team leader or somehow interested in the hottest available Microsoft technologies – this is definitely the conference you want to be at.&lt;/p&gt;  &lt;p&gt;The conference is held on 10/07/2011-14/07/2011, and there are &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/AllTracks"&gt;dozens of one-day workshops&lt;/a&gt;, which are packed with practical knowledge which you can immediately use. Investing your time in these workshops will certainly payoff immediately!&lt;/p&gt;  &lt;p&gt;For more information regarding the available workshops, feel free to click the banner below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/AllTracks"&gt;&lt;img border="0" alt="Workshops" src="http://blogs.microsoft.co.il/blogs/stiller/image_1F06BD48.png" width="484" height="140" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Among all of the conference’s experts, you can also find &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/Trainer/16" target="_blank"&gt;me&lt;/a&gt; there this year and I gladly invite you to participate in my workshop – &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Courses/Details/39" target="_blank"&gt;Developing .NET Systems in Real-Life&lt;/a&gt;. Note that if you specify that you arrived through me and specify my instructor code (&lt;b&gt;121&lt;/b&gt;) you can get a &lt;b&gt;20% discount&lt;/b&gt; to my workshop!&lt;/p&gt;  &lt;p&gt;Except for my workshop, I personally recommend arriving at workshops performed by some of the other &lt;a href="http://codevalue.net"&gt;CodeValue&lt;/a&gt; experts – &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/Trainer/14"&gt;Alon Fliess&lt;/a&gt;, &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/Trainer/17" target="_blank"&gt;Eran Stiller&lt;/a&gt;, &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/Trainer/18"&gt;Eli Arbel&lt;/a&gt;, &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/Trainer/19"&gt;Josh Reuben&lt;/a&gt; &amp;amp; &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/Trainer/15"&gt;Shay Friedman&lt;/a&gt;.&lt;/p&gt;  &lt;h5&gt;But wait – there’s more!&lt;/h5&gt;  &lt;p&gt;This year, &lt;a href="http://www.expertdays.co.il"&gt;Expert Days&lt;/a&gt; has a free &lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/OpeningEvent"&gt;opening event&lt;/a&gt; on 10/07/2011. This event includes a cocktail party, mingling and a series of professional lectures all of which are absolutely free. During the opening event, an XBOX+Kinnect set will be given to one of the attendees. That’s right – an XBOX+Kinnect set shall be given away to one of you! All you have to do is register and show up…&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.e4d.co.il/Events/ExpertDays2011/Home/OpeningEvent"&gt;&lt;img border="0" alt="Opening Event" src="http://blogs.microsoft.co.il/blogs/stiller/image_530B36CE.png" width="442" height="127" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;See you there,    &lt;br /&gt;Amir&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=842216" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/TECH/default.aspx">TECH</category></item><item><title>MEF – Thread Safety and GetExportedValue</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2011/01/02/mef-thread-safety-and-getexportedvalue.aspx</link><pubDate>Sun, 02 Jan 2011 21:11:11 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:762117</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>37</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=762117</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2011/01/02/mef-thread-safety-and-getexportedvalue.aspx#comments</comments><description>&lt;p&gt;In one of our projects, we encountered odd exceptions that were thrown out of “GetExportedValue&amp;lt;T&amp;gt;” from the CompositionContainer of MEF.&lt;/p&gt;  &lt;p&gt;There are 2 exceptions that I have seen that are related to the same issue:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;System.InvalidOperationException&lt;/strong&gt;: GetExportedValue cannot be called before prerequisite import &amp;#39;MyType..ctor(Parameter=&amp;quot;myParameter&amp;quot;, ContractName=&amp;quot;MyOtherType&amp;quot;)&amp;#39; has been set&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;System.ArgumentException&lt;/strong&gt;: An item with the same key has already been added.&lt;/p&gt;  &lt;p&gt;After digging into the matter, we found that these errors occurred while trying to call GetExportedValue concurrently from numerous threads.   &lt;br /&gt;Yeah, simple as that, MEF isn’t thread-safe in its default form.&lt;/p&gt;  &lt;p&gt;Following is a sample code which demonstrates the error -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e1f858c7-a62f-4665-b06a-0b6cf00bf63b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; { }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;))]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;[&lt;span style="color:#2b91af;"&gt;PartCreationPolicy&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;CreationPolicy&lt;/span&gt;.Shared)]&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; { }&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fa41f6f9-c83b-4333-a949-cacc1877ce5a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Program&lt;/span&gt;).Assembly);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;(catalog);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;gt; tasks = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 5; i++)&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    tasks.Add(&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; container.GetExportedValue&amp;lt;&lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;&amp;gt;()));&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.WaitAll(tasks.ToArray());&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;No errors, thread safe&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;AggregateException&lt;/span&gt; ex)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Error occurred: {0}&amp;quot;&lt;/span&gt;, ex.InnerException.ToString());&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;In this example, “Task.WaitAll” would throw an exception because of the concurrent hit on GetExportedValue.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;How to fix this -&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Well, the fix is as easy as it can be. You need to construct the container as thread safe -&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:11f92885-e0e5-40cb-a566-9165827e42d0" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;(catalog, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;In the second parameter we pass ‘true’ to make it thread-safe.&lt;/p&gt;  &lt;p&gt;That’s it! dead easy.   &lt;br /&gt;You should note that it has a performance hit though, obviously.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=762117" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/MEF/default.aspx">MEF</category></item><item><title>TechEd 2010 Eilat - Great so far</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/11/29/teched-2010-eilat-great-so-far.aspx</link><pubDate>Mon, 29 Nov 2010 23:35:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:750945</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=750945</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/11/29/teched-2010-eilat-great-so-far.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;I decided to blog alittle about my ongoing experience here in TechEd Eilat.&lt;br /&gt;Yesterday, on day 1, I arrived to Eilat and immediately got the great atmosphere this conference has put out for us.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Day 1&lt;/b&gt;&lt;/p&gt;&lt;p&gt;I attended the evening key-note session and was very impressed by the operation the Microsoft has gathered and pulled it off beautifully. It wasn&amp;#39;t hard to see that there was a great investment in this conference, and I&amp;#39;m simply glad.&lt;br /&gt;The key-note was refreshing with good entertainment. There wasn&amp;#39;t much that came new to me, but it was certainly nice to see how Microsoft sees the next generation in a new world of sharing and collaboration being materialized.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Day 2&lt;/b&gt;&lt;/p&gt;&lt;p&gt;I attended 2 sessions - Windows Azure Guidance Part 1 and 2. I liked these presentations very much, they met my expectations perfectly.&lt;br /&gt;There wasn&amp;#39;t any code, as much as I love to see code in sessions, that&amp;#39;s what I expected. The presenter talked about all the key components in Windows Azure, explaining about them and how you should generally use them. That&amp;#39;s what I was aiming for, just to get a bit more organized about things, and these sessions did just that.&lt;/p&gt;&lt;p&gt;Another session that I attended today was Deep Dive on Workflow Services. It was rather disappointing, don&amp;#39;t get me wrong, the session was interesting and well built but I was expecting a more of a deep dive than what was actually there.&lt;/p&gt;&lt;p&gt;All in all, so far I&amp;#39;m having a blast. Soon there&amp;#39;s the big party and I&amp;#39;m sure it would be great.&lt;/p&gt;&lt;p&gt;For day 3, I plan to go see the tooling presentation made by Alon Fliess, the CTO of our new company. I&amp;#39;m pretty sure I&amp;#39;ll recognize everything there, but knowing Alon, this will be very professional and amusing to watch.&lt;br /&gt;Another session that I plan to attend is about storage in Azure, I guess it&amp;#39;ll be good too.&lt;/p&gt;&lt;p&gt;To summarize, the TechEd met my expectations in terms of content sessions and the execution of everything around it had been fabulous up until now. Microsoft - good job! thank you. &lt;br /&gt;&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=750945" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/TECH/default.aspx">TECH</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/Teched_5F00_il/default.aspx">Teched_il</category></item><item><title>Cost-Oriented Development</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/11/27/cost-oriented-development.aspx</link><pubDate>Sun, 28 Nov 2010 01:29:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:749564</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=749564</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/11/27/cost-oriented-development.aspx#comments</comments><description>&lt;p&gt;With the emerging of cloud computing, our perception of software design needs to shift and take account for the affects we need to deal with.    &lt;br /&gt;The signs are clear, with investments of billions of dollars by leading companies, such as Microsoft, Google, Amazon - Cloud computing is the future of the IT industry and many other specific product companies that can leverage its benefits.&lt;/p&gt;  &lt;p&gt;If you looked into it, you must know by now – the pricing models for hosting applications in the cloud varies and you can choose from different packages to meet the requirements of your specific application.    &lt;br /&gt;How do you choose the right one? Well, that isn’t a simple question and it very much depends on the characteristics of your application.     &lt;br /&gt;Does it have enormous data behind it? Is it mostly data-centric with a lot of transactions? How busy is the front-end? How many cores would you need? Do you expect different request peaks at different times? And the list goes on..&lt;/p&gt;&lt;p&gt;Developing for the cloud requires good architecture if you want to leverage its true powers.&lt;br /&gt;While there&amp;#39;s a general architecture on how to program things in order to gain good practices, such as scalability, good performance and fault tolerance, this
 post is written specifically to present the Cost-Oriented Development and Architecture approach as a 
new relevant concept.&lt;br /&gt;This subject no short and simple, in future posts I plan to include technical examples, best 
practices and design principles as well. &lt;/p&gt;&lt;p&gt;&lt;b&gt;Why should you care?&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;In most companies, the hardware is purchased and deployed to support the expected traffic and requirements and the applications are developed with no unique thought to address the cost-related circumstances.    &lt;br /&gt;This approach should change. With cloud computing, the operating system and hardware is ready for you in an environment where you essentially pay-per-use for the resources you utilize. (May vary according to the specific pricing package you purchased)&lt;/p&gt;  &lt;p&gt;In such world, inefficient code and resource-utilization &lt;b&gt;should &lt;/b&gt;be your concern! This has a much more direct affect, inefficient code means less money in your pocket.     &lt;br /&gt;To clarify, inefficient in that sense stands for &lt;b&gt;cost-inefficiency&lt;/b&gt;, thus the term – &lt;b&gt;Cost-Oriented Development.&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Cost-inefficiency can obviously stand for performance-based issues, but also simple yet unaware mistakes of resource utilization or bad practices when it comes to cloud development.    &lt;br /&gt;Product companies which want to be cost-optimized, need to understand the implications of the code in terms of how it can affect the hosting price.     &lt;br /&gt;You can see some examples in the following article - &lt;a href="http://technet.microsoft.com/en-us/magazine/gg213848.aspx" target="_blank"&gt;Windows Azure: Cost Architecting for Windows Azure&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;When developing for the cloud – there is no such thing as “cheap developers”, this can end up costing you a lot of money down the road, unless you plan to invest in code review, guidance, and perhaps seeking for consult.&lt;/p&gt;    &lt;p&gt;&lt;b&gt;So what next?&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This is the main aspect my new company assists with and this is only the beginning. &lt;/p&gt;  &lt;p&gt;CloudValue is a Cost-Oriented Development solution company. We provide tools &amp;amp; methodologies for cost optimized applications targeting the new Cloud platforms, and essentially help you to save a lot of money.    &lt;br /&gt;In addition to providing professional services all around, our leading product, Cloudoscope™, the first Cost-Profiler ever, will help you write cost-optimized code and provide you with the ability to review your code, statically or dynamically, to predict problematic spots, inspect actual cost-value of every request and generally help you understand exactly what you are paying for and how you can make it even better.&lt;/p&gt;  &lt;p&gt;Enough with the teasers, read more about us – &lt;a href="http://cloudvalue.com/" target="_blank"&gt;CloudValue&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=749564" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/TECH/default.aspx">TECH</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/CodeValue/default.aspx">CodeValue</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/Teched_5F00_il/default.aspx">Teched_il</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/CloudValue/default.aspx">CloudValue</category></item><item><title>TechEd Eilat 2010</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/11/23/teched-eilat-2010.aspx</link><pubDate>Tue, 23 Nov 2010 19:36:16 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:747121</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=747121</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/11/23/teched-eilat-2010.aspx#comments</comments><description>&lt;p&gt;I’d like to thank Michal and Microsoft for sending me to the TechEd in Eilat next week.   &lt;br /&gt;There are some presentations which seem cool and I’m happy to get the chance to hear them out in person.&lt;/p&gt;  &lt;p&gt;I plan to blog about interesting sessions, so tune in if you like.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/zuker/image_31B2A033.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="image" border="0" alt="image" src="http://blogs.microsoft.co.il/blogs/zuker/image_thumb_1D4CDAA8.png" width="193" height="168" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=747121" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/TECH/default.aspx">TECH</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/Teched_5F00_il/default.aspx">Teched_il</category></item><item><title>WCF – TCP with UserNameToken without Message Security</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/11/07/wcf-tcp-with-usernametoken-without-message-security.aspx</link><pubDate>Sun, 07 Nov 2010 18:41:14 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:738631</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=738631</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/11/07/wcf-tcp-with-usernametoken-without-message-security.aspx#comments</comments><description>&lt;p&gt;There was a project that I assisted with the WCF communications where they needed to allow the client to specify different credentials without being dependent on the windows account.&lt;/p&gt;  &lt;p&gt;The first thing that comes into mind is to use the UserNameToken technique to pass in the client credentials. The design instructed to use TCP as the transport and not use message security. Obviously, this technique has privacy and integrity issues where there isn’t any encryption nor signing, but that was their decision because it wasn’t an issue in the purpose of the project.&lt;/p&gt;  &lt;p&gt;Well, this setting isn’t as trivial as you would expect.   &lt;br /&gt;The default form of the NetTcpBinding allows you to use UserNameToken only as part of message security and forces you to use a certificate to enable that message security.&lt;/p&gt;  &lt;p&gt;I ended up setting them a CustomBinding which provides the scenario they needed.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Service Side&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Configuring the Service Host -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:aaca3724-5018-4f8d-8429-c163beea3b83" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;_host = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ServiceHost&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Service&lt;/span&gt;));&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;_host.AddServiceEndpoint(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IService&lt;/span&gt;), &lt;span style="color:#2b91af;"&gt;Config&lt;/span&gt;.ServiceBinding, &lt;span style="color:#2b91af;"&gt;Config&lt;/span&gt;.ServiceAddress.Uri.AbsoluteUri);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;_host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = &lt;span style="color:#2b91af;"&gt;UserNamePasswordValidationMode&lt;/span&gt;.Custom;&lt;/li&gt; &lt;li&gt;_host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CustomUserNameTokenValidator&lt;/span&gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;_host.Open();&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Custom Simple Validator (you can write any logic you need here) -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bc87d7db-f49e-463a-8a9d-57193bc28a40" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CustomUserNameTokenValidator&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;UserNamePasswordValidator&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Validate(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; userName, &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; password)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;CustomUserNameTokenValidator.Validate - {0} / {1}&amp;quot;&lt;/span&gt;, userName, password);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.IsNullOrEmpty(userName))&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SecurityTokenValidationException&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Invalid username&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;In the service code, you can extract the caller’s identity name as follows -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f50ce8b2-138b-445a-8ed1-41ce8709ff19" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Service&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IService&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Do()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Service.Do() - Identity Name: {0}&amp;quot;&lt;/span&gt;, &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#2b91af;"&gt;ServiceSecurityContext&lt;/span&gt;.Current.PrimaryIdentity.Name);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;Client Side&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In the client side you need to use the same binding, provide the UserNameToken credentials and simply call the service -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:767b13e7-dec1-445b-9146-8a5c3a3ffb94" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;ChannelFactory&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;IService&lt;/span&gt;&amp;gt; factory = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ChannelFactory&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;IService&lt;/span&gt;&amp;gt;(&lt;span style="color:#2b91af;"&gt;Config&lt;/span&gt;.ServiceBinding, &lt;span style="color:#2b91af;"&gt;Config&lt;/span&gt;.ServiceAddress);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;factory.Credentials.UserName.UserName = &lt;span style="color:#a31515;"&gt;&amp;quot;myUser&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;factory.Credentials.UserName.Password = &lt;span style="color:#a31515;"&gt;&amp;quot;myPassword&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;IService&lt;/span&gt; proxy = factory.CreateChannel();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;proxy.Do();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;((&lt;span style="color:#2b91af;"&gt;ICommunicationObject&lt;/span&gt;)proxy).Close();&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;Configuration&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Following is the CustomBinding which enables this scenario -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bb69b21a-331f-4a73-954a-c200c7e28470" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;SecurityBindingElement&lt;/span&gt; securityElement = &lt;span style="color:#2b91af;"&gt;SecurityBindingElement&lt;/span&gt;.CreateUserNameOverTransportBindingElement();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;((&lt;span style="color:#2b91af;"&gt;TransportSecurityBindingElement&lt;/span&gt;)securityElement).AllowInsecureTransport = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;_serviceBinding = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CustomBinding&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BindingElement&lt;/span&gt;[] {&lt;/li&gt; &lt;li&gt;    securityElement,&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BinaryMessageEncodingBindingElement&lt;/span&gt;(),&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TcpTransportBindingElement&lt;/span&gt;()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;});&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Feel free to download the &lt;a href="http://cid-635a6c896115e61b.office.live.com/self.aspx/Public%20View/Code%20Samples/WCF/WcfTcpUsernameTokenUnsecured.zip" target="_blank"&gt;source code&lt;/a&gt; and see that in action.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=738631" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/WCF/default.aspx">WCF</category></item><item><title>MEF – Conventional catalog for registering non-attributed parts</title><link>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/10/18/mef-conventional-catalog-for-registering-non-attributed-parts.aspx</link><pubDate>Mon, 18 Oct 2010 17:38:27 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:727632</guid><dc:creator>Amir Zuker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/zuker/rsscomments.aspx?PostID=727632</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/zuker/archive/2010/10/18/mef-conventional-catalog-for-registering-non-attributed-parts.aspx#comments</comments><description>&lt;p&gt;As part of the task where I had to upgrade an existing WPF project to PRISM 4.0 and work with MEF, one of the requirements was to support registering existing non-attributed parts.&lt;/p&gt;  &lt;p&gt;Consider the following example -&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1b38904e-e1a2-4375-9e45-063c7af9b086" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; { }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; { }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Main(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;[] args)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000;"&gt;//This is the desired thing to do&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    myCatalog.Register&amp;lt;&lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;As you can see, type Foo has no export definitions set on it.   &lt;br /&gt;One approach would be to go over all the registrations and add attributes where needed, or create wrappers for legacy components, but the goal in our case was to support this kind of registration.&lt;/p&gt;  &lt;p&gt;After digging around, I found two ways that you can go about it -&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Implement a conventional catalog using the ReflectionModelServices which is the MEF’s API for building reflection related parts information.&lt;/li&gt;    &lt;ol&gt;     &lt;li&gt;Example - &lt;a href="http://codepaste.net/yadusn" target="_blank"&gt;CodePaste.NET - A conventional catalog for MEF (Prototype)&lt;/a&gt;        &lt;br /&gt;&lt;/li&gt;   &lt;/ol&gt;    &lt;li&gt;Use the MEF’s AttributedModelServices with a TypeDelegator to expose the actual type as an attributed type even though it doesn’t have attributes defined directly on it.     &lt;br /&gt;This is quite a sophisticated solution and I decided to stick with that one.&lt;/li&gt;    &lt;ol&gt;     &lt;li&gt;Example - &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/04/27/poco-mef-and-custom-type-systems-are-you-ready-to-take-the-red-pill.aspx" target="_blank"&gt;Poco, Mef, and custom type systems. Are you ready to take the red pill&lt;/a&gt;&lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;p&gt;You can look at the links to see the actual code of each solution. Plus, you can integrate it with the type catalog which supports type registration in runtime which I described in my &lt;a href="http://blogs.microsoft.co.il/blogs/zuker/archive/2010/10/17/mef-runtime-type-catalog-support-multi-registrations-in-runtime.aspx" target="_blank"&gt;previous post&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=727632" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/zuker/archive/tags/MEF/default.aspx">MEF</category></item></channel></rss>