Latest Tweet:
  • Loading...

On the Silverlight.net forums there are several questions about how to configure Silverlight applications. A common question is how to change the URL of a WCF service you are consuming. Another common question is how pass in some user defined configuration settings. There are several interesting solutions, and a couple of days ago Bill Reiss posted an article about “Calling WCF on the server of origin from Silverlight”. Bills solution involves using the HTML Bridge to grab the current URI, and building up the URL of the web service, and then creating the service client using one of the constructors allowing you to manually specify binding and endpoint. This is all cool and really helpful in many cases, but the ServiceReferences.ClientConfig file is there for a reason. I think a lot of developers are a bit confused about how to use this file. And I don’t blame them. In Silverlight 2 Beta 1 this file was generated by Visual Studio, but was not read by the WCF runtime. In Beta 2 this was fixed, and the WCF runtime will now read and use the configuration file.

A lot of developers are afraid they have to rebuild their application to change web service configurations; something Bill also write in his blog post:

This is fine if you're hitting a service like Digg or one of the other public web service APIs out there, but not if you want to be able to test on your own machine and also deploy the same XAP to your server without rebuilding.

You could go ahead and change this to the URI on your server and rebuild, but then it won't work locally for testing unless you have a crossdomain file on your server, and you would be hitting your server's web service, not your local one while testing.

When building a Silverlight 2 application Visual Studio 2008 will spit out a XAP file in the Client Bin directory. The XAP file is just a standard ZIP archive containing some assemblies, the configuration file, a manifest file, and any resources you might have added to your project and market as content. It’s completely safe to rename it to ZIP, unpack it, make changes to the ServiceReference.ClientConfig file and package it all back together to a XAP file. Turns out repacking your XAP file is actually a good idea. Back in May Valeri Hristov discovered that he could chop 1MB of a 3MB large Telerik example application by repackaging it using the 7Zip application. Later David Anson followed up with some more examples showing a 22% average saving by repacking the XAP file… Why Microsoft is using such an inefficient ZIP implementation I don’t know –hopefully this will be fixed before release. Until then creating a post-build event that repackages your XAP will probably be the easiest 22% savings you will get all day.

Content of a XAP file

Anyway, let’s get back to configuration. Now that we can reconfigure our Silverlight application without rebuilding it we might be able to use the configuration file for more useful stuff. Silverlight does not come with a full configuration API like the full .NET framework. You do have the System.Configuration namespace, but it only contains some internal, abstract classes used by the Silverlight WCF stack. If you search for “configuration” in Reflector targeting Silverlight 2.0 you get some hits in the System.ServiceModel.Configuration namespace as well. It got configuration section handlers used by WCF to read information about your web service bindings and end points. All classes are internal and not really helpful for handling general application configuration.

Since the ServiceReference.ClientConfig is a plain XML file there is nothing stopping us from adding our own elements to the file. I decided to use the application setting convention we’re used to from .NET, and added an appSettings element with a bunch of key-value pairs elements. The application continued to run just fine with these additional elements. I also tried adding and removing service references from Visual Studio to see if it would screw up my changes. Thankfully Visual Studio only touched the serviceModel element, and didn’t care about our appSettings element. The ServiceReference.ClientConfig file now looks something like this:

configsample1

This is quite handy, as we can keep all application settings in one file using a format developers know. Next step is writing some code to read the configuration file. I decided to mimic how things work in the full .NET framework by creating a ConfigurationManager class with an AppSettings property. To parse the configuration file I used LINQ to XML which made it really easy to extract the key-value pairs.

configsample2

In the full framework the AppSettings property is of type NameValueCollection. This collection does not exist in Silverlight, so instead of re-implemented it I created a simple class backed by dictionary, exposed as an index property.

configsample3 

You read the application settings the same way you are used to from any .NET application:

configsample4

Hopefully this post gives you a better understanding how Silverlight 2 applications are packaged, and how to configure your application without rebuilding it. If I was consuming WCF services in my project I would add configuration management to my automated build process. It would be fairly simple to write a build script that repackages the XAP with a more efficient ZIP implementation, as well as building different versions for test, staging and deployment.

Back in may I posted about a way to get basic webcam support in Silverlight by integrating Flash. The post got some good feedback, and lately I’ve gotten a few requests to update the application for Silverlight 2 Beta 2. I have now done that, as well as adding a new sample application using the webcam support for something (somewhat) useful.

The new example application is called “Webcam Sliding Puzzle” and extends a picture puzzle game written by Cigdem Patlak and adds support for capturing pictures from your local webcam. The original game supported reading images from your local computer using the open file dialog in Silverlight, so adding support for webcam images was fairly straight forward. Click the screenshot to try out the game.

Webcam Sliding Puzzle

If you want to learn how to add webcam support in Silverlight I recommend checking out my older post “Webcam in Silverlight 2 - MacGyver style!” For more details on integrating Silverlight and Flash check out the article “Silverlight and Flash Interoperability using HTML Bridge and ExternalInterface API” I wrote for www.silverlightshow.net.

The iPhone showed that it is possible to squeeze the real web into the users’ pockets. But there are still limitations, the big one being support for third party plug-ins such as Flash, Silverlight and different media players.

An article on TechCrunch showing how to access the NBC Olympics Silverlight 2 player from a Nokia phone really caught my attention. Sure, Nokia have announced Silverlight Mobile support, but no details are available yet, and certainly not a public beta. It turns out Skyfire, the browser used to watch Silverlight 2 content on the mobile, takes a completely different approach to mobile browsing than the iPhone. On the iPhone you’re running the full Safari browser, doing all the heavy lifting on the device. The Skyfire browser uses remote rendering of the page. This is similar to the Operas successful mobile browser, which uses a proxy that downloads the page and recompresses images and re-arranges the HTML to a format better suited for mobile devices. The Skyfire takes this approach one step further and does all the rendering on the server and having the mobile device act as some sort of remote desktop client. Their FAQ is fairly cryptic about the details of how the rendering is done, and they obviously have done some smart things to make this fast enough to stream to a mobile device.

Skyfire Logo

Q: How is Skyfire able to render the PC Web?
A: Skyfire's proprietary technology enables us to support the real web both now and in the future as new web technologies are implemented. We have created a brand new architecture that's server assisted. It's an asymmetric distributed approach where the server does the heavy lifting so the client is high performing.

Q: Seriously, how does Skyfire do all of this?
A: The only way we can tell you this is if you join our team. We're hiring bright, passionate folks who want to completely change the way the Internet works on mobile phones.

Skyfire is currently in private beta, running on Nokia Series 60 phones and Windows Mobile 5 and 6. I’m using the iPhone, and haven’t been able to try the browser myself. But I strongly recommend checking out this video showing the NBC Olympics Silverlight player on a Nokia device.

Is this the future of mobile browsing? I don’t know – And I have no idea if this would be able to scale. I’m not sure if Skyfire is talking to their back-end server which does the rendering, or if Skyfire includes both a mobile client, and something you have to install on your local PC to get it to render your page, upload it to Skyfire servers, and then stream it down to your device... No matter which approach they take, the demos do look impressive.

The session catalog for Tech Ed Australia and Tech Ed New Zealand has been available online for a few weeks now, but I haven’t gotten around to blog about me speaking at TechEd. I will be speaking about “Silverlight 2 for Developers”, as well as sitting on a panel discussing “Web Futures - the next 18 months”. The panel will also be comprised of Scott Hanselman, Nigel Parker, and Trent Mankelow. In Sydney I have a 10 minute quest appearance in the Visual Studio track showing my favorite .NET 3.5 SP1 feature.

The abstract for my Silverlight talk goes like this:

Silverlight 2 enables you to build some stunning web applications. But it's not all about eye candy. Silverlight 2 brings the power of .NET to the browser. This requires some new thinking in the way we build web applications. In this session we will focus on Silverlight 2 from a developer's point of view. This session will show you how to write code that both you and your designers can work with, by separating out business- and data access logic and leveraging the power of data binding in Silverlight. The session will also cover development practices such as unit testing and continuous integration, as well as more technical topics such as network- and data access, local storage and more. You will also learn a few tips on how you as a developer can leverage Blend 2.5

As you can see by the description I don’t want the session to be yet another “introduction to Silverlight 2” presentation. I will cover concrete things such as how to build a data driven CRUD application in Silverlight, talking to web services and using features like data binding and the visual state manager. However, I also want to cover design patterns and development practices that can help you build more flexible, testable and designable (as in UI design) code.

CodeCampNZ Logo The day before Tech Ed the New Zealand development community is putting together Auckland Code Camp 2008. Since I’m in town I got invited to do a presentation, and I had such good time at CodeCampOZ earlier this year that I couldn’t say no to that invitation. Robert Fonseca-Ensor is doing a TDD and DI/IoC talk in the morning, so I will use part of my session to build on that and show how to use Ninject to do dependency injection in Silverlight 2. I will also cover some more advanced Silverlight 2 testing scenarios, such as writing asynchronous tests, before finishing up with some HTML Bridge fun.

Tech Ed New Zealand, Auckland (1– 3 September 2008)

WEB304 - Web Futures - the next 18 months (02/09/2008 10:45AM-12:00PM)
Where is the web going over the next 18 months and what is in the pipeline from Microsoft to cater for and drive this? Is Microsoft up to the challenge? How is Microsoft working with the part of the web that is not MS based? How is Microsoft changing to cater for emerging trends? Submit your questions and hear the answers from the experts.

WEB309 - Silverlight 2 for developers (03/09/2008 9:00AM-10:15AM)
See description above...

Tech Ed Australia, Sydney (2 – 5 September 2008)

WEB309 - Silverlight 2 for developers (04/09/2008 8:30AM-9:45AM )
See description above...

WEB304 - Web Futures - the next 18 months (05/09/2008 10:15AM-11:30AM)
See description above...

DEV210 - The Hour of Power: 6 of the Best - Best of the Best in VS2008, Fx 3.5 inc SP1 (05/09/2008 8:30AM-9:45AM)
Rock on up to the “Hour of Power” this will be a fun energy packed session presented by 6 speakers each speaking for 10 minutes on their favourite feature(s) in Visual Studio 2008 and Fx 3.5 including SP1. It’s a must see session and a great way to pick up a few new tips and tricks!!

Going to TechEd Australia or New Zealand? Or got any questions, suggestions or ideas to things you want me to cover? If so, it would be awesome if you drop me a comment.

When building line-of-business (Smart Clients or Rich Internet Applications) you normally talk to some back-end web service responsible for enforcing business rules, authentication, and storing your data in some database. When implementing your client application you often end up re-implementing some of these business rules to give the user a better experience. Instead of having to pass an Order-object to the web service just to get an exception saying that the order date field is a required, you should try to enforce these validation rules directly in your user interface. How do you best implement these rules across your tiers?

Sharing is caring!

In his January 2004 MSDN article title "A Guide to Developing and Running Connected Systems with Indigo" Don Box presented four rules for connected systems, later often referred to as “the four tenets of SOA”. The rules are as following:

  • Boundaries are explicit
  • Services are autonomous
  • Services share schema and contract, not class
  • Service compatibility is determined based on policy

Especially rule number three is relevant when talking about implementing business rules on both server and client. In traditional object oriented design you would implement your domain- or business objects and let them encapsulate your rules and logic. One of the advantages of web services is that they work across boundaries and platforms. They also help you make your systems loosely coupled, only depending on the service contract and not a specific binary component. Web services use open standards such as HTTP, SOAP and XML to achieve interoperability and louse coupling.

Services do not deal in types or classes per se; rather, only with machine readable and verifiable descriptions of the legal "ins and outs" the service supports. The emphasis on machine verifiability and validation is important given the inherently distributed nature of how a service-oriented application is developed and deployed. Unlike a traditional class library, a service must be exceedingly careful about validating the input data that arrives in each message. Basing the architecture on machine-validatible schema and contract gives both developers and infrastructure the hints they need to protect the integrity of an individual service as well as the overall application as a whole. – Don Box

Blindly following the rule about not sharing class can lead to developers and architects forgetting what they know about object oriented design. This is something I have experienced on several projects. However, there is nothing in Don Box reasoning stopping you from building some really smart classes or libraries to help you consume and talk to these web services. Most development platforms have some sort of tool support to automatically generating proxies to create the SOAP messages needed to communicate with the service. However, since these classes are all generated based on WSDL and XSD definitions they don’t contain any "smarts", like validation rules, authorization, or computed properties.

There are different techniques to achieve sharing of some logic between server and client. An old trick is to move all your business entities into a separate assembly and use this on both server and client. After Visual Studio have generated a web service proxy you remove any generated types and include a using statement to the namespace containing your business entities. When the proxy de-serializes an Order-object from your web service it will create a "real" Order-object instead of an auto generated type. This object will have any validation rules or computed properties, which would be lost in the auto generated type. In Visual Studio 2003 and 2005 you had to do this by hand, which was bothersome, and something you had to redo every time you update your service reference. In Visual Studio 2008 this pattern is supported by the tool. When adding a new service reference you can choose to reuse any types in referenced assemblies, letting you use real objects instead of automatically generated proxy objects.

When working with Silverlight this is a little bit harder. The problem is that the client Silverlight .NET runtime is different from the server .NET runtime. You can’t directly reference a Silverlight class library in your web service application, or reference a traditional .NET class library in your Silverlight application. There are workarounds to help you share code between a .NET server application and a Silverlight client application. You can create two different class libraries, one for Silverlight and one for the server, and link the class files between them. You have one copy of the file, but it’s added to two different projects, letting you compile the same class into two different assemblies. Since Silverlight is a subset from .NET you may not be able to do all the same things in the client library as in the server library. You can separate out Silverlight and .NET specific code using compiler directives or partial classes.

#If Silverlight then 
	// Silverlight only code 
#Else 
	// .NET only code #End If 

This is the approach taken by Rockford Lhotka in CSLA Light, a trimmed down version for Silverlight of his CSLA.NET application framework. He recently started blogging about how CSLA Light helps you reuse business objects between tiers. This approach might be difficult if your server code have many external dependencies, for instance to ORM tools or other frameworks you don’t have a Silverlight version for.

Extending the automatically generated types

The web service proxies generated by Visual Studio have gotten better and better, and the classes generated by Visual Studio 2008 are quite useful. Sure, they won’t know about server specific rules, but the generated objects use properties, implements change notification and use observable collections for lists. This enables you to data bind against the proxy objects, which is really handy. However the solution isn’t perfect. One of the features added in Silverlight 2 Beta 2 is the support for binding validation. This enables you to detect if the data entered by the user in a data bound field breaks any validation rules. When the binding validation event fires you can highlight the field, for instance by changing background color. The way binding validation works in Silverlight 2 Beta 2 is by exceptions thrown in the setter of a property. If you bind against the automatically generated proxy objects you can’t change the setter to implement your validation rules.

Another example where you want to extend the proxy objects with additional functionality is for computed properties. Since I just got back from a dive trip I’m going to use that as an example. When logging a dive you enter date, time in water, bottom time and time out of water. If I were to implement a dive log application I would store the time the diver entered the water and the bottom time in the database. The time out of water would be a computed property, computed adding the bottom time (minutes) to the time the entered the water time. If I did this on my server, the data would get passed to the client, but the automatically generated proxy object would see this as any other "dumb" property. It wouldn’t understand the relationship between time-in water, bottom time and time-out of water. If I change the bottom time, the time out of water property wouldn’t change.

To solve these problems you have two options. You can choose to treat your web service objects as simple data transfer objects, and wrap them in your own custom client side objects. This can be done by creating a new DiveLogClient-object that takes the DiveLog proxy object in the constructor, and creates new properties for any field used by the UI. This approach gives you full flexibility but involves doing a lot of extra work. Another option is to create a partial class that extends the automatically generated proxy object. This class could add new properties, like TimeInClient, TimeOutClient and BottomTimeClient, which use the same private backing fields as the generated properties, but have additional logic needed to understand the relationship between time-in, bottom time and time-out. Your user interface would bind against the new properties instead of the generated properties, but since they use the same backing fields for their values you can still send the object back to the server to store any changes.

Is Microsoft doing anything to help?

Wanting to share some logic between the server and client is a common pattern. Surely Microsoft must be thinking about how they can help. By improving the service reference tool in Visual Studio Microsoft made it easier to share types across boundaries. But there is still allot that can be done. The Smart Client Software Factory contains functionality to map between service entities and business entities, which let you build richer business entities on the client, and get some framework support to map between the different types. But I’m sure they can do better…

Yesterday Microsoft announced 50 new session abstracts for PDC 2008 held in October. It’s really interesting to read the session abstract to get some ideas of unannounced products and project they are working. One of the sessions that caught my attention is "Microsoft Silverlight: Building Business Applications":

What if you could develop your solutions with the ease pioneered by Microsoft Office Access, deploy them like an Internet application, and take advantage of the power of Microsoft .NET? Learn about an exciting new technology that is all about making business applications for RIA (Rich Internet Applications) much easier to build. In this session, hear how we've made n-tier application development as simple as traditional 2-tier, provided application level solutions to developers, and how we're doing all of this with the same .NET platform and tools on both the client and server.

I guess it’s safe to assume that Microsoft have some really cool stuff in the works. Which approach they take to make n-tier development as simple as traditional 2-tier development I don’t know. But I wouldn’t be surprised to see some kind of smart sharing of linked code files, with partial classes or compiler directives to separate server and client code. It’s also interesting that SOA-expert Pat Helland has been working on a project in the area of RIAs, so hopefully he has been able to provide some valuable input on how to architect RIAs.

Which approach do I use?

Well, it depends… For larger Silverlight projects I tend to consider the automatically generated proxy objects as dumb data transfer objects that I wrap in business objects used by my Silverlight application. The advantage is that I get full control over the object, but the drawback is that I have to do more manual work… For smaller projects where I need minor changes I tend to use partial classes to extend the proxy objects with additional properties used by the client, but backed by the same fields as the properties that get serialized when sending the object back to the server. In the long run I’m a strong believer in tools or frameworks that help us reuse code between server and client in a clean way giving us something smarter than the proxies generated by Visual Studio. Frameworks like CSLA Light or the tools Microsoft will announce at PDC will probably help to deliver this. I expect the announcements at PDC to be to Silverlight what ASP.NET Dynamic Data is to web application. Dynamic Data lets you scaffold a complete data driven application, but without compromising customizability or generating tons of hard to maintain crappy code.

How do you share business logic between server and client?

I try to keep this blog more or less focused on software development, but every now and then I think it’s good to throw in some personal updates as well… Last week Hege and I went up to Cairns for a dive expedition with TAKA Dive to the Northern Great Barrier Reef and Coral Sea. Before the trip we did some research, checking out YouTube video and Flickr photos from the area, but the trip turned out even better than what we dared hop fore. Already after the first dives on Ribbon Reef #10 a group of 6-7 Dwarf Minkie Whales came up to the boat. We got to snorkel with the animals, and it was just unreal having a 6 meter animal swimming just couple of meter underneath me. Another highlight of the trip was the diving at Osprey reef - heaps of sharks, big fishes and amazing visibility (30-40 meters). We also did a shark feed at North Horn, which attracted schools of white tip reef shark, gray whaler sharks, and some oceanic white tip sharks. On one of Hege’s photos we could count more than 30 sharks in one shot. So I don’t know the total number, but they were plentiful. We did a total of 14 dives over a 5 day/4 night trip, and got to sea sharks, whales, turtles, big schools of fish, amazing healthy coral reefs and more.

We were about 30 people on the boat, but it never felt crowded. It had plenty of space, good cabins, modern mid-deck with plasma TV’s and computers where we watched the video of the days diving every night. The food was top notch, including dishes like lamb roast, roast beef and homemade lasagna. The crew did an awesome job, and I can’t do anything but highly recommend TAKA for anyone wanting a great dive expedition to the Great Barrier Reef.

I’m going to keep this post short, and Hege have also posted a more lengthy story on her blog, as well as heaps of photos on Flickr. I’m including some of the best once here, but I strongly recommend checking out her Flickr feed.

MinkieWhale  PotatoCod

Shark1 Shark2

Shark3  Shark4

<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910