A few posts back I started talking about what it would take to create a new application
for the new Windows Phone 7. I’m not a fan of learning from trivial applications
that don’t touch on the same technologies that I would be using in the real world,
so I thought I would build a real application that someone can use.
Since this application uses a well known dataset I kind of get lucky because I already
have my database schema, which is in a reasonably well designed way. My first
step is to get it to the Phone, so I will use WCF Data Services and an Entity Model.
I created the model and just imported the necessary tables. I called this model
RaceInfoModel.edmx. The entities name is RaceInfoEntities This is ridiculously
simple to do.
The following step is to expose the model to the outside world through an XML format
in a Data Service. I created a WCF Data Service and made a few config changes:
using System.Data.Services;
using System.Data.Services.Common;
using System;
namespace RaceInfoDataService
{
public class RaceInfo : DataService
{ public static void InitializeService(DataServiceConfiguration config) { if (config
== null) throw new ArgumentNullException("config"); config.UseVerboseErrors
= true; config.SetEntitySetAccessRule("*", EntitySetRights.AllRead); //config.SetEntitySetPageSize("*",
25); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
} } }
This too is reasonably simple. Since it’s a web service, I can hit it from a
web browser and I get a list of available datasets:
This isn’t a complete list of available items, just a subset.
At this point I can package everything up and stick it on a web server. It could
technically be ready for production if you were satisfied with not having any Access
Control’s on reading the data. In this case, lets say for arguments sake that
I was able to convince the powers that be that everyone should be able to access it.
There isn’t anything confidential in the data, and we provide the data in other services
anyway, so all is well. Actually, that’s kind of how I would prefer it anyway. Give
me Data or Give me Death!
Now we create the Phone project. You need to install the latest build of the
dev tools, and you can get that here http://developer.windowsphone.com/windows-phone-7/.
Install it. Then create the project. You should see:
The next step is to make the Phone application actually able to use the data.
Here it gets tricky. Or really, here it gets stupid. (It better he fixed
by RTM or else *shakes fist*)
For some reason, the Visual Studio 2010 Phone 7 project type doesn’t allow you to
automatically import services. You have to generate the service class manually.
It’s not that big a deal since my service won’t be changing all that much, but nevertheless
it’s still a pain to regenerate it manually every time a change comes down the pipeline.
To generate the necessary class run this at a command prompt:
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
DataSvcutil.exe
/uri:http://localhost:60141/RaceInfo.svc/
/DataServiceCollection
/Version:2.0
/out:"PATH.TO.PROJECT\RaceInfoService.cs"
(Formatted to fit my site layout)
Include that file in the project and compile.
UPDATE: My bad, I had already installed the reference, so this won’t
compile for most people. The Windows Phone 7 runtime doesn’t have the System.Data
namespace available that we need. Therefore we need to install them… They
are still in development, so here is the CTP build http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=b251b247-70ca-4887-bab6-dccdec192f8d.
You should now have a compile-able project with service references that looks something
like:
We have just connected our phone application to our database! All told, it took
me 10 minutes to do this. Next up we start playing with the data.
This past weekend I gave a talk on “Blendability”; The ability to maintain and leverage blend design time compatibility with your WPF and Silverlight projects. Thanks to everybody who came out to the talk, we had some good discussions despite the oppressive heat in the room. You’ll find my slides above. As some of you have requested, here you can find my demos including the MVVM template that was used in the Blend 4 previews that demonstrates the behavior technique for calling methods on your view models.
I just finished my two presentations at this year’s Toronto Code Camp. As is my standard, I have put my slides on Slideshare.net. They can be found at:
OData - http://bit.ly/akPCcH
Silverlight 4 Out of Browser - http://bit.ly/91xQwp
As well, the sample code for the Silverlight 4 talk can be downloaded here.
This is the demo code for my Silverlight 4 Out of Browser talk at the Toronto Code Camp
The dreaded “The remote server returned an error: NotFound” error message in RIA Services can be frustrating if you are not aware of what it really means, and more importantly how to diagnose it.
This generic error message is due to network stack limitation in your browser, basically the browser is intercepting the error and not giving it all to Silverlight. That does not mean there isn’t good information to be found. Your friend here is Fiddler and/or WCF Service Tracing.
NotFound vs OnError: A NotFound generally means that something in the WCF side blew up, OnError will handle errors in your model (or custom code in your DomainService). So when you see NotFound, overriding OnError will not be much help, break points placed inside of it will most likely not be reached during debugging, but its an easy place to start.
The first tool we are going to us is Fiddler, a web debugging proxy with a WCF Binary Inspector. This will show you all the requests are being made and will show you what error code, and possibly what error is being returned by your service. (Using Fiddler with ASP.Net Development Server / Casini)
If there is no good information in Fiddler then the next step is to hit your service url, which you can find in Fiddler. In the picture above you can see that my service is at /ClientBin/HockeyStatsService-Web-HockeyStatsDomainService.svc (ProjectName-Web-DomainServiceName.svc). Remember that RIA Services does not actually generate a .svc file for you, it is handled dynamically by the DomainServiceHttpModule, so don’t go looking for a physical file.
Actual 404: If you hit your service in the browser and you still get a 404, then something is wrong with your deployment.
Check out Saurabh Pant’s Blog or Tim Heuer for good articles on deploying WCF Ria Services to IIS.
If you are using Casini (ASP.NET development server) then make sure your web.config is correct (Has proper references to DomainServiceHttpModule), and that you have a reference in your ASP.NET project to your service library (if you are using RIA Class Libraries, otherwise you should be fine on this point).
Now if you can hit your service url without a 404, but still get a NotFound, and the Fiddler response tab didn’t give you anymore insight we will move on to some other steps.
SvcTraceViewer: Visual Studio has a tool to help you configure WCF, it can be found under TOOLS | WCF Service Configuration Editor. Use this tool to open your Web.Config and then on the ‘Diagnostics’ folder, turn on Message Logging and Tracing. On the ‘Message Logging’ node, LogMessagesAtServiceLevel=true and LogMessagesAtTransportLevel=false.
You can view the generated svclog files with C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\SvcTraceViewer.exe
In my example DomainService I am loading a large amount of hockey stats data. When I open my svclog file and find the error generated I can see that the actual exception being thrown by my service says:
'Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. '.
Your message obviously might be different, but with that information you should be able to find a solution or at least provide the right information to the WCF RIA Services Forums so they can help find a solution
Another good debugging tip is to turn off custom errors in your Web.Config. <customErrors mode="Off"/> This will provide more informative exceptions to silverlight, however it also exposes information to the user that you may not intend, so do not leave this set to ‘off’ on a publicly available site.
If your error is the same as mine, take a look at Fixing the MaxItemsInObjectGraph.
The MaxItemsInObjectGraph error message comes up fairly often in the WCF RIA Services forums and I just ran into it on a personal project so I thought I’d write a quick blog on how to fix this error.
The exact error message from WCF Trace is:
'Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. '.
You need to add the following code to your web.config, and replace the following:
HockeyStatsService-Web-HockeyStatsDomainService with YourRiaProjectName-Web-YourDomainServiceName
HockeyStatsService.Web.HockeyStatsDomainService with YourRiaProjectName.Web.YourDomainServiceName
<services>
<service name="HockeyStatsService.Web.HockeyStatsDomainService"
behaviorConfiguration="HockeyStatsService-Web-HockeyStatsDomainService">
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="HockeyStatsService-Web-HockeyStatsDomainService">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="655360"/>
</behavior>
</serviceBehaviors>
</behaviors>
Let me officially call this the Grey Screen of Death. You create a lovely sketch flow prototype in Expression Blend and it looks great at design time, but at run-time you get nothing. If you’re clever, you’ll try to dig into the java script error, which fortunately, the nice folks on the SketchFlow player team have raised up from the Silverlight layer. Unfortunately, I’ve found that most errors you get in here are pretty useless.
In my case I was recently getting:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8; .NET4.0C; .NET4.0E)
Timestamp: Wed, 31 Mar 2010 03:58:03 UTC
Message: Unhandled Error in Silverlight Application AG_E_PARSER_BAD_TYPE [Line: 5 Position: 78] at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at SilverlightPrototype_10Screens.Screen_1.InitializeComponent()
at SilverlightPrototype_10Screens.Screen_1..ctor()
Line: 1
Char: 1
Code: 0
Your mileage might vary by attaching the visual studio debugger or running the application from within Visual Studio. In my case, I ended up doing some process of elimination pruning to see if I could back track and find the problem. I was having difficulty with some sample data in this case. It was tricky to find, but I’ve determined that you really don’t want a space in your SketchFlow project names. This will cause some erroneous code gen for the loading of the sample data in the sample data classes.
I absolutely love Expression Blend and SketchFlow, but finding this bug was one of those times when I’ve wanted to physically hurt my computer. I hope this helps some poor soul find the root cause a little faster than it took me.
And please share in the comments any other common reasons you’ve found SketchFlow Player failures.
Once of the interesting elements of this year’s MIX is the complete domination of Twitter as a medium for distributing updates. If you have been following me on Twitter (I’m @LACanuck), then you will already have heard a lot about the Windows Phone 7 development announcements. However, as useful as Twitter is, it’s not really a place for opinion. Unless your opinions fit into <140 characters. Mine don’t
Windows Phone
There is no question that there is a lot of buzz around developing apps for the Windows Phone 7. This is completely understandable, as WP7 allows Silverlight developers the ability to create applications for the phone. According to Scott Gu’s keynote, there is only “one Silverlight”. That is to say that applications that run on the browser should also be able to run on WP7.
Now there is going to be a little bit of a reality check for that statement, especially as we hit Silverlight 4. I’m not sure, for example, if Silverlight as running on WP7 has the concept of a trusted application. I suspect that it doesn’t, although I’m open to correction if my assumption is misplaced.
But working solely within the security sandbox is not the only real difference. Specifically, the design of a WP7 application is very different than a Web application. The size of the design surface is, naturally, much smaller on the WP7. And the UI needs to consider that main UI gesture is touching, a paradigm that doesn’t apply to Web applications. All of this is to say that while, theoretically, the same application could run on both platforms, it’s much more likely that different views will be used by the different targets. If nothing else screams that you should be using MVVM as a design pattern for Silverlight, this will.
Deployment
Once you see what’s possible in the WP7 environment, the excitement regarding creating applications is easy to understand. And not only are the apps exciting, so too is the ability to monetize your application. Microsoft will be making a Marketplace available so that you can sell your apps on-line. Given how well Microsoft has done with community driven marketplaces, I have no doubt this will be successful.
But what about your own personal applications? What if you want to develop a WP7 application that is used by your mobile sales force? At the moment, the answer seems to be that you’re out of luck. This might change before it goes live, but the word that I’m hearing is that the only way to get apps onto your phone is through the Marketplace.
Now, that’s not completely accurate. If you have Visual Studio 2010, you can deploy your application to a physically connected phone. However, the time to live for applications which have been deployed in such a matter is limited, To, approximately, a month. After which the app would need to be redeployed.
I’m not a fan of this. In fact, in my mind ,it drops the collection of Silverlight developers who might write WP7 apps by 50%. At least. I can take guesses at the reason why this limitation is the case, but still, it’s not what I was hoping for. The term for what I’m looking for is ‘siloed’ deployment’ (that is, deployment only for people in a particular silo) and I’m hoping that it becomes part of the released version of WP7 before it goes live with the first version.
While there is more of interest that is being revealed here, this is probably a decent start. And I’ll be both blogging and tweeting as much as I can while I’m here at MIX ‘10

Expression Blend (3 and higher) lets you create Sample Data Sources, and for each property, select a data type and sample data format, such as Lorem ipsum, Colors, Company Names, Names, etc. It’s a nice list, which is great for prototyping. However, if you want to add to this list of types, or just edit the potential values for each type, you can do edit a simple csv file.
In your Blend program directory, you will find a “SampleDataResources” folder. Inside this you will find an Images folder (in case you don’t set your own folder when creating a property) and a Data folder that stores the sample string data.
C:\Program Files (x86)\Microsoft Expression\Blend 3\SampleDataResources\en\Data
You can edit this file in excel and simply edit the values or even add new columns.

Windows 7 or Vista doesn’t find it cool to be editing any file underneath your Program Files folder, so you’ll either need to copy the file out to yoru documents folder and edit it there, and copy it back, or run excel with elevated admin privileges.
I learned a very important lesson about binding in Silverlight that I thought I would share.
Here is what I was trying to do:
<TextBlock Text=”{Binding Path=MyPeople, Converter={StaticResource OldestPersonNameConverter}}” />
MyPeople is a collection of People and the converter is going to take that collection, find the oldest person and return their name. I thought this would just work and it does the first time. However in this case, adding a new person to MyPeople will NOT trigger the binding to update, your converter will never run again.
Right? Wrong? I’ve been debating the point with a few people at Objectsharp, and I didn’t expect this to fail (some did). I was under the assumption that binding was binding, the magic behind the covers would look for INotifyPropertyChanged or INotifyCollectionChanged regardless of the property you are trying to bind.
This doesn’t work for my example, but in my actual code I was able to bind to myCollection.Count which will fire a PropertyChanged event when you add or remove an item from the collection. In the actual example above, I don’t have a good answer, they all have drawbacks.