How to host and consume WCF RESTful Services

There’s been a lot of jazz about RESTful services, which was published to the masses by a person named Roy Thomas Fielding in his PhD dissertation. Since then, RESTful services have been embraced and widely used. To name a few, Twitter and Facebook exposes RESTful web services API to us to consume. I shall not go into the principles and concepts of REST, but I personally find RESTful services to be very natural, easy to understand and it just flows with how the World Wide Web works, which is driven by the HTTP protocol. You can learn more about RESTful principles from wikipedia, or just google it. It’s very much the opposite of using SOAP, which has been the Microsoft way for some time now, but in .NET 3.5, REST has found its way into WCF.

Now back to business, let’s talk about how to create a RESTful service using WCF. If you search google, you will find heaps of examples how to do create WCF RESTful services, but there’s been very little information out there about how to consume it. I found some examples about using WCF ChannelFactory and generating a client proxy to consume the WCF RESTful service, but I feel these approaches violates the whole point of using REST. In order to consume a RESTful service, regardless of how it’s hosted, we should just consume it using simple HTTP requests generated from code. In this post, I will show you how to create, host and consume the services with WCF and the .NET framework.

Read the rest of this entry »

Posted in WCF. Tags: . 5 Comments »

Article: JQuery with ASP.NET MVC

I came across another great JQuery article on MSDN magazine, with great examples used ASP.NET MVC framework. It’s definitely worth a read!

Read the JQuery article here.

Article: Microsoft Patterns and Practices

I came across a good article on MSDN that talks about the separation of concerns using Enterprise Library (now at 4.1), using the Object Builder framework and Unity Application Block for Dependency Injection/Inversion of Control, with examples of incorporating it into Model View Presenter (MVP) pattern.

With quite a number of open source frameworks (like Castle Windsor) out there that do these already, it’s refreshing to see that MS is churning out their own pattern frameworks for .NET Development.

Read article here!

Tips/Tricks: Importing Visual Studio 2008 item and project templates

My colleague was having problems today importing a project template into Visual Studio 2008. He was setting up a Virtual Machine, which has some guidance package install, but the project templates were not showing up.

If you are encountering similar issues, this article on MSDN should help heaps.

How to: Locate and Organize Project and Item Templates.

Note: Remember to zip up your .vstemplate file, plus other necessary files, e.g. .csproj, .cs, etc  files. VS 2008 will recognize it only if it’s in a .zip file.

WPF Commands: Better approach to using ICommandSource

I originally wanted to write a post about using creating Command Source controls using ICommandSource but after looking at it on MSDN, I found quite a number of limitations with this approach…namely,

  • You need to create a custom control for every one that does not support taking a command source natively.
  • You can only bind one command at a time.
  • Your number of custom controls could grow fairly large very quickly, which makes maintenance of the code a horror, and not scalable.
  • The ICommandSource implementation code will be repeated in most of your custom controls, which is not a good option. I have thought about abstracting that out using a base class (that implements ICommandSource), but the problem is that your custom control needs to inherit the original WPF control (e.g. Slider, ListView), and multiple inheritance is not allowed in C#, so there goes my idea.

Better Approach

I pondered to myself….there has to be a better way to do this, and after much searching, I found a great article which suggest an alternate and better approach. I came across the solution in a blog post (WPF Commands Everywhere) by Tomer Shaman. The idea behind it is by using custom-written Command Source Trigger classes and using the WPF Attached Properties mechanism. This allows us to define the Command Source Triggers in XAML, and the author introduces two kinds of triggers, one for Routed Events and one for Property Changed. When a routed event is triggered or property value is changed, the Command that is hooked up will be triggered, essentially emulating the ICommandSource behaviour, but without all the messy/repetitive code. This is a snipplet of the XAML code from Tomer’s blog post.

<ListView x:Name="list"
		  ItemsSource="{Binding Emails}"
			<ts:EventCommandTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonUp"
									Command="{Binding Path=DownloadEmail}"
									CustomParameter="{Binding ElementName=list, Path=SelectedValue}" />

			<ts:EventCommandTrigger RoutedEvent="UIElement.PreviewMouseRightButtonUp"
									Command="{Binding Path=MarkAsRead}"
									CustomParameter="{Binding ElementName=list, Path=SelectedValue}" />

			<ts:EventCommandTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonDown"
									Command="{Binding Path=OpenEmail}"
									CustomParameter="{Binding ElementName=list, Path=SelectedValue}" />
<Expander IsExpanded="{Binding Path=DummyProperty}" Header="Contact">
		<ts:PropertyCommandTrigger Property="Expander.IsExpanded"
					   Command="{Binding Path=DownloadContact, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />

In the code behind, the Commands (DownloadEmail, MarkAsRead, OpenEmail) are instantiated and exposed with public getters for databinding in XAML. I strongly suggest that you go over to Tomer’s blog post and have a read, and download the full code for this implementation. It will start to make sense once you start digging into it.

I personally feel that this implementation is much cleaner, reusable and just brilliant. If you agree, do go over to Tomer’s blog and leave him a ‘thank you’ or comment, I did. 🙂

Share this post:
Posted in WPF. Tags: . Leave a Comment »

How to use DeepZoom in your website

Today a good friend asked me about how to use DeepZoom feature in your own website, so I figured this could be a good blog post. I had previously fiddled around with DeepZoom with Silverlight 1.1, but now that some time has passed…Silverlight 2 has been released, and DeepZoom has been enhanced. Here are my findings:

Read the rest of this entry »

Articles: Good JQuery articles

Here are 2 links to good JQuery overviews on MSDN Magazine. They are pretty extensive, good to read for someone starting out in JQuery.

Article 1

Article 2

Posted in JQuery. Tags: . Leave a Comment »