Archive for April, 2010


I am planning for a multi part blog series on Silverlight Out Of Browser Capabilities using Silverlight 4.

Recently I have explored the silverlight’s capabilities as an Out Of Browser application and I found it’s very much sound and interesting.

But what I have noticed is all the features of silverlight OOB (Out Of Browser) capabilities are not available at one place or not really available in proper order to explore for any developer like me. So I have decided to write a multi part blog series on Silverlight’s Out Of Browser capabilities.

So in next couple of months I will write around 8 blogs on the topic and the current list of  blogs is as following……

1.> Getting Started with Silverlight’s Out of Browser Capabilities

2.> OOB Custom window and events

3.> OOB Notification API

4.> OOB Trusted File Access

5.> OOB Trusted Network Access

6.> OOB Trusted COM API

7.> OOB XAP signing for trusted Applications

8.> OOB Using Isolated Storage

I will try to publish these blogs on time and in given order.

As I will write the blog, I will link that blog with the list given here that anyone can simply browse through any blog on the topic from here only.

I will use Windows 7 Ultimate 64 bit, Visual Studio 2010 and Silverlight 4.0 throghout the blog series.

In my last blog post I had explained the two standard ways of  validating data in Silverlight 4. One way was implementing “INotifyProprtyChanged” interface. And the second way was “INotifyDataErrorInfo” interface, which is the new option available with Silverlight 4 for validating data.

Now in this blog post I will show, how we can use the validation asynchronously with “INotifyDataErrorInfo” interface in Silverlight 4.

Simple Scenario where Asynchronous Validation can be useful:

Assume that we are just registering new users for a website accepting username and password in Silverlight front-end UI. Now when user enters the preffered username, we need to check that preffered username is available or not. So we just call the web service for checking the availability of preffered username. But as we are using Asynchronous Data Validation to perform this username validation, and so while the web service returns the response, Silverlight front-end UI still remains available for User for entering password and so on……….

Here is the Entity class “User” which implements the “INotifyDataErrorInfo” interface as shown in the image below……

Here is the property “username” in the entity class “User” which is getting validated asynchronously…….

In this code “WebService.CheckUserNameAvailability(string userName, UserNameCallBack callBackMethod)” accepts userName as first parameter and CallBackMethod for UserNameCallBack delegate as a second parameter. So Web Service returns the response by calling this callBackDalegateMethod and passing “isUserNameAvailable” variable in the method. Then this method process the validation as shown in the image below………

So this callBackDalegateMethod will be called whenever the WebService will be ready with the response. So if web service returns the response in say 10 seconds, by the time, User can enter the password and so on.. And we can keep the “OK” button disable by that time as User can not click it before validation completes on “UserName” as shown in the image below…..

After this asynchronous validation, if UserName will not be available silverlight validation framework generates the error visualisation as shown in the image below……

 

So it was all about Asynchronous Data Validation with Silverlight 4.

For more info on silverlight, just go to, http://www.silverlight.net

For more on silverlight 4, click here, http://www.silverlight.net/getstarted/silverlight-4/

For more details on Silverlight 4.0, you can also browse through following blogs………

http://timheuer.com/blog/          —  Tim Heuer Blog – Silverlight Program Manager

http://blogs.silverlight.net/blogs/jesseliberty/       — Jesse Liberty Blog – Silverlight Program Manager

http://johnpapa.net/              —  John Papa website

http://adamkinney.wordpress.com/          — Adam Kinney Blog — For Silverlight graphics, interface and Expression Blend

With silverlight 3 we were able to validate data using ‘INotifyPropertyChanged’  (System.ComponenetModel.INotifyPropertyChanged) interface.

Now in silverlight 4 we have one new interface called ‘INotifyDataErrorInfo’  (System.ComponenetModel.INotifyDataErrorInfo) as well.

 

Any Entity class can implement both or any of these interfaces to validate the data.

I am using the Entity class “User” to validate the data (Name and Email) as shown in the image below……

Here “Name” is validating through “INotifyDataErrorInfo” and “Email” is validating through “INotifyPropertyChanged”.

Validation using “INotifyPropertyChanged” :

To use “INotifyPropertyChanged”, we need to implement one event “PropertyChanged” in code behind file as shown in the image above (second image in the post).

We also need to set couple of attributes (“ValidatesOnExceptions”, “NotifyOnValidationError”) on the control in XAML code which actually performs the validation as shown in the image below……

Now when the value of property “email” is changed, code in the following image throws an exception…..

And this exception is then handled by silverlight validation framework and it creates the error visualization on silverlight control as shown in the folowing image…….

Validation using “INotifyDataErrorInfo” :

To use “INotifyDataErrorInfo” we need to implement one event “ErrorsChanged”, one method “GetErrors” and one property “HasErrors” in the code behind file as shown in the image above (second image in the current blog post).

Here we do not need to implement anything in XAML code to perform validation as in case of “INotifyPropertyChanged”.

Now when the value of property “name” is changed, code in the following image validates the value of “name” and called the event “ErrorsChanged” and which in turns called the method “GetErrors()” which returns the “errors” (or null if there is no error) as shown in the image below…….

And then if “GetError()” method returns any error is then handled by silverlight validation framework and it creates the error visualization on silverlight control as shown in the folowing image…….

Silverlight validation framework seems lot more sound with silverlight 4 now in compare with silverlight 3. Silverlight 4 validation framework also supports “Asynchronous Data Validation”, which I will discuss in my next silverlight blog post.

 For more info on silverlight, just go to, http://www.silverlight.net

For more on silverlight 4, click here, http://www.silverlight.net/getstarted/silverlight-4/

For more details on Silverlight 4.0, you can also browse through following blogs………

http://timheuer.com/blog/          —  Tim Heuer Blog – Silverlight Program Manager

http://blogs.silverlight.net/blogs/jesseliberty/       — Jesse Liberty Blog – Silverlight Program Manager

http://johnpapa.net/              —  John Papa website

http://adamkinney.wordpress.com/          — Adam Kinney Blog — For Silverlight graphics, interface and Expression Blend

Currently I am working on a sample project in .net 4.0 and in which I am using Entity Model to expose my Data from SQL Server 2008 database.
Now I am also using POCO entities as in .Net 4.0 POCO entities gives us better control over the entities we are using to map with the entities in the Entity Model compare to the auto generated entities generated by Entity Model itself.
Now here are my two POCO entities ‘User’ and ‘Account’….

Click the image for full view…….

Now I am querying data from SQL using LINQ to SQL as shown below in code…….

This code should return the ‘Account’ with users in it but instead it returns no data in browser or no Error/ Exception as well but simply shows “Webpage can’t be displayed” in IE8 as shown in the image below…..

Now I just tried to figure out the issue but not found anything as there was no error or Exception in the code and the only symptom of this issue was that when I was debugging the code it comes to the debug point twice in the method where I am using LINQ to SQL. The good thing was it was showing the correct results inside the variable ‘accountList’ but was not showing any result in the browser.

Then I suddenly notice that my ‘Account’ entity has the property ‘Users’ of type [List of ‘User’] (Angle brackets not allowed here) and then again each User has the property ‘Account’ and again each ‘Account’ has List and seems like infinite dependency on each other….

So what I tried for the solution was, I just added the attribute [IgnoreDataMember] (using System.Runtime.Serialization assembly) on ‘Account’ property in my ‘User’ entity class as shown below in image……..

And tested it again…. and guess what…. Bingoooo…. It just simply works and now browser shows the correct data as shown in image below……

Click the image for full view…….

So what I guess is that it was happening because of kind of infinite circular dependency created by data model I am using.

(If I am not using extention method ‘Include()’ in my LINQ to SQL, it works just fine and returns the Accounts without ‘Users’ off course)

But the question that still remains is, it’s the known issue or the unexplored bug of using LINQ to SQL with POCO entities.