Windows Support Number

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 24 March 2011

Calling 'HtmlPage.Window.Invoke' from a background thread

Posted on 05:59 by Unknown
Received what at the time seemed like a strange exception from a piece of code in a silverlight I was debugging yesterday - 'this operation can only occur on the ui thread.'

What makes this strange was the fact this wasn't attempting to update any UI controls or classes bound to views,  infact this was the complete opposite, this was a method trying to invoke out to javascript from a randomly assigned thread (thread pool). To compound my confusion not only was I seeing this intermittently but the action I was trying to achieve still appeared to completely successfully, writing to an in-memory javascript log!

Now the answer is as you probably known, is...

Any interaction with the host browser from silverlight has to be done on the UI thread even if you're not updating any UI.





Read More
Posted in Silverlight, threading, UI | No comments

Monday, 21 March 2011

Considerations when building a caching mechanism for WP7Contrib.

Posted on 14:39 by Unknown
For anyone wanting to build a cache for an application, there are several guidelines(may be rules) you want to beware of and more than likely abid by.

Firstly, more than likely you are going to have an in-memory representation of the cache as well as a persisted format if the cache is to survive application restarts. Now an in-memory representation is more than likely going to be a hash-table - especially if you want acceptable retrieval time. Now this is where 'GetHashCode' comes into play and the importance of this is explained perfectly by Eric Lippert,

Secondly, and this follows on from a statement in Eric's post - 'the integer returned by GetHashCode should never change', if you're using the internal state of a class to calculate the hash code, you can't allow that state to change overtime. So this means you are more than likely going to have create a copy of the instance before using it as a 'key' when adding to the cache, because this will then negate this issue,

Thirdly, the data in the cache will be become stale at some point, so expirying data out of the cache will be a requirement, a cache is not a replacement for permanent persistence store like a database or files etc,

Finally, the cache should be transparent to the application, what I mean by this is if the cache fails during execution the only observable difference should be in application performance, the application should still be fully functional. This means if an exception occurs inside the cache it shouldn't be propergated to the host application.

So when we defined the cache providers in WP7Contrib we added an extra interface to the 'Common' project - ICloneable<T>. This is there to help define how an object will be copied\cloned, it defines methods for both shallow and deep copying. We use this interface for any class we wish to use as a 'key' when adding to a cache provider.
Read More
Posted in Caching, CodePlex, WP7, WP7Contrib | No comments

Thursday, 17 March 2011

WP7Contrib: RESTful communications using HttpWebRequest & Rx extensions

Posted on 14:50 by Unknown
20/03/2011 UPDATE: code samples after changes to WP7Contrib code base.

Simply I want to show how we do communication with web services when using WP7Contrib.

When using web services from WP7 devices you have several options on how you are going to communicate  with the service. What follows are the steps required to achieve this using the WP7Contrib. We show how to use a third party web service and how this pattern is very advantageous in this scenario.

We aren't advocating this is a better approach than using WCF data services (or any of other implementation), we've just been burnt before by WCF and prefer to possibly go through the pain of hand crafting resources (DTOs) than deal with WCF configs and it's short comings in Silverlight.

In Silverlight as we all know communication with web services has to be asynchronous, this in it's self is not a problem, but it can make the code ugly at best and difficult to understand. This is where Reactive Extensions come to the rescue, they allow you to handle asynchronous calls in a synchronous programming manner - I can write the code in a synchronous manner and it will execute in an asynchronous manner.

In the example below I'm using one of the 'secret' services available via the Google API - there's a great post here giving more info. The example uses the Google Weather API, this is a read only service that gives weather information for any location around the world, you access the data via a HTTP GET request.

Below is the code to make a HTTP request and debug out when the response is returned:

weatherResource.Get<xml_api_reply>("london")
   .ObserveOnDispatcher()
   .Subscribe(weather =>
   {
      Debug.WriteLine("we got weather news!");
   });

As you can see the code is very simple and has abstracted away the complexity of using the HttpWebRequest class. The other thing to note is the use of 'ObserveOnDispatcher' and 'Subscribe', these are method provided by the Reactive Extensions framework, if you're not familiar I would recommend doing some research. The 'ObserveOnDispatcher' method makes sure the asynchronous method invocation is on the dispatcher thread so we don't get any surprises when binding data. The 'Subscribe' method allows you to register as an observer.

Below is the code to initialise the 'weatherResource':

   weatherResource = new ResourceClient()
      .ForType(ResourceType.Xml)
      .UseUrlForGet("http://www.google.com/ig/api?weather={0}");

We use a class called 'ResourceClient' which implements an interface IHandleResources. The methods POST & PUT have 2 generic types, one for the request resource and one for the response resource. The reason why we don't just a single generic type is because this pattern is not just for making RESTful calls to REST services but can be used to make calls to SOAP services. The 2 other methods of interested are used to defined how the resource is serialized (JSON or XML) - 'ForType' and the  templated URL to use for a HTTP GET request. The parameters passed in the previous example to the 'Get' method are combined with the templated URL to create a valid url - http://www.google.com/ig/api?weather=london (make sure you 'view source' to see what is returned).

Below is the code defining the 'weatherResource':

   private IHandleResources weatherResource;

As you can see we define the interface 'IHandleResources<TRequest, TResponse>' this defines the operations that are supported - GET, PUT, POST & DELETE, along with a set of methods for configuring the URLs (for the operations), authentication, namespaces etc. The interface definition is shown below:

 public interface IHandleResources<TRequest, TResponse> where TRequest : class, new() where TResponse : class, new()
{
      ResourceType Type { get; }
      IHandleResources ForType(ResourceType type);
      IHandleResources UseUrl(string url);
      IHandleResources UseUrlForGet(string getUrl);
      IHandleResources UseUrlForPost(string postUrl);
      IHandleResources UseUrlForPut(string putUrl);
      IHandleResources UseUrlForDelete(string deleteUrl);
      IHandleResources WithNamespace(string prefix, string @namespace);
      IHandleResources WithBasicAuthentication(string username, string password);
      IObservable<TResponse> Get<TRequest>();
      IObservable<TResponse> Get<TRequest>(params object[] @params);
      IObservable<TResponse> Put<TRequest, TResponse>(TRequest resource);
      IObservable<TResponse> Put<TRequest, TResponse>(TRequest resource, params object[] @params);
      IObservable<TResponse> Post<TRequest, TResponse>(TRequest resource);
      IObservable<TResponse> Post<TRequest, TResponse>(TRequest resource, params object[] @params);
      IObservable<TResponse> Delete<TRequest>();
      IObservable<TResponse> Delete<TRequest>(params object[] @params);
      IHandleResources WithHandlerDefinitions(JsonHandlerDefinitions definitions);
}

As you can see the request & response resources used in the examples are of type 'xml_api_reply' - I generated this automatically from an example XML response using XML Schema Definition Tool (xsd.exe). This was then included in the demo project and I removed manually the attributes which aren't supported by Silverlight in WP7.

If I was using a RESTful service which returned JSON I would have handcrafted the request & response resources as required - we use JSON.NET to handle the serialisation of JSON, so any request or response resources have to support serialization\deserialization by JSON.NET when communication with JSON based services.

That pretty much covers the basics of what is required to make a HTTP request using this pattern. You can find the demo application 'CommunicationDemo' in the Spikes directory of the WP7Contrib code base. Shown below is a screenshot from the demo application.


For completeness I've included a more detailed class on how to use this pattern - this is in the demo application. It shows how we don't use the 'ResourceClient' or the resources directly in a view model - we map the resources into a model and encapsulate this and the 'ResourceClient' class inside a 'Service' class. The service class then uses it's own IObservable<T> via the Subject<T>class. If you're not come across the idea of 'services' before check out Eric Evans Domain Driven Design book.

    public class WeatherService : IProviderWeather
    {
       private readonly IHandleResources<xml_api_reply, xml_api_reply> weatherResource;

       public WeatherService()
       {
            weatherResource = new ResourceClient()
                .ForType(ResourceType.Xml)
                .UseUrlForGet("http://www.google.com/ig/api?weather={0}");
       }

       public IObservable<WeatherSummary> Get(string location)
       {
            try
            {

                // TODO service related stuff before call...
               
                var observable = new Subject<WeatherSummary>();
                this.weatherResource.Get<xml_api_reply, xml_api_reply>(location)
                                        .Subscribe(response =>
                                                       {
                                                          var weather = ProcessResponse(response);
                                                          // TODO other service related stuff like caching
         
                                                          observable.OnNext(weather);
                                                       },
                                                   observable.OnError,
                                                   observable.OnCompleted);

                return observable;
            }
            catch (Exception exn)
            {
                throw new Exception("Failed to get Weather!", exn);
            }
       }

       private WeatherSummary ProcessResponse(xml_api_reply response)
       {
            var summary = new WeatherSummary();
            summary.Current = new Weather();
            summary.Current.Temperature = Convert.ToInt32(response.weather[0].current_conditions[0].temp_c[0].data);

            return summary;
       }
    }

So that pretty much covers it, keep your eyes open for a post on applying this principle to the MS Bing Maps API by Rich Griffin.



Read More
Posted in CodePlex, REST HTTP, WP7, WP7Contrib | No comments

Sunday, 6 March 2011

WP7Contrib: Why we use SilverlightSerializer instead of DataContractSerializer

Posted on 07:04 by Unknown
Like all applications developed for WP7 devices we (WP7Contrib) want to get the best performance possible from any libraries or patterns we use and this applies to how we serialize and deserialize data inside the WP7Contrib. In several of the libraries which make up the WP7Contrib we require binary serialization support, specifically we use binary serialization for the isolated storage cache provider and the storage service in the services project.

Simply after testing we found SilverightSerializer gave better performance, shown below the results of serializing a collection of 1000 items compared to the DataContractSerializer.


It shows the tick count, the equivalent time in milliseconds and the size of the generated byte array . So you can see the SilverlightSerializer gives better performance from both a time and size perspective. These results were generated using the StopWatch class.

The only downside I can see from using the SilverlightSerializer is the support for generic types used with custom collection types - you have to explicitly implement a serialization helper class for each type, you'll see this in the test application I created I had to create a class ProductCollectionSerializer to support serialization of the type ObservableCollection.

The test application 'SerializationPerformance' is available in the Spikes directory of the WP7Contrib code base.

That's it for now, I'll be back with the post about RESTful communication in WP7Contrib.
Read More
Posted in CodePlex, Serialization, WP7, WP7Contrib | No comments

Saturday, 5 March 2011

WP7Contrib: Trickling data to a bound collection

Posted on 13:04 by Unknown
Recently Rich & I've had problems with adding items to list control where the data template is chocked full of XAML goodies - triggers, opacity masks, images, overlay, loads of things I know little about... ;) The culmination of all this UI goodness when used with a bound list control is the blowing of the '90 mb limit' for WP7 devices. As I posted before, we knew the problem wasn't with the data model so we knew it was the data template, now there is plenty of coverage of issues with data templates in Silverlight and this post it not adding to this list, its about a technique to reduce the memory footprint once you've done as much as you can with the data template.

So we're developing an app which exhibits high memory usage, now obviously we want to reduce the memory consumption for the app so we trimmed down the data template for the list control, but we're still seeing the 90 mb limit exceeded for relative few items. Our data set could in theory contain several hundred items but it was failing for less than 40 items and this is an issue.

So Rich can up with the idea of adding a delay between each add to the bound collection - trickle the items, so after some experimentation we discovered the use of a call to the garbage collector when all the items have been added along with a delayed between each item reduced the memory usage to an acceptable level.

The initial implementation Rich came up with used a DispatcherTimer and I thought I could improve on this with Rx (reactive extensions) - this didn't work out (Rich - I won't try and be smart again). Simply the Rx added to much overhead and this caused  the rendering to be less smooth than the DispatcherTimer implementation. So we went back to the DispatcherTimer.

We've provider the implementation in the WP7Contrib, there are currently 2 implementations:

  • TrickleUniqueToCollection<T> - Only adds items not already in the collection,
  • TrickleAllToCollection<T> - adds all items irrespective if they already exist in the collection.

Both classes implement the interface ITrickleToCollection<T> via the base class BaseTrickleToCollection<T>. It defines a set of methods that allow the starting, pausing, resuming and stopping of trickling as required. The start method defined the time delay between each add as well the source collection and the destination collection.

public interface ITrickleToCollection<T>
{
   bool Pending { get; }
   bool IsTrickling { get; }
   void Start(int trickleDelay, IEnumerable<T> sourceCollection, IList<T> destinationCollection);
   void Stop();
   void Suspend();
   void Resume();
}

Both classes use a functional style via the Action method to notify the caller when an action occurs, e.g. when the trickling has started, stopped, item added etc. Shown below is the constructor signatures for both implementations:

public sealed class TrickleUniqueToCollection<T> : BaseTrickleToCollection<T>
{
   public TrickleUniqueToCollection(Action addedAction, Action completedAction)
      : this (addedAction, () => {}, () => {}, () => {}, () => {}, completedAction)

   public TrickleUniqueToCollection(Action addedAction, Action startedAction, Action stoppedAction,
                                    Action suspendedAction, Action resumedAction, Action completedAction)
   : base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
}

public sealed class TrickleAllToCollection<T> : BaseTrickleToCollection<T>
{
   public TrickleAllToCollection(Action addedAction, Action completedAction)
      : this (addedAction, () => {}, () => {}, () => {}, () => {}, completedAction)

   public TrickleUniqueToCollection(Action addedAction, Action startedAction, Action stoppedAction,
                                    Action suspendedAction, Action resumedAction, Action completedAction)
   : base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
}

Each class has a specific implementation for the DispatcherTimer, shown below is the implementation for the TrickleAllToCollection<T>, the interesting detail is the timer being shutdown once trickling all of the items has completed - no point leaving it running consuming device resources (memory & CPU).

public TrickleAllToCollection(Action addedAction, Action startedAction, Action stoppedAction,
                              Action suspendedAction, Action resumedAction, Action completedAction)
   : base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
{
      this.DispatcherTimer = new DispatcherTimer();
      this.DispatcherTimer.Interval = this.StartupDelay;
      this.DispatcherTimer.Tick += delegate
         {
            if (this.DispatcherTimer.IsEnabled)
            {
               if (this.Source.Count != 0)
               {
                  var item = this.Source.Dequeue();
                  this.Destination.Add(item);
                  this.AddedAction();

                  if (this.Count == 0)
                     this.DispatcherTimer.Interval = this.Delay;

               this.Count++;
               }
               else
               {
                  this.DispatcherTimer.Stop();
                     this.CompletedAction();
               }
            }
         };
   }

Using these classes is simply, just instantiate an instance with the required Action methods:

 this.trickler = new TrickleUniqueToCollection<Property>(() => this.UpdateTrickleStatus(TrickleStatus.Added),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Started),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Stopped),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Suspended),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Resumed),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Completed));

And then start trickling to the bound destination collection:


   this.trickler.Start(432, result.Properties, this.aggregatedPropertiesView);

That's pretty much it, as I said these are in the WP7Contrib and can be found in the Collections project. I've also created a demo application 'TrickleDemo' in the Spikes directory of the code base. This demo doesn't show any memory problems with data templates, it only demonstrates how to use this trickle to collection pattern.

Next time a post about how we do RESTful communication in WP7 using the HttpWebRequest  & Reactive Extensions...
Read More
Posted in CodePlex, WP7, WP7Contrib | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • Unit testing Rx methods Timeout & Retry with moq
    Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran i...
  • StructureMap: ILifecycle
    The other day I wanted to control the scope of a service inside a web based app with semantics which didn't fit either 'HttpContextS...
  • How many pins can Bing Maps handle in a WP7 app - part 1
    part2 -  http://awkwardcoder.blogspot.com/2011/10/how-many-pins-can-bing-maps-handle-in.html part3 -  http://awkwardcoder.blogspot.com/2011/...
  • MVVM anti-pattern: Injecting the IoC container into a View Model
    This is another anti-pattern I've seen a lot recently, the dynamic use of the IoC container inside a view model to resolve child view mo...
  • Implementing a busy indicator using a visual overlay in MVVM
    This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whil...
  • Daily Dilbert Service - the most important service I've ever written...
    NuGet package available here ... First off a big shout to  @hamish  &  @leeoades  on this one - I'm just blogging about it. At work ...
  • WP7Contrib: Isolated Storage Cache Provider
    15/04/2001 - The code for this example has been updated - new WP7 build of SilverlightSerializer, this has removed the explicit implementati...
  • Mocks, Fakes, Stubs - why bother?
    Ever wondered why there are so many different names for the objects that mimic behaviour of the 'real' objects in a system - mocks, ...
  • No GetEntryAssembly in Silverlight!
    Ran into a problem today, wanting to get the assembly that started the an application. Now this isn't a tricky problem just had to bend ...
  • Bad developers love 'The Daily WTF'
    When 'The Daily WTF' started up back in 2003/2004 it was a great laugh looking at shocking code other developers wrote, but after a ...

Categories

  • .Net
  • .Net 4.5
  • Abstractions
  • Advertising
  • Agile
  • Agile Courage
  • AOP
  • Async
  • automated testing
  • Azure
  • Azure IIS RESTful development
  • BDD
  • Bing Maps
  • Bounded Context
  • C#
  • C# 5.0
  • Caching
  • Chocolatey
  • CLoud
  • CodePlex
  • Coding
  • Coding Building CI Testing
  • Coding C#
  • coding C# IoC StructureMap
  • Coding Functional-Programming
  • Coding REST Knowledge
  • Coding Services
  • Coding TDD Refactoring Agile
  • Command
  • continuous testing
  • coupling
  • CultureInfo
  • DAL
  • databases
  • DDD
  • DDD Coaching
  • DDD Domain Events Auditing nHibernate
  • DDD Entities Value Objects
  • Debugging
  • Design Patterns
  • Design Patterns Databases Auditing
  • Developement
  • Development
  • Development Coding
  • Development Process
  • Development unit testing
  • Development VS 2011
  • Diagnostics
  • Disposable
  • Exceptions
  • FINDaPAD
  • FindaPad Property Rental Windows Phone 7 Mobile Devices
  • Fun Coding Duct-Tape
  • Hotfixes
  • integration testing
  • IoC
  • jasmine
  • javascript
  • Jobs Development
  • LINQ
  • marketplace
  • Mobile Devices
  • Mocking
  • MSDN Coding
  • MSpec
  • Multilingual
  • MVC
  • MVVM
  • nCrunch
  • nHbiernate Repository Pattern Criteria
  • nHibernate Auditing Design Fluent
  • nHibnerate Entities Events Listeners
  • node.js
  • nodes.js
  • Nokia
  • NoSQL RavenDB Azure Development
  • Observations
  • OO
  • ORM
  • Performance
  • Portable Class Library
  • Portable Library
  • PostSharp
  • Process
  • Rants
  • RavenDB IIS 7.5 Development
  • Reactive
  • Reactive Extension
  • Reactive Extensions
  • ReadOnlyCollections
  • Resharper
  • REST Distributed-Systems
  • REST HTTP
  • rest web
  • RESTful
  • Rx
  • Serialization
  • Silverlight
  • Silverlight Installation
  • Task
  • TDD
  • TDD IoC DI
  • TDD Mocking
  • TDD Team Observation
  • Telerik
  • testing
  • threading
  • TPL
  • UI
  • Undo-Redo
  • unit testing
  • ViewModels
  • VS 2012
  • wcf
  • web api
  • Web Services
  • web services mobile devices data
  • WebAPI
  • Windows
  • Windows 8
  • windows phone
  • Windows Phone 7
  • WP7
  • WP7 Bing Maps Development Network HTTP
  • WP7 Bing Maps Development UK Crime
  • WP7 Bing Maps Development UK Crime Clustering
  • WP7 Bing Maps Development UK Polygons Clustering Performance
  • WP7 cryptography bouncy castle
  • WP7 Cultures C#
  • WP7 feedback development app store
  • WP7 Javascript web browser
  • WP7 MSBuild
  • WP7 ORM Databases performance
  • WP7 Serialisation
  • WP7 SilverlightSerializer C#
  • WP7 sqlite performance development
  • WP7 WP7Contrib Bing Maps Development
  • WP7 WP7Contrib Bing Maps Polygon Development
  • WP7 WP7Contrib CodePlex
  • WP7 WP7Contrib CodePlex Bing Maps Development
  • WP7 WP7Contrib CodePlex ObservableCollection
  • WP7 WP7Contrib ILMerge .Net
  • WP7 WP7Contrib Phone Maps
  • WP7 WP7Contrib SilverlightSerializer C#
  • WP7Contrib
  • WP7Contrib Bing Maps WP7
  • WP7Contrib WP7 Geo-Location development C#
  • WP7Contrib WP7 HTTP Compression
  • WP7Contrib WP7 Url Development Rx
  • WP7Dev
  • WPF
  • WPF Cultures
  • WuApi
  • XAML

Blog Archive

  • ►  2013 (16)
    • ►  November (5)
    • ►  September (3)
    • ►  August (1)
    • ►  July (1)
    • ►  June (3)
    • ►  May (2)
    • ►  January (1)
  • ►  2012 (44)
    • ►  November (2)
    • ►  October (8)
    • ►  September (5)
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (2)
    • ►  March (13)
    • ►  February (4)
  • ▼  2011 (52)
    • ►  December (3)
    • ►  November (5)
    • ►  October (7)
    • ►  September (7)
    • ►  August (11)
    • ►  July (4)
    • ►  May (2)
    • ►  April (1)
    • ▼  March (5)
      • Calling 'HtmlPage.Window.Invoke' from a background...
      • Considerations when building a caching mechanism f...
      • WP7Contrib: RESTful communications using HttpWebRe...
      • WP7Contrib: Why we use SilverlightSerializer inste...
      • WP7Contrib: Trickling data to a bound collection
    • ►  February (3)
    • ►  January (4)
  • ►  2010 (1)
    • ►  August (1)
  • ►  2009 (32)
    • ►  December (3)
    • ►  November (7)
    • ►  October (6)
    • ►  September (11)
    • ►  April (1)
    • ►  March (4)
Powered by Blogger.

About Me

Unknown
View my complete profile