Saturday 6 August 2011

Passing Objects between Silverlight Applications

This week I have been making some prototype applications using Silverlight, which has been awesome, and I decided that I wanted to pass a few objects between these applications. If this wasn't a quick prototype and didn't have to run off of a Sales person's laptop I would have whipped together a quick bit of RIA Services and got them talking that way. However these are just two out of browser applications.


Enter Local Messaging.

Silverlight has a great little feature, that I'm not sure is too often used, Local Messaging. Local Messaging allows you to send simple string messages between two Silverlight applications running on a local computer. This can be within the same Web Page or two Out of Browser Applications


Local messaging works by having a LocalMessageReciever listening for messages in one application and then a LocalMessageSender sending messages from the other. If you want two way communication you simply have a listener and sender in each application.



// In the receiving application:
LocalMessageReceiver messageReceiver = new LocalMessageReceiver("receiver");

// In the sending application:
LocalMessageSender messageSender = new LocalMessageSender("receiver");

I'm not going to explain how you send messages between the applications as MSDN does a far better job of doing so. However MSDN only talks about simple string messages, I wanted to send objects.

Serialising the objects

The obvious solution I thought was to just serialise the objects to a string and then back at the other end. Silverlight doesn't support binary serialisation and as we have to send the message as a string the Serialisation could be XML, which is common practice and I'm sure most people know instinctively how to do so, however XML is bloated. Alot of our message string would be information we aren't interested in. Now in most applications we don't worry about this and XML is fine however this local app messaging has a maximum string size of 40 kilobytes. We don't want to fill this up with XML notation, instead we want something alot leaner.


Although Silverlight doesn't support Binary Serialisation it does however support JSON. JSON is a far less verbose markup which will mean our messages will be more about the object data and structure rather than syntax which is a good thing


When it comes to JSON serialisation there are a few options, the built in DataContractJsonSerializer , JSON.Net etc. For the ease of setup I have gone for using the build in DataContractJsonSerializer. Here i take a generic object and serialise it to a stream which we can then use in our message.


DataContractJsonSerializer serialiser = new DataContractJsonSerializer(message.GetType());
var stream = new System.IO.MemoryStream();
serialiser.WriteObject(stream, message);

With this string we can simply use a streamreader to get the string that our sendasync message sender method requires.


 new System.IO.StreamReader(stream).ReadToEnd())

But there's a problem, we cant just send the json object, as at the other end we won't know the type. What we have to do then is send the type and the serialised form. This leaves us with the following:


stream.Position = 0;
sender.SendAsync(String.Format("{0}|!|{1}", message.GetType(), new System.IO.StreamReader(stream).ReadToEnd()));

Note: I have used |!| as a way to know where the type name ends and the stream begins, I could have said {type}={jsonserilaisedobject} but I chose not too, for neatness though this should be possible I just chose to use a delimiter I knew wouldn't appear in my serialised object.

Receiving the message and getting the object back

To get the object back in the other application, after receiving the message from the sender we have to split the string based on the delimeter and then deserialise the object.


var type = e.Message.Substring(0, e.Message.IndexOf("|!|"));
var data = e.Message.Substring(e.Message.IndexOf("|!|") + 3);

The DataContractJsonSerializer requires a stram not a string to deserialise an object so next take the data string and turn it into a stream. Then just deserialise.


var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(data));
var serialiser = new DataContractJsonSerializer(Type.GetType(type));
if(type == "OurCustomType"){
    var result = (OurCustomType)serialiser.ReadObject(stream);
}

And that's it, obviously handling lots of types would mean not using an if statement, maybe a switch or whatever but the principle is the same.


I hope you find this of use, it certainly made my prototype far more functional and proved a neat way of doing it

Friday 22 July 2011

linq to lucene magic unicorn edition

This week I started to look at using Lucene.Net again in a project. Previously I have used it directly however whilst on the new look Lucene.Net website I noticed the linq to lucene project. Which at first look seemed pretty good. However upon looking at the source I found it only works with XML and LinqToSql, this prototype project however was in .Net 4 and used Sql CE4 with Entity Framework 4.1 Code First.


Rather than abandon linq to lucene I decided it should be easy enough to get it working with EF4.1. DatabaseIndexSet contains the LinqToSql specific code so for the purpose of producing a proof of concept this is where all the changes go.


The first item to change is to rescope the generic where clause to be DbContext rather than the linq context.


 public class DatabaseIndexSet : IndexSet
        where TDataContext : DbContext

Next the Write Method needs altering, the LinqToSQL implementation uses the GetTable method, however E.F doesn't really have the same methods. Instead with E.F we can use the Set method with our entity as the generic, this will allow us to obtain all of the records


Finally the GetTableTypes method needs "tweaking", I say tweak as so far its a bit of bodge. The GetTableTypes method is used to find all of the "tables" within our database context which are then later used to create lucene index files. The original LinqToSql method ensured that each property was a generic type and that it is assignable from the base type. For our initial run as our entity classes don't inherit anything I have removed that check, just ensuring the properties are generic. This will be added back in for the final release.


With those changes made we can then use LinqToLucene as follows:


 var index = new EnquiriesIndexContext();
            index.Write();
            var query = from p in index.Parts
                        where p.PartNumber == "ghjtyugfhj-61"
                        select p;
           Console.WriteLine("Simple Query:");
           ObjectDumper.Write(query);
           Console.WriteLine("Query Output: {0}", query.ToString());
           
            var results = query.ToList();
            Console.WriteLine("SimpleDemo returned {0} results", results.Count);


Have a go of the prototype code that is attached, I'll be aiming to tidy it up and get it pushed back to the main project on codeplex over the next week. Enjoy

Saturday 7 May 2011

Using the Razor Engine to create PDFs

I've been using MVC 3 and the Razor syntax for a while now, and a while ago whilst reading how people were using Razor for their email templates I had the idea to use Razor to help create PDF templates.


Now sadly it's been a while since I played around with this but I have only just started getting back on top of things so here's how I've done it.


Components Used

My solution is to write HTML templates using the Razor syntax and then to make use of the Razor View Engine for filling in the dynamic data. Then for the PDF creation to make use of iTextSharp library to generating the PDF documents from the generated HTML.

iTextSharp is a free library that allows you to create PDF's using C#, unfortunatley its API is a bit of a pain to use natively so I have made use of Hugo Bonacci's HtmlToPdfBuilder.cs class to simply my interaction with the library.


The first thing to do is to generate the HTML markup for out PDF. So fire up a new RazorViewEngine and load the view (template) you wish to use. We load the view by using the view engines FindView method. This method requires a ControllerContext so if you are using in an MVC site you can pass the current context, which is how for my protoype I used it, or you need to pass a custom one.


 var frontPageData = new RazorViewEngine().FindView(this.ControllerContext, "Newsletter", "", false);

In my example I wanted my Newsletter template, I have decided to not use a master view or caching but in a real application you may want to enable view caching.


Next you need a view context, a view context also needs a controller context, the view you found previously, a viewdatadictionary, a tempdatadictionary and a textwriter. The viewdatadictionary should be used to pass a model to your view or viewdata items, these will then be used within the view to populated your dynamic data items. The textwriter will be the object that the final generated HTML will be written to and this is what we will use to generate the PDF from.


  var textwriter = new StringWriter();
        var dataDict = new ViewDataDictionary<NewsletterData>()
        {
            Model = new NewsletterData{ Name = "Mike" }
        };
        var context = new ViewContext(this.ControllerContext, frontPageData.View, dataDict, new TempDataDictionary(), textwriter);

The final thing we do to get our generated HTML is call the Render method on our view, this render method requires our viewcontext and the text writer.


frontPageData.View.Render(context, textwriter);

Now the HTML is generated we can make use of the HtmlToPdfBuilder class to take our HTML and generate a PDF document. This is as simple as creating a new builder with a provided pagesize, calling add page and then appending our HTML. Hugo's HtmlToPdfBuilder class expects an array of values to use in the HTML, as it expects the HTML to have placeholders in the String.Format fashion {0} {1} etc. We don't need to do this as we have already finalised our HTML, if this was going to be production code I would seriously consider not using the HtmlToPdfBuilder class and instead write one that only uses iTextSharp and the RazorViewEngine, however for this simple prototype it makes things alot simpler.


var myHtmlPdfBuilder = new HtmlToPdfBuilder(PageSize.A4_LANDSCAPE);
//add a new page
myHtmlPdfBuilder.AddPage();
//using the current page take our html data from our view and write it
myHtmlPdfBuilder[0].AppendHtml(data, new object[]{ });


Now the PDF has been generated we can call the RenderPDF method on our HtmlPdfBuilder class to get an array of bytes back that represent the PDF. We can then use this array and write it to disk as a PDF document or to a response stream to have a dynamic PDF page on a website.


It really is that simple, I hope this prototype shows you the power the RazorViewEngine gives you especially when you start combining its output with other third party libraries. You can download my protoype MVC 3 site with this in and have a poke. It includes a simple PDFActionResult along with the above code.


Update

Whilst writing this blog post up I have decided to roll this prototype into a proper component which should simplfy the above and not require the use of Hugo's class. I will blog about this component when it is finished and publish it on CodePlex or something similar.

Monday 31 January 2011

Using DeferredLoadListBox in a Pivot Control

Recently I've been using the DeferredLoadListBox It's a fantastic way of improving the performance of your list views.


I started using the DeferredLoadListBox within a Pivot control but occasionally found that the app would randomly crash throwing the following exception:

All containers must have a Height set (ex: via ItemContainerStyle), though the heights need not all need to be the same.
This usually means you haven't set the properly. However I had ensured I had set the style height. I then got the source and started poking around.


What I found was although in the UnmaskItemContent method the container had a height if you inspected the ActualHeight property this was 0. I did a bit of research and found the following on MSDN:

ActualWidth and ActualHeight are calculated based on the Width/Height property values and the layout system.  There is no guarantee as to when these values will be "calculated"
So the problem appeared to be with timing.


After contacting the very helpful David Anson and exchanging a few ideas on solving the issue I decided to implement a retry counter. The concept being the first two times we find the container height is 0 drop out of the unmask method and try again later. If we still don't have a height on the third attempt an error must have occurred.


Implementing the counter was very simple and the most important thing is that this simple retry counter allows time for the height to be calculated and work as expected. The only minor side effect of this counter is that you can notice a slight pause in the UI however as this exception doesn't occur everytime this seems fair enough.


Find the modified source below and I hope it can be of use to you.

// Copyright (C) Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the Microsoft Public License
// (Ms-PL, http://opensource.org/licenses/ms-pl.html).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace Delay
{
    /// 
    /// Implements a subclass of ListBox based on a StackPanel that defers the
    /// loading of off-screen items until necessary in order to minimize impact
    /// to the UI thread.
    /// 
    public class DeferredLoadListBox : ListBox
    {
        private enum OverlapKind { Overlap, ChildAbove, ChildBelow };

        private ScrollViewer _scrollViewer;
        private ItemContainerGenerator _generator;
        private bool _queuedUnmaskVisibleContent;
        private bool _inOnApplyTemplate;

        /// 
        /// Handles the application of the Control's Template.
        /// 
        public override void OnApplyTemplate()
        {
            // Unhook from old Template elements
            _inOnApplyTemplate = true;
            ClearValue(VerticalOffsetShadowProperty);
            _scrollViewer = null;
            _generator = null;

            // Apply new Template
            base.OnApplyTemplate();

            // Hook up to new Template elements
            _scrollViewer = FindFirstChildOfType(this);
            if (null == _scrollViewer)
            {
                throw new NotSupportedException("Control Template must include a ScrollViewer (wrapping ItemsHost).");
            }
            _generator = ItemContainerGenerator;
            SetBinding(VerticalOffsetShadowProperty, new Binding { Source = _scrollViewer, Path = new PropertyPath("VerticalOffset") });
            _inOnApplyTemplate = false;
        }

        /// 
        /// Determines if the specified item is (or is eligible to be) its own item container.
        /// 
        /// The specified item.
        /// true if the item is its own item container; otherwise, false.
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            // Check container type
            return item is DeferredLoadListBoxItem;
        }

        /// 
        /// Creates or identifies the element used to display a specified item.
        /// 
        /// A DeferredLoadListBoxItem corresponding to a specified item.
        protected override DependencyObject GetContainerForItemOverride()
        {
            // Create container (matches ListBox implementation)
            var item = new DeferredLoadListBoxItem();
            if (ItemContainerStyle != null)
            {
                item.Style = ItemContainerStyle;
            }
            return item;
        }

        /// 
        /// Prepares the specified element to display the specified item.
        /// 
        /// The element used to display the specified item.
        /// The item to display.
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            // Perform base class preparation
            base.PrepareContainerForItemOverride(element, item);

            // Mask the container's content
            var container = (DeferredLoadListBoxItem)element;
            if (!DesignerProperties.IsInDesignTool)
            {
                container.MaskContent();
            }

            // Queue a (single) pass to unmask newly visible content on the next tick
            if (!_queuedUnmaskVisibleContent)
            {
                _queuedUnmaskVisibleContent = true;
                Dispatcher.BeginInvoke(() =>
                {
                    _queuedUnmaskVisibleContent = false;
                    UnmaskVisibleContent();
                });
            }
        }

        private static readonly DependencyProperty VerticalOffsetShadowProperty =
            DependencyProperty.Register("VerticalOffsetShadow", typeof(double), typeof(DeferredLoadListBox), new PropertyMetadata(-1.0, OnVerticalOffsetShadowChanged));
        private static void OnVerticalOffsetShadowChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            // Handle ScrollViewer VerticalOffset change by unmasking newly visible content
            ((DeferredLoadListBox)o).UnmaskVisibleContent();
        }

        private void UnmaskVisibleContent()
        {
            // Capture variables
            var count = Items.Count;

            // Find index of any container within view using (1-indexed) binary search
            var index = -1;
            var l = 0;
            var r = count + 1;
            while (-1 == index)
            {
                var p = (r - l) / 2;
                if (0 == p)
                {
                    break;
                }
                p += l;
                var c = (DeferredLoadListBoxItem)_generator.ContainerFromIndex(p - 1);
                if (null == c)
                {
                    if (_inOnApplyTemplate)
                    {
                        // Applying template; don't expect to have containers at this point
                        return;
                    }
                    // Should always be able to get the container
                    var presenter = FindFirstChildOfType(_scrollViewer);
                    var panel = (null == presenter) ? null : FindFirstChildOfType(presenter);
                    if (panel is VirtualizingStackPanel)
                    {
                        throw new NotSupportedException("Must change ItemsPanel to be a StackPanel (via the ItemsPanel property).");
                    }
                    else
                    {
                        throw new NotSupportedException("Couldn't find container for item (ItemsPanel should be a StackPanel).");
                    }
                }
                switch (Overlap(_scrollViewer, c, 0))
                {
                    case OverlapKind.Overlap:
                        index = p - 1;
                        break;
                    case OverlapKind.ChildAbove:
                        l = p;
                        break;
                    case OverlapKind.ChildBelow:
                        r = p;
                        break;
                }
            }

            if (-1 != index)
            {
                // Unmask visible items below the current item
                for (var i = index; i < count; i++)
                {
                    if (!UnmaskItemContent(i))
                    {
                        break;
                    }
                }

                // Unmask visible items above the current item
                for (var i = index - 1; 0 <= i; i--)
                {
                    if (!UnmaskItemContent(i))
                    {
                        break;
                    }
                }
            }
        }

        private bool UnmaskItemContent(int index)
        {
            var container = (DeferredLoadListBoxItem)_generator.ContainerFromIndex(index);
            if (null != container)
            {
                // Return quickly if not masked (but periodically check visibility anyway so we can stop once we're out of range)
                if (!container.Masked && (0 != (index % 16)))
                {
                    return true;
                }
                // Check necessary conditions
                if (0 == container.Height)
                {
                    throw new NotSupportedException("All containers must have a Height set (ex: via ItemContainerStyle), though the heights need not all need to be the same.");
                }
                // If container overlaps the "visible" area (i.e. on or near the screen), unmask it
                if (OverlapKind.Overlap == Overlap(_scrollViewer, container, 2 * _scrollViewer.ActualHeight))
                {
                    container.UnmaskContent();
                    return true;
                }
            }
            return false;
        }

        private static bool Overlap(double startA, double endA, double startB, double endB)
        {
            return (((startA <= startB) && (startB <= endA)) ||
                    ((startB <= startA) && (startA <= endB)));
        }

        private static OverlapKind Overlap(FrameworkElement parent, FrameworkElement child, double padding)
        {
            // Get child bounds relative to parent
            var transform = child.TransformToVisual(parent);
            var bounds = new Rect(transform.Transform(new Point()), transform.Transform(new Point(/*child.ActualWidth*/ 0, child.ActualHeight)));
            // Return kind of overlap
            if (Overlap(0 - padding, parent.ActualHeight + padding, bounds.Top, bounds.Bottom))
            {
                return OverlapKind.Overlap;
            }
            else if (bounds.Top < 0)
            {
                return OverlapKind.ChildAbove;
            }
            else
            {
                return OverlapKind.ChildBelow;
            }
        }

        private static T FindFirstChildOfType(DependencyObject root) where T : class
        {
            // Enqueue root node
            var queue = new Queue();
            queue.Enqueue(root);
            while (0 < queue.Count)
            {
                // Dequeue next node and check its children
                var current = queue.Dequeue();
                for (var i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
                {
                    var child = VisualTreeHelper.GetChild(current, i);
                    var typedChild = child as T;
                    if (null != typedChild)
                    {
                        return typedChild;
                    }
                    // Enqueue child
                    queue.Enqueue(child);
                }
            }
            // No children match
            return null;
        }
    }
}

Saturday 22 January 2011

Windows Phone 7: Security Exception on deactivate

This morning I have been finishing the tombstoning part of one of my Windows Phone applications I have been developing. Whilst testing I found that when the application deactivated or terminated that a SecurityException was thrown. Looking at the stack trace I noticed that it was occurring whilst trying to serialise some data to the Applications State store.


Now I knew I was putting some data into state so this wasn't to hard to find however, when I looked at what I was putting into state it was nothing more complicated than a custom type that exposed a collection of POCO classes. Why would this cause a security exception.


I decided to do a simple Google search for the exception "windows phone 7 security exception" and the second result looked similar: "c# - SecurityException was unhandled when using isolated storage" [http://stackoverflow.com/questions/4209280/securityexception-was-unhandled-when-using-isolated-storage]. So I had a look, and guessed that if you can't put internal classes into IsolatedStorage you also can't do it for ApplicationState, which makes sense when you think about it. So I made my type public and not internal and all was good in the world again.


Another week another tip.... I wonder what I will find out next week

Saturday 15 January 2011

Windows Phone Development and Designer Exceptions

I've been building a few Windows Phone applications recently and have been learning lot's along the way and am hopefully releasing a couple of these in the near future. However on my current project I have been constantly hounded by the designer view of my xaml pages throwing exceptions.


Tonight I decided to finally look into why these occur, I have managed to ignore them previously due to the weird nature of them. My exception scenario consists of a page that contains a pivot control and inside one of the pivot items there is a user control. The user control contains a simple list view which has some data bound to it

Now I have always got past the exceptions as if you use the designer view for the user control it works fine, however as soon as I put it into a page or a pivot the designer starts kicking off.


The actual exception is as follows:

Could not load type 'System.Net.HttpUtility' from assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
It then goes on to tell me the line number and location etc of this. At first glance this exception looks a bit weird as in the code view and if you run the application the exception is never thrown.

The first thing I did to try and resolve this issue was to find where System.Net.HttpUtility lives, in the desktop version of Silverlight it lives in System.Windows.Browser But as the Windows Phone version of Silverlight doesn't include System.Windows.Browser the development team moved the httputility type into the System.Net namespace and put it into the System.Windows assembly, http://msdn.microsoft.com/en-us/library/dd470087(v=vs.95).aspx#Assemblies. This explains why the project builds and runs etc, however it doesn't explain why the designer has such a bad time with it.


Sadly I couldn't find a source or information on this exception, my guess is the designer is loading an older version of the assembly, a missing designer tool update from the RTM or something similar {Edit see my update below}. However what I have found is that Silverlight does have another way of encoding and decoding URL strings. System.Uri.EscapeUriString, if you use this the phone and the designer all work happily. A minor change and a headache eased :) I'm hoping the next tools update fixes the issue but until then this seems the safest method of escaping url strings.

Update

As I oddly found this issue intriguing I decided to investigate further. I opened another Visual Studio attached it to the Visual Studio process I had my project in and turned on catch all exceptions. Then when the exception was thrown I looked at the AppDomains assembly list, System.AppDomain.CurrentDomain.GetAssemblies(), 55 in all were loaded. I then looked through this list for the assembly referenced in the exception. This assembly then had the following location value: Location "c:\\Program Files (x86)\\Microsoft Silverlight\\4.0.51204.0\\System.Windows.dll". This is the main Silverlight 4 runtime, NOT the Windows Phone version which is 3.7.x, to be certain I then opened the assembly in Reflector to be sure the System.Net.HttpUtility was missing and it was.

So as suspected the designer is loading the wrong assembly thus the error, the assembly it should be loading is in the referenced assemblies folder, C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone\System.Windows.dll , hopefully reporting this on Connect will result in a hotfix / update.