The power of separation

I am getting ready to deploy my 1st Windows Phone 7 app. The application consumes some data that is served up by a WCF service that I wrote. Until today, I had been testing the application using some dummy data that I hard-coded into the UI.

To add the WCF service, I added a new WCF project to my solution (and a supporting data project that uses Entity Frameworks). I ran into the same problem as I encountered last year that I forgot about – when you add a reference to a project that uses Entity Frameworks, you need to add a reference to System.Data.Entities in the consuming project. If not, you can see the business classes but the context will not work. Unfortunately, the only way to see this error it to hard-code the context into your consuming project, hit F5, and get this message:

'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Once I figured that out, I whipped up a basic method using some LINQ like so:

public Location GetLocation(int locationId) { var q = from l in context.RoadAlert_Location where l.LocationId == locationId select l; return MapLocation(q.FirstOrDefault()); }

Easy enough, I then added my interface:

[ServiceContract(Namespace="")] [ServiceKnownType(typeof(Location))] [ServiceKnownType(typeof(List<Location>))] public interface IRoadAlert { [OperationContract] Location GetLocation(int locationId); [OperationContract] List<Location> GetLocations(int alertTypeId); }

And then published the service to my WinHost server. It published fine and I got the WSDL no problem. However, after I added a reference, I could not create a proxy class. I checked the clientConfig and I was surprised because it was empty:


I did a quick search and found posts like this one. I made sure I was using BasicHTTPBinding like so:


However, it still was not working. I would see the service reference and the wizard exited with no errors. However, when I tried to create the proxy, I got an exception.

I then decided to take a step back and separate the service and data projects from my solution – instead of deplying to WinHost. I started the service project and then tried to add a reference from the client. Once I did that, I got this in my client config:

<configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IRoadAlert" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <security mode="None" /> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:52296/RoadAlert.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRoadAlert" contract="Tff.RoadAlert.Services.IRoadAlert" name="BasicHttpBinding_IRoadAlert" /> </client> </system.serviceModel> </configuration>

Magic! I have no idea why/how it now works, but separating the projects did the trick. I then could create my proxy locally. Then to deploy, I just published the service and changed the endpoint address on the client. I found this Microsoft page that details many of the problems and steps I went through.

Another interesting note is that you can’t do a synchronous call in Win Phones – I Guess they all have to be asynch…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: