• Latest Posts

  • Latest Comments

  • Archives

Some Silverlight 4, WCF, and EF Rookie Mistakes

Today I am just making some notes to myself about mistakes I’ve made in the past 24 hours with Silverlight. I had things so twisted that I started again from scratch. The initial issue (that I’ve not yet completely solved) was that of using an Enum in a class that is used in a WCF (Windows Communication Foundation) service. I then seemed to have all sorts of issues. Here is a list of some of the things I though were wrong – largely based on various web sites – but that did not seem to be an issue in the end. This is primarily due the issues being addressed in Silverlight 4.

  • Must use basicHttpBinding for Silverlight – false
  • Cannot use ObservableCollection for your collection type in Silverlight – false

Before I finish my notes, here is an architectural diagram using a Layer Diagram in Visual Studio 2010 Ultimate:


Lesson 1 – Get references in the right place

Having only worked for hours with Silverlight in earnest, I’m making very rookie mistakes.

“The type ‘DataGrid’ was not found.” – Sure, I had a reference to System.Windows.Controls.Data in my MVVM (Model-View-ViewModel) project (WcfLearn.Controls.Silverlight), but I didn’t have it in my actual application (WcfLearn.Silverlight). This did not break until runtime. Now that I have the reference in both, it works fine.

Does anybody at Microsoft use Silverlight in a TFS Environment?

Everything was working fine, the – nothing worked. I backtracked for quite some time. Here’s what happened. I added the solution to source control – I use TFS (Team Foudation Server). When I did that, it excluded the ClientBin directory as well as WcfLearn.SilverlightTestPage.aspx and WcfLearn.SilverlightTestPage.html from my ASP.NET Web Application! It didn’t delete them, it just excluded them! Added them back and I was back on track… Almost. As a result of that, it turned off the linkage between the Silverlight Application and the hosting ASP.NET Web Application. I had to go back into the properties of the web application (WcfLearn.Silverlight.Web) and add the WcfLearn.Silverlight application back…


That places the WcfLearn.Silverlight.xap file in the ClientBin. That’s how Silverlight is “referenced” by a web application… I don’t actually specify any references to the Silverlight application. It lives in it’s own little world with all the magic being done by the WcfLearn.SilverlightTestPage.aspx.

WCF Service Configuration in wrong place

When you use a Class Library for your MVVM Views – Copy the ServiceReferences.ClientConfig from the Class Library to the Silverlight Application!

customBinding Seemed to Work Fine?

Here is the service configuration that was generated for me (that I then had to move to the Silverlight application – see above). It seems to be working fine without basicHttpBinding. Many posts state that you cannot use wsHttpBinding or anything other than basic. I believe that applies to earlier versions of Silverlight.

  1. <configuration>
  2.     <system.serviceModel>
  3.         <bindings>
  4.             <customBinding>
  5.                 <binding name="CustomBinding_LibraryService">
  6.                     <binaryMessageEncoding />
  7.                     <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
  8.                 </binding>
  9.             </customBinding>
  10.         </bindings>
  11.         <client>
  12.             <endpoint address="http://localhost:3520/LibraryService.svc"
  13.                 binding="customBinding" bindingConfiguration="CustomBinding_LibraryService"
  14.                 contract="LibraryServiceReference.LibraryService" name="CustomBinding_LibraryService" />
  15.         </client>
  16.     </system.serviceModel>
  17. </configuration>

You can see that this will allow me to use binary message encoding that I presume will speed up my application.


I kept reading that I could not use an ObservableCollection with Silverlight and WCF Services, but here’s a screenshot from the Service Reference Settings when I right click on the Service Reference (LibraryServiceReference in my case) and select Configure Service Reference:


It seems to work fine for me today.

Whew… What a day. I have a lot to learn.

As a note – I did not do the Entity Framework (EF) coding today. I already had that sitting on the shelf and just wanted to put together the full path – from a Database, to a WCF Service, through the service, into the Silverlight ViewModel, and finally onto the page with Binding. Here is a depiction of that flow with a series of code images – starting from the database, and making the way to the page:

In LibraryRepository.cs:



Which is called by the LibraryService.svc.cs in the ASP.NET Web Application:


Note: The Entity Framework Connection String must be in the Web.config file for the web application.

That service is then consumed by the Silverlight Class Library that has the MVVM components in the BookListViewModel:


The books finally make their way to the View via DataBinding:


Then finally, the very crude page looks like this:


I say crude, because I only have one column… I don’t have a busy indicator while I wait for the books to come back (I have several hundred in the database)… I don’t have any pagination… you get the idea… But it’s a start!

Leave a Comment