<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>Dot Net What Not - WCF</title>
    <link>http://dotnetwhatnot.pixata.co.uk/</link>
    <description>My rambling thoughts on exploring the .NET framework and related technologies</description>
    <language>en-gb</language>
    <copyright>AY Silver</copyright>
    <lastBuildDate>Tue, 10 May 2011 14:21:00 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>dasblog@example.com</managingEditor>
    <webMaster>dasblog@example.com</webMaster>
    <item>
      <trackback:ping>http://dotnetwhatnot.pixata.co.uk/Trackback.aspx?guid=f925d3dd-b7cd-43f0-8b5b-fe4cf41a0515</trackback:ping>
      <pingback:server>http://dotnetwhatnot.pixata.co.uk/pingback.aspx</pingback:server>
      <pingback:target>http://dotnetwhatnot.pixata.co.uk/PermaLink,guid,f925d3dd-b7cd-43f0-8b5b-fe4cf41a0515.aspx</pingback:target>
      <dc:creator>AY Silver</dc:creator>
      <wfw:comment>http://dotnetwhatnot.pixata.co.uk/CommentView,guid,f925d3dd-b7cd-43f0-8b5b-fe4cf41a0515.aspx</wfw:comment>
      <wfw:commentRss>http://dotnetwhatnot.pixata.co.uk/SyndicationService.asmx/GetEntryCommentsRss?guid=f925d3dd-b7cd-43f0-8b5b-fe4cf41a0515</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When you use Linq to create a query against an entity framework model, a common scenario
is to use the .Include() extension method to make sure that any child objects are
also loaded. This is mainly useful when the results of the query are to be sent over
WCF, where the client is disconnected from the source of the query, and so cannot
go back to the database to pick up any child objects as needed.
</p>
        <p>
This works fine for simple queries, but falls apart when you want to do anything clever,
like joins or shaping.
</p>
        <p>
Without going into details, all you need to do is cast the query to an ObjectQuery&lt;&gt;
and use .Include() on that. The syntax is not as obvious as it might be, so here’s
an example…
</p>
        <pre class="csharpcode">ObjectQuery&lt;Ferret&gt; ferretQuery = ((from f <span class="kwrd">in</span> ctx.Ferrets
select f) <span class="kwrd">as</span> ObjectQuery&lt;Ferret&gt;) .Include(<span class="str">"User"</span>);</pre>
        <p>
This particular example is too simple to require this trick, but I didn’t want to
distract from the syntax of the cast.
</p>
        <p>
I got this trick (after a long time of frustrated scratching of head at some of Microsoft’s
more obscure and less helpful error messages) from <a href="http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx" target="_blank">this
blog post</a>.
</p>
        <img width="0" height="0" src="http://dotnetwhatnot.pixata.co.uk/aggbug.ashx?id=f925d3dd-b7cd-43f0-8b5b-fe4cf41a0515" />
      </body>
      <title>How to persuade a Linq query to include your child objects</title>
      <guid isPermaLink="false">http://dotnetwhatnot.pixata.co.uk/PermaLink,guid,f925d3dd-b7cd-43f0-8b5b-fe4cf41a0515.aspx</guid>
      <link>http://dotnetwhatnot.pixata.co.uk/2011/05/10/HowToPersuadeALinqQueryToIncludeYourChildObjects.aspx</link>
      <pubDate>Tue, 10 May 2011 14:21:00 GMT</pubDate>
      <description>&lt;p&gt;
When you use Linq to create a query against an entity framework model, a common scenario
is to use the .Include() extension method to make sure that any child objects are
also loaded. This is mainly useful when the results of the query are to be sent over
WCF, where the client is disconnected from the source of the query, and so cannot
go back to the database to pick up any child objects as needed.
&lt;/p&gt;
&lt;p&gt;
This works fine for simple queries, but falls apart when you want to do anything clever,
like joins or shaping.
&lt;/p&gt;
&lt;p&gt;
Without going into details, all you need to do is cast the query to an ObjectQuery&amp;lt;&amp;gt;
and use .Include() on that. The syntax is not as obvious as it might be, so here’s
an example…
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;ObjectQuery&amp;lt;Ferret&amp;gt; ferretQuery = ((from f &lt;span class="kwrd"&gt;in&lt;/span&gt; ctx.Ferrets
select f) &lt;span class="kwrd"&gt;as&lt;/span&gt; ObjectQuery&amp;lt;Ferret&amp;gt;) .Include(&lt;span class="str"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;
This particular example is too simple to require this trick, but I didn’t want to
distract from the syntax of the cast.
&lt;/p&gt;
&lt;p&gt;
I got this trick (after a long time of frustrated scratching of head at some of Microsoft’s
more obscure and less helpful error messages) from &lt;a href="http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx" target="_blank"&gt;this
blog post&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://dotnetwhatnot.pixata.co.uk/aggbug.ashx?id=f925d3dd-b7cd-43f0-8b5b-fe4cf41a0515" /&gt;</description>
      <comments>http://dotnetwhatnot.pixata.co.uk/CommentView,guid,f925d3dd-b7cd-43f0-8b5b-fe4cf41a0515.aspx</comments>
      <category>Linq</category>
      <category>WCF</category>
    </item>
    <item>
      <trackback:ping>http://dotnetwhatnot.pixata.co.uk/Trackback.aspx?guid=1bd63dbc-5a99-4f7a-9899-9e82ab16316a</trackback:ping>
      <pingback:server>http://dotnetwhatnot.pixata.co.uk/pingback.aspx</pingback:server>
      <pingback:target>http://dotnetwhatnot.pixata.co.uk/PermaLink,guid,1bd63dbc-5a99-4f7a-9899-9e82ab16316a.aspx</pingback:target>
      <dc:creator>AY Silver</dc:creator>
      <wfw:comment>http://dotnetwhatnot.pixata.co.uk/CommentView,guid,1bd63dbc-5a99-4f7a-9899-9e82ab16316a.aspx</wfw:comment>
      <wfw:commentRss>http://dotnetwhatnot.pixata.co.uk/SyndicationService.asmx/GetEntryCommentsRss?guid=1bd63dbc-5a99-4f7a-9899-9e82ab16316a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I was having some trouble with WPF data binding yesterday, where the binding looked
correct, but the data wasn't being shown. It turned out that I had forgotten an .Include()
on the original query (this data is being sent across the wire through a WCF service,
so I can't use lazy loading), but along the way, I discovered a really useful blog
post on <a href="http://www.wpftutorial.net/DebugDataBinding.html" rel="nofollow" target="_blank">how
to debug WPF data binding</a>.
</p>
        <p>
Edit: After posting this entry, I noticed that one of the comments in the above blog
post mentioned <a href="http://www.beacosta.com/blog/?p=52" rel="nofollow" target="_blank">an
older blog post that discussed the same subject</a>. That adds a couple more ideas
to the pot, so is worth reading as well.
</p>
        <img width="0" height="0" src="http://dotnetwhatnot.pixata.co.uk/aggbug.ashx?id=1bd63dbc-5a99-4f7a-9899-9e82ab16316a" />
      </body>
      <title>Debugging WPF data binding</title>
      <guid isPermaLink="false">http://dotnetwhatnot.pixata.co.uk/PermaLink,guid,1bd63dbc-5a99-4f7a-9899-9e82ab16316a.aspx</guid>
      <link>http://dotnetwhatnot.pixata.co.uk/2011/02/22/DebuggingWPFDataBinding.aspx</link>
      <pubDate>Tue, 22 Feb 2011 14:06:13 GMT</pubDate>
      <description>&lt;p&gt;
I was having some trouble with WPF data binding yesterday, where the binding looked
correct, but the data wasn't being shown. It turned out that I had forgotten an .Include()
on the original query (this data is being sent across the wire through a WCF service,
so I can't use lazy loading), but along the way, I discovered a really useful blog
post on &lt;a href="http://www.wpftutorial.net/DebugDataBinding.html" rel="nofollow" target="_blank"&gt;how
to debug WPF data binding&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Edit: After posting this entry, I noticed that one of the comments in the above blog
post mentioned &lt;a href="http://www.beacosta.com/blog/?p=52" rel="nofollow" target="_blank"&gt;an
older blog post that discussed the same subject&lt;/a&gt;. That adds a couple more ideas
to the pot, so is worth reading as well.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://dotnetwhatnot.pixata.co.uk/aggbug.ashx?id=1bd63dbc-5a99-4f7a-9899-9e82ab16316a" /&gt;</description>
      <comments>http://dotnetwhatnot.pixata.co.uk/CommentView,guid,1bd63dbc-5a99-4f7a-9899-9e82ab16316a.aspx</comments>
      <category>Entity model framework</category>
      <category>WCF</category>
      <category>WPF</category>
    </item>
    <item>
      <trackback:ping>http://dotnetwhatnot.pixata.co.uk/Trackback.aspx?guid=a59123a7-72a4-426d-9284-8165e930e8b8</trackback:ping>
      <pingback:server>http://dotnetwhatnot.pixata.co.uk/pingback.aspx</pingback:server>
      <pingback:target>http://dotnetwhatnot.pixata.co.uk/PermaLink,guid,a59123a7-72a4-426d-9284-8165e930e8b8.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://dotnetwhatnot.pixata.co.uk/CommentView,guid,a59123a7-72a4-426d-9284-8165e930e8b8.aspx</wfw:comment>
      <wfw:commentRss>http://dotnetwhatnot.pixata.co.uk/SyndicationService.asmx/GetEntryCommentsRss?guid=a59123a7-72a4-426d-9284-8165e930e8b8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Well, after realising (or even RIAlising if you like puns) that RIA services weren't
cut out for non-Silverlight clients, I went searching for The Next Big Thing. Can't
remember if I mentioned it, so I'll bore you by repeating it, but all of this investigation
is for a big project I'm in that will need some sort of central data access point
that can be used by all sorts of clients, most likely being WinForms, WPF (one of
my colleagues is really excited about WPF), ASP.NET and Silverlight. Ideally, we want
all of these to be able to use the same point of access for data, with the business
logic behind that point of access.
</p>
        <p>
RIA looked like the answer, but doesn't seem to play ball with anything other than
Silverlight. So, I went looking at its big brother WCF. The idea was to build an end-to-end
solution that had a WCF service on top of an entity framework model, and various clients
consuming the service.
</p>
        <p>
All went well at first (doesn't it always!). Building the service was surprisingly
easy, as was consuming it in a WinForms and/or ASP.NET client. I tried the default
configuration, which sends out objects that inherit from EntityObject, then moved
on to adding a T4 template to generate POCOs (Plain Old CLR Objects). This has a big
benefit over the first approach in that the objects sent out don't depend on the entity
framework, and are more lightweight. The downside is that you lose the built-in change
tracking, which is a shame.
</p>
        <p>
With a big fanfare (well, not really, but you get the idea) I moved on to another
T4 template, this time to produce STEs (Self Tracking Entities), which don't depend
on the entity framework, but have change tracking built in. Now because WCF is a data
service, the objects that get sent over the wire consist of the data, and not the
methods. For this reason, you need to move the STE classes into a separate class library,
so you can reference it on the client.
</p>
        <p>
By the way, if this is all new or Chinese to you, I highly recommend <a title="Click here to see this book on Amazon UK" href="http://www.amazon.co.uk/Programming-Entity-Framework-Julia-Lerman/dp/0596807260/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1288104222&amp;sr=1-1" rel="nofollow" target="_blank">Programming
Entity Framework</a>, as well as the <a href="http://msdn.microsoft.com/en-gb/data/ee712907.aspx" rel="nofollow" target="_blank">MSDN
entity framework videos</a>, as these cover this stuff in detail.
</p>
        <p>
Anyway, once I had the class library, writing a WinForms or ASP.NET client was pretty
easy. I added a service reference to the WCF service, and away I went. Boosted by
this success, I concluded that I had finally found the right combination of technologies.
Ha, little did I know!
</p>
        <p>
Where it all went wrong was when I tried to consume the same service from Silverlight.
The first problem I encountered was that I couldn't add a reference to the STE class
library in the Silverlight project, as you can only add references to other Silverlight
projects, not plain old .NET ones (which, for the want of a better abbreviation, I'm
going to call PODN - Plain Old Dot Net). Apparently this is more of a design issue
than a technical one as, despite the claims to the contrary, Silverlight isn't really
built on a separate framework from .NET, it's actually a byte-compatible one. With <a href="http://www.netfxharmonics.com/2008/12/Reusing-NET-Assemblies-in-Silverlight" rel="nofollow" target="_blank">a
little bit of fiddling</a>, you can reference a non-Silverlight assembly in an Silverlight
project. Whilst this works, I didn't really like the idea.
</p>
        <p>
So, I experimented. The obvious solution was to build two STE class libraries, one
in PODN and one in Silverlight. This failed miserably, I think due to the fact that
the WCF service was trying to handle two different sets of objects at the same time.
I got a <a title="Click here to see a definition of &quot;cornucopia&quot;" href="http://dictionary.reference.com/browse/cornucopia" rel="nofollow" target="_blank">cornucopia</a> of
exotic compiler errors that I never managed to fix, even after I removed the Silverlight
class library from the solution.
</p>
        <p>
After pouring out my tale of woes to <a title="Click here to see Yossi's blog" href="http://blog.josephbabad.co.uk/" rel="nofollow" target="_blank">Yossi</a>,
I gave up and went on to something else. He went off in search of other technologies,
and I thought that was the end of it. It wasn't.
</p>
        <p>
A rather excited Yossi called me yesterday to say he had spent far too much time on
this problem, and had found out how to do it. Basically, it comes down to a bit of
a fiddle, based on the way we think (probably totally inaccurately) how WCF works.
As I mentioned above, WCF is a data service. I had assumed that it had an intimate
knowledge of the classes it was sending out, and was therefore getting all mixed up
about whether it was sending out PODN STEs or Silverlight STEs. It seems it isn't
that clever, or at least isn't that worried...
</p>
        <p>
WCF is a data service (did I mention this?), and all it seems to care about is being
able to serialise the data at the server end, and the client being able to deserialise
it. It doesn't actually seem to worry if you have the exact same type at either end,
as long as the class members match enough to enable deserialisation. In other words,
as long as there is a Customer object at either end, and that object has a Name property
that is a string, WCF couldn't care if the class library is PODN, Silverlight or (possibly)
any other technology you like.
</p>
        <p>
So, all you need to do is physically copy the generated STE class files from the project
that has the T4 template into a Silverlight class library, and reference that on your
Silverlight client. Your WCF service doesn't know anything about this class library,
as you copied the files without linking them. It thinks it's sending out PODN STEs,
which it is. The client thinks it's getting PODN STEs, which in the case of WinForms
and ASP.NET it is. However the clever bit is that you can generate a Silverlight class
library, and then a Silverlight client will think it's getting Silverlight STEs. As
the data side of the classes is the same in both cases, they can be serialised and
deserialised without problems.
</p>
        <p>
Yossi managed to build a solution with a Silverlight client and a WPF client using
the same WCF service, even though they were referencing different class libraries.
</p>
        <p>
The only downside to this is that if/when the model changes, you need to remember
to copy the regenerated class files into the Silverlight class library and rebuild
it. You don't get the automatic regeneration that you would have by linking the T4
template. However, until Microsoft come up with a better solution, it will do.
</p>
        <p>
So, until we hit the next hurdle, we are back on track with WCF on the server and
anything we like for the client.
</p>
        <img width="0" height="0" src="http://dotnetwhatnot.pixata.co.uk/aggbug.ashx?id=a59123a7-72a4-426d-9284-8165e930e8b8" />
      </body>
      <title>Using WCF services with a variety of clients, including WinForms, ASP.NET, WPF and Silverlight</title>
      <guid isPermaLink="false">http://dotnetwhatnot.pixata.co.uk/PermaLink,guid,a59123a7-72a4-426d-9284-8165e930e8b8.aspx</guid>
      <link>http://dotnetwhatnot.pixata.co.uk/2010/10/26/UsingWCFServicesWithAVarietyOfClientsIncludingWinFormsASPNETWPFAndSilverlight.aspx</link>
      <pubDate>Tue, 26 Oct 2010 14:14:16 GMT</pubDate>
      <description>&lt;p&gt;
Well, after realising (or even RIAlising if you like puns) that RIA services weren't
cut out for non-Silverlight clients, I went searching for The Next Big Thing. Can't
remember if I mentioned it, so I'll bore you by repeating it, but all of this investigation
is for a big project I'm in that will need some sort of central data access point
that can be used by all sorts of clients, most likely being WinForms, WPF (one of
my colleagues is really excited about WPF), ASP.NET and Silverlight. Ideally, we want
all of these to be able to use the same point of access for data, with the business
logic behind that point of access.
&lt;/p&gt;
&lt;p&gt;
RIA looked like the answer, but doesn't seem to play ball with anything other than
Silverlight. So, I went looking at its big brother WCF. The idea was to build an end-to-end
solution that had a WCF service on top of an entity framework model, and various clients
consuming the service.
&lt;/p&gt;
&lt;p&gt;
All went well at first (doesn't it always!). Building the service was surprisingly
easy, as was consuming it in a WinForms and/or ASP.NET client. I tried the default
configuration, which sends out objects that inherit from EntityObject, then moved
on to adding a T4 template to generate POCOs (Plain Old CLR Objects). This has a big
benefit over the first approach in that the objects sent out don't depend on the entity
framework, and are more lightweight. The downside is that you lose the built-in change
tracking, which is a shame.
&lt;/p&gt;
&lt;p&gt;
With a big fanfare (well, not really, but you get the idea) I moved on to another
T4 template, this time to produce STEs (Self Tracking Entities), which don't depend
on the entity framework, but have change tracking built in. Now because WCF is a data
service, the objects that get sent over the wire consist of the data, and not the
methods. For this reason, you need to move the STE classes into a separate class library,
so you can reference it on the client.
&lt;/p&gt;
&lt;p&gt;
By the way, if this is all new or Chinese to you, I highly recommend &lt;a title="Click here to see this book on Amazon UK" href="http://www.amazon.co.uk/Programming-Entity-Framework-Julia-Lerman/dp/0596807260/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1288104222&amp;amp;sr=1-1" rel="nofollow" target="_blank"&gt;Programming
Entity Framework&lt;/a&gt;, as well as the &lt;a href="http://msdn.microsoft.com/en-gb/data/ee712907.aspx" rel="nofollow" target="_blank"&gt;MSDN
entity framework videos&lt;/a&gt;, as these cover this stuff in detail.
&lt;/p&gt;
&lt;p&gt;
Anyway, once I had the class library, writing a WinForms or ASP.NET client was pretty
easy. I added a service reference to the WCF service, and away I went. Boosted by
this success, I concluded that I had finally found the right combination of technologies.
Ha, little did I know!
&lt;/p&gt;
&lt;p&gt;
Where it all went wrong was when I tried to consume the same service from Silverlight.
The first problem I encountered was that I couldn't add a reference to the STE class
library in the Silverlight project, as you can only add references to other Silverlight
projects, not plain old .NET ones (which, for the want of a better abbreviation, I'm
going to call PODN - Plain Old Dot Net). Apparently this is more of a design issue
than a technical one as, despite the claims to the contrary, Silverlight isn't really
built on a separate framework from .NET, it's actually a byte-compatible one. With &lt;a href="http://www.netfxharmonics.com/2008/12/Reusing-NET-Assemblies-in-Silverlight" rel="nofollow" target="_blank"&gt;a
little bit of fiddling&lt;/a&gt;, you can reference a non-Silverlight assembly in an Silverlight
project. Whilst this works, I didn't really like the idea.
&lt;/p&gt;
&lt;p&gt;
So, I experimented. The obvious solution was to build two STE class libraries, one
in PODN and one in Silverlight. This failed miserably, I think due to the fact that
the WCF service was trying to handle two different sets of objects at the same time.
I got a &lt;a title="Click here to see a definition of &amp;quot;cornucopia&amp;quot;" href="http://dictionary.reference.com/browse/cornucopia" rel="nofollow" target="_blank"&gt;cornucopia&lt;/a&gt; of
exotic compiler errors that I never managed to fix, even after I removed the Silverlight
class library from the solution.
&lt;/p&gt;
&lt;p&gt;
After pouring out my tale of woes to &lt;a title="Click here to see Yossi's blog" href="http://blog.josephbabad.co.uk/" rel="nofollow" target="_blank"&gt;Yossi&lt;/a&gt;,
I gave up and went on to something else. He went off in search of other technologies,
and I thought that was the end of it. It wasn't.
&lt;/p&gt;
&lt;p&gt;
A rather excited Yossi called me yesterday to say he had spent far too much time on
this problem, and had found out how to do it. Basically, it comes down to a bit of
a fiddle, based on the way we think (probably totally inaccurately) how WCF works.
As I mentioned above, WCF is a data service. I had assumed that it had an intimate
knowledge of the classes it was sending out, and was therefore getting all mixed up
about whether it was sending out PODN STEs or Silverlight STEs. It seems it isn't
that clever, or at least isn't that worried...
&lt;/p&gt;
&lt;p&gt;
WCF is a data service (did I mention this?), and all it seems to care about is being
able to serialise the data at the server end, and the client being able to deserialise
it. It doesn't actually seem to worry if you have the exact same type at either end,
as long as the class members match enough to enable deserialisation. In other words,
as long as there is a Customer object at either end, and that object has a Name property
that is a string, WCF couldn't care if the class library is PODN, Silverlight or (possibly)
any other technology you like.
&lt;/p&gt;
&lt;p&gt;
So, all you need to do is physically copy the generated STE class files from the project
that has the T4 template into a Silverlight class library, and reference that on your
Silverlight client. Your WCF service doesn't know anything about this class library,
as you copied the files without linking them. It thinks it's sending out PODN STEs,
which it is. The client thinks it's getting PODN STEs, which in the case of WinForms
and ASP.NET it is. However the clever bit is that you can generate a Silverlight class
library, and then a Silverlight client will think it's getting Silverlight STEs. As
the data side of the classes is the same in both cases, they can be serialised and
deserialised without problems.
&lt;/p&gt;
&lt;p&gt;
Yossi managed to build a solution with a Silverlight client and a WPF client using
the same WCF service, even though they were referencing different class libraries.
&lt;/p&gt;
&lt;p&gt;
The only downside to this is that if/when the model changes, you need to remember
to copy the regenerated class files into the Silverlight class library and rebuild
it. You don't get the automatic regeneration that you would have by linking the T4
template. However, until Microsoft come up with a better solution, it will do.
&lt;/p&gt;
&lt;p&gt;
So, until we hit the next hurdle, we are back on track with WCF on the server and
anything we like for the client.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://dotnetwhatnot.pixata.co.uk/aggbug.ashx?id=a59123a7-72a4-426d-9284-8165e930e8b8" /&gt;</description>
      <comments>http://dotnetwhatnot.pixata.co.uk/CommentView,guid,a59123a7-72a4-426d-9284-8165e930e8b8.aspx</comments>
      <category>Entity model framework</category>
      <category>WCF</category>
    </item>
  </channel>
</rss>