Using KnockoutJS with SignalR in ASP.NET MVC

KnockoutJS is a MVVM implementation for JavaScript written by Steve Sanderson, in my opinion the author of the best ASP.NET MVC textbooks available. Simply put it lets you bind a JavaScript object model to your HTML UI using a Read more

A MongoDB Tutorial using C# and ASP.NET MVC

In this post I'm going to create a simple ASP.NET MVC website for a simple blog that uses MongoDB and the offical 10gen C# driver. MongoDB is no NOSQL database that stores information as Binary JSON (BSON) in documents. I Read more

Linq To SQL Tutorial

Check out some of my other Linq to SQL posts: EntityBase Inheritance Modifiers with SQLMetal Linq to SQL with WCF Services Linq to SQL Framework (Repository/Business wrapper) ObjectDataSource binding with server side paging and sorting Load Options Generic Framework using reflection This is a basic tutorial for Read more

Alert, confirm and input message boxes in Silverlight

Posted on by Joe in Silverlight | Leave a comment

To use alert, confirm and input message boxes in Silverlight you need to import the following namespace.

using System.Windows.Browser;

You can then show an alert box by doing…

HtmlPage.Window.Alert("This is an alert")

Or a confirm dialog by doing…

if (HtmlPage.Window.Confirm("Are you sure you want to do something?"))
{
    //Do something
}

Or an input prompt by doing…

string name = HtmlPage.Window.Prompt("What is your name?");

Passing a byte array to a WCF method from Silverlight – The remote server returned an error: NotFound

Posted on by Joe in Silverlight | 8 Comments

My Silverlight application calls a method to get an image from the server as a byte array. When adding my Service Reference the ServiceReferences.ClientConfig automatically generated the binding’s MaxBufferSize and MaxRecievedMessageSize as 2147483647 which is a lot bigger than I want to I changed them to 5242880 (5MB).  When the application calls the service to bring down an image these values are used to limit the size of the response message.

I also set the MaxBufferSize and MaxRecievedMessageSize to 5242880 in the web.config file of the web application hosting my service.  At this point when I call the method in my service to upload an image passing a byte array I get a The remote server returned an error: NotFound error message, which really isn’t very helpful.

In this instance the problem is that the maximum array length for the XML reader has been exceeded. This can be fixed by expanding the binding section to insert a readerQuotas section and increase the maxArrayLength.

<services>
    <service behaviorConfiguration="GalleryServiceBehavior" name="GalleryService.GalleryService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpLargeMessage" contract="GalleryService.IGalleryService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>
</services>

<bindings>
    <basicHttpBinding>
        <binding name="basicHttpLargeMessage" maxReceivedMessageSize="5242880" maxBufferSize="5242880">
            <readerQuotas maxArrayLength="5242880" />
        </binding>
    </basicHttpBinding>
</bindings>

I can now upload and download files up to 5MB through my service.

Set Silverlight Image Source from byte array

Posted on by Joe in Silverlight | 12 Comments

I’ve been playing around with Silverlight and WCF services and I came across the need to set the Source of an Image control in Silverlight from a byte array. I have a service method which returns an image from the server as a byte array which I want to show in the Silverlight application.

My service method is simple and looks like this:

using System.IO;

public byte[] GetImage(string serverPath)
{
    return File.ReadAllBytes(serverPath);
}

In the Silverlight application I create a MemoryStream from the byte array, then a BitmapImage from the MemoryStream and set the image source to the BitmapImage.


using System.IO;
using System.Windows.Media.Imaging;

void proxy_GetImageCompleted(object sender, GetImageCompletedEventArgs e)
{
    MemoryStream stream = new MemoryStream(e.Result);
    BitmapImage b = new BitmapImage();
    b.SetSource(stream);
    imgImage.Source = b;
}

proxy_GetImageCompleted is the event handler for when my asynchronous call to GetImage completes, where e.Result is the byte array.

Wrapped Listbox items in Silverlight and WPF

Posted on by Joe in Silverlight, WCF | Leave a comment

I wanted to display a list of image thumbnails similar to how Windows Explorer does it so they are displayed horizontally and wrap onto the next line. The ListBox allows you to choose a template for the panel used to display items. The following XAML uses the WrapPanel for the ItemsPanelTemplate and gives the functionality I wanted.

<listBox x:Name="ImageList" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <listBox.ItemTemplate>
        <dataTemplate>
            <usrcntrl:Image ImageName="{Binding Name}" ServerPath="{Binding ServerPath}" />
        </dataTemplate>
    </listBox.ItemTemplate>
    <listBox.ItemsPanel>
        <itemsPanelTemplate>
            <controls:WrapPanel />
        </itemsPanelTemplate>
    </listBox.ItemsPanel>
</listBox>

I have also disabled the horizontal scroll bar so the images wrap to the next line.

Visual Studio closing when trying to choose items for the toolbox

Posted on by Joe in Visual Studio 2008 | Leave a comment

The other day I was trying to add new items to my toolbox in Visual Studio 2008 and each time Visual Studio would close itself without reason. I tried the Reset Toolbox option which didn’t solve the problem.

I then tried running Visual Studio in safe mode by choosing run from the start menu and typing ‘devenv /safemode’. In safe mode I was able to get the Choose Items dialog so I just flicked through all the tabs to make sure it was working. I then closed Visual Studio and loaded it normally and everything worked as expected.

Not sure why it happened but this seemed to fix it :)

WCF Services and Silverlight

Posted on by Joe in Silverlight, WCF | Leave a comment

After looking at WPF the other week I thought being a web developer it would be beneficial to look at Silverlight.  One of the first things I was interested to try was to access the server’s file system from the Silverlight application. I quickly found out that this wasn’t possible so I decided to create a WCF service hosted in the web application running my Silverlight application to access the file system for me.

To use a WCF service in Silverlight it must use basicHttpBinding. In Visual Studio you can add a ‘Silverlight-enabled WCF Service’ from the Silverlight category which creates a service using basicHttpBinding as default.

I created a simple service method called GetDirectories which returns an array of directory names within a given location.

 

public string[] GetDirectories()
{
    string path =  ConfigurationManager.AppSettings["GalleryDir"];
    IList<string> directories = new List<string>();

    foreach (string directory in Directory.GetDirectories(path))
        directories.Add(directory);

    return directories.ToArray();
}

After adding my service reference and creating an instance of my proxy I noticed that it did not contain the method GetDirectories. Instead I have GetDirectoriesAsync and an event called GetDirectoriesCompleted. This is cool as all service calls with Silverlight are asynchronous.

 

public Directories()
{
    InitializeComponent();

    GalleryContractClient proxy = new GalleryContractClient();
    proxy.GetDirectoriesCompleted += new EventHandler<getDirectoriesCompletedEventArgs>(proxy_GetDirectoriesCompleted);
    proxy.GetDirectoriesAsync();
}

void proxy_GetDirectoriesCompleted(object sender, GetDirectoriesCompletedEventArgs e)
{
    foreach (string directory in e.Result)
        tvDirectories.Items.Add(directory);
}

The Result property of GetDirectoriesCompleteEventArgs has the response from the request.

WPF Community Workshop – Sydney

Posted on by Joe in Personal | Leave a comment

Last weekend (23rd May) I attended the WPF Community Workshop in Sydney put on by Microsoft and run by a colleague of mine, Deepak Kapoor.

I had never looked into WPF as I am predominantly an ASP.NET developer although I did use Windows Forms for several years. I thought this would be a good opportunity to see what WPF was all about and I was impressed.

The workshop featured several labs which quickly taught the basics of WPF and XAML and went on to data binding.

We also looked at Expression Blendwhich is written with WPF. I really liked how easy it was to change the appearance of the standard controls and make them look however you want.

Will definitely be looking further into WPF in the near future…

The beast that is WCF

Posted on by Joe in WCF | Leave a comment

After spending nearly 3 years working some time ago on an application utilising Web Services with .NET 2.0 I thought it was about time I learned about WCF. I am currently reading the book Learning WCF by Michele Leroux Bustamante which so far has done a really good job describing the concepts of WCF, also including some good labs. WCF seems pretty powerful, but as the title of this post suggests, is quite a beast!

Blog me up

Posted on by Joe in Personal | Leave a comment

Over the time I’ve been using Microsoft .NET technologies I’ve often come across something I’ve done where I’ve thought, “If I had a blog I’d write a post about this”. I never got around to it before but after speaking with a guy at work I decided it was a good idea to start one. The only problem now is thinking of stuff to put on it…