| Subcribe via RSS

Creating a new post in Wordpress using the JoeBlogs library

October 12th, 2009 | 5 Comments | Posted in Development

A few people have recently been asking how the NewPost method works within JoeBlogs

First, you need to create an instance of Post.

Then, set the following properties:

dateCreated
Fairly self explanatory, but you should set this to today’s date (or whatever date you wish the post to be set as published)

title
The title of the post

description
The body of the post.
This can of course contain HTML

categories
This is a string array of categories to associate with the post

mt_keywords
Another string array, representing the tags for the post

Then, using your presumably already instantiated Wrapper class, you can call the NewPost method, which takes the above Post object as a parameter, and a boolean – indicating if the post is to be set as published. Note – if this is set to false, the post is set in draft mode.

Here’s some sample code:

//create a new post
var post = new Post();

//since this is a struct, we can't have a constructor that does this!
post.dateCreated=DateTime.Now;
post.title="This is a title";
post.description="this is the body of the post. it <strong>could</strong> be html.";

//create the post!
wp.NewPost(post,true);

Hope this helps!

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: , ,

NVelocity template with decimal to two decimal places

October 2nd, 2009 | No Comments | Posted in Development

I wanted to be able to output a decimal value from an object in my NVelocity template.

For example, the value of the decimal was: 3.40000 to represent 3.4.

The end result:

Total Order Value: 3.40 GBP

I needed to display this as currency format. Sure, I could of used this String.Format method, but that adds £ or $ or whatever (depending on your environment setup) to the start of the resulting string.

In the end, I used ToString with “N2” formatter.

So, in my nVelocity template, here is what I would use:

Total Order Value: $Order.GrandTotal.ToString("N2") GBP

There are some other alternatives discussed here:
http://stackoverflow.com/questions/1048643/format-a-double-value-like-currency-but-without-the-currency-sign-c

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: ,

C# Ternary conditional operator – Question mark in an if statement

September 9th, 2009 | No Comments | Posted in Development

I’m all for shortening the amount of code we write, if it makes it more readable.

One enhancement I make use of in C# is the ternary conditional operator.

Consider the following block of code:

            string input = "hi";
            bool saidHello;

            if (input = "hello")
            {
                saidHello = true;
            }
            else
            {
                saidHello = false;
            }

In this example, we can see, the saidHello boolean value would be false,  as the input string actually said ‘hi’ rather than hello.

This could be shortened down to a single line like so (or 2, if we count the input initialisation)

            string input = "hi";
            var saidHello = input == "hello" ? true : false;

This makes use of the ternary operator – the ? (and of course var – as explained in another article)

the ? acts as a ‘then’ and the : acts as an else…

So you can think of it as something like below:

saidHello equals (if input equals hello then) true, else false.

These can of course be nested, as explained in this article, however in my opinion, this kind of defeats the whole point of this from my point of view – to increase readability of code.

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: ,

JoeBlogs – Typo in dummy URL fixed

August 31st, 2009 | 1 Comment | Posted in Development

I’ve recently had a few comments that the JoeBlogs wrapper wasn’t working – and they were getting an “invalid response” or more specifically “Response XML not valid XML-RPC – missing methodResponse element.”

It seems that I had a typo in my comments in JoeBlogs.TestHarness/Program.cs on line 15:

//typically http://www.yourdomain.com/xmlprc.php (if your wordpress blog is installed in root dir)

The problem, is the xmlprc.php – this should be xmlrpc.php.

Those of you that copy pasted / uncommented out that line, would be executing requests against a file that didn’t exist on your server!

I’ve changed this typo, and committed to Codeplex: revision #27138

Thanks to Felix for pointing this out!

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: ,

Reverse GeoCoding with Google API and C#

August 9th, 2009 | 1 Comment | Posted in Development

I needed to write a function to get a set of co-ordinates from an address, supplied as a string.
Using the Google API, I came up with this:

First, I created a struct, to represent the lat and lon co-ordinates:

public struct Coordinate
{
	private double _latitude;
	private double _longitude;

	public Coordinate(double latitude, double longitude)
	{
		_latitude = latitude;
		_longitude = longitude;
	}

	public double Latitude { get { return _latitude; } set { _latitude = value; } }
	public double Longitude { get { return _longitude; } set { _longitude = value; } }
}

This is a simple representation of a set of latitude and longitude co-ordinates.

The main method, which returns the above Coordinate struct is:

public static Coordinate GetCoordinates(string address)
{
	using (var client = new WebClient())
	{
		Uri uri = YOUR_URI_HERE;

		/* The first number is the status code,
		* the second is the accuracy,
		* the third is the latitude,
		* the fourth one is the longitude.
		*/
		string[] geocodeInfo = client.DownloadString(uri)		.Split(',');

		return new Coordinate(
			Convert.ToDouble(geocodeInfo[2]),
			Convert.ToDouble(geocodeInfo[3]));
	}
}

On line 5, you need to specify your Google API URL, in the format of:

http://maps.google.com/maps/geo?q=ADDRESS GOES HERE&output=csv&key=YOUR KEY HERE

And that’s it!

Obviously, there are usage restrictions when using the Google API for this, as outlined on the documentation pages

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: , ,

Windows Azure Pricing Announced

July 15th, 2009 | No Comments | Posted in Development

Microsoft has finally released their pricing model for the forthcoming Windows Azure

Windows Azure:

  • Compute = $0.12 / hour
  • Storage = $0.15 / GB stored / month
  • Storage Transactions = $0.01 / 10K
  • Bandwidth = $0.10 in / $0.15 out / GB

SQL Azure:

  • Web Edition – Up to 1 GB relational database = $9.99
  • Business Edition – Up to 10 GB relational database = $99.99
  • Bandwidth = $0.10 in / $0.15 out / GB

.Net Services:

  • Messages = $0.15/100K message operations , including Service Bus messages and Access Control tokens
  • Bandwidth = $0.10 in / $0.15 out / GB

Further details available from:
http://www.microsoft.com/azure/pricing.mspx

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags:

Why Are Interfaces Useful?

July 6th, 2009 | No Comments | Posted in Development

Interfaces, usually prefixed by “I” are useful in software engineering, for a number of reasons.
Primarily, they allow you to create “pluggable” code.
By this, I mean that your code is easier to manage, easier to maintain, easier to change the way certain parts of your application work, without changing the entire way it works.

Lets look at an example.

Our application is an photo sharing web application, allowing users to view and upload photos.
The main functions of the site, are handled by our ImageManager class. This facilitates the retrieval, and storing of images. Fairly integral to the application.

As we are designing our application using SoC (Separation of Concerns), all our ImageManager class will do, is get, or put images. (in reality, we should really have a separate concern for each operation – get & put)
It doesn’t care about storing user details or meta data, against the image in a database, nor does it care about checking to see if a user can view that image or anything like that.
It purely puts, or gets, an image.

Now. this operation sounds simple, to get an image, I just need to connect to my file store, and retrieve my image?
Yes. But what if, one day, you need to be able to switch where you store your images.
For example..on day 1, you are storing your images on the file system, in a directory within your web application project.
This works fine, especially during beta testing, however as demand grows, the strain on your server and your bandwidth become a problem, and you need to change where images are stored.

You also want to be able to use a storage service, such as Amazon S3 to store your images.
(I’m not going to go into how S3 or any other service works in this article, it’s just an example.)

Luckily, we have a couple of ImageManagers….
FileSystemImageManager, and S3ImageManager

Both of which, implement our interface – IImageManager (notice the prefix "I”)

Here’s the interface:

C#

public interface IImageManager
{
  void PutImage(Image image, string fileName);
  Image GetImage(string fileName);
}

VB

Public Interface IImageManager
  Sub PutImage(ByVal image As Image, ByVal fileName As String)
  Function GetImage(ByVal fileName As String) As Image
End Interface

Note the empty method signatures…

Interfaces contain no logic – they are simply a contract that each implementation agrees to follow.

So lets have a look at an implementation of IImageManager:

C#

public class FileSystemImageManager : IImageManager
{
  public void PutImage(Image image, string fileName)
  {
    //code to save the image on the file system
  }

  public Image GetImage(string fileName)
  {
    //code to get the image from the file system
  }
}

VB

Public Class FileSystemImageManager
    Implements IImageManager
    Public Sub PutImage(ByVal image As Image, ByVal fileName As String)
        'code to save the image on the file system
    End Sub

    Public Function GetImage(ByVal fileName As String) As Image
        'code to get the image from the file system
    End Function
End Class

The FileSystemImageManager implements IImageManager (indicated by the : in c#)

note, in visual studio, you can right on your interface implementation declaration and select Implement Interface – this will create the required method signatures within your class.

Implement Interface

Our class now conforms to IImageManager.

But – there’s no code! That doesnt technically matter.

As long as you’ve implemented each method (even by throwing a NotImplementedException), your class will compile.

So what’s the point?

Well, we now have a FileSystemImageManager class, that (after you’ve added your code) gets and puts images on the file system.

In our application, we can do something like:

C#

      //get the image:
      IImageManager imageManager = new FileSystemImage();
      var image = imageManager.GetImage("my_filename");

VB

'get the image:
Dim imageManager As IImageManager = New FileSystemImage()
Dim image = imageManager.GetImage("my_filename")

“Great” you say….

But that means you can “swap out” your functionality, really easily.

Instead of declaring imageManager as a new FileSystemImage() we could have another class, that implements IImageManager called “FlickrImage”

This could then use Flickr to get and put images (obviously additional functionality would be required to authenticate against Flickr API)

I’ll expand this tutorial at a later stage to explain how to effectively use config, factory patterns, IoC etc…

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: , , ,

Missing Files from DotNetNuke 4.9.4 Starter Kit causing error

July 2nd, 2009 | No Comments | Posted in Development

I downloaded the latest release of the Dot Net Nuke Starter Kit (Version 4.9.4  released on 23rd June 2009)

I followed the instructions to install the Visual Studio templates etc… as found here
They are a little outdated, but the same process still applies.

Upon installing it, loading a new website, I selected the DotNetNuke Web Application Framework (pictured below)

DotNetNuke Web Application Framework

The template began loading, and after a short while, two errors popped up:

The file Blog_03.05.00_Install.resources could not be found within the project templates.
Continuing to run, but the resulting project may not build properly.

The file ActiveDirectory_01.00.05_install.resources could not be found within the project templates.
Continuing to run, but the resulting project may not build properly.

(see below)

The file Blog_03.05.00_Install.resources could not be found within the project templates.  Continuing to run, but the resulting project may not build properly.

The file ActiveDirectory_01.00.05_install.resources could not be found within the project templates.  Continuing to run, but the resulting project may not build properly.

Of course, I Googled the error.
It seems other people are having the same problem as me – here and here.

That got me thinking – it must be something actually missing from this release of the template.

I opened my Visual Studio Templates Directory > Project Templates > Visual Web Developer > Visual Basic
In there, there is a DotNetNuke.zip

To make poking around the files easier, I unzipped it to my desktop.

I opened up DotNetNuke.vstemplate in Notepad2 (but of course opening in VS would also work)

On lines 1186 and 1210 of this file, we see the problem:

Line 1186 DotNetNuke.vstemplate

Line 1210 DotNetNuke.vstemplate

If you open up the folder Install \ Package you will notice 4 files:

  • CardSpace_01.00.00_Install.resources
  • LiveID_01.00.00_Install.zip
  • OpenID_01.00.00_Install.zip
  • PlaceHolder.txt

Notice the missing ActiveDirectory_01.00.05_install.resources – yet it is specified in the DotNetNuke.vstemplate!

The same goes for Install \ Module
Again, a bunch of files are listed, however the missing Blog_03.05.00_Install.resources is not there!

This is obviously an error on the release.

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: ,

Base checksum mismatch – A solution for TortoiseSVN users.

May 1st, 2009 | No Comments | Posted in Development, Geek Speak, Version Control

I recently had the misfortune to get a Base Checksum Mismatch error, when I tried to check in files to my repository.
The problem occurs when the files svn data has become corrupted.
There are many solutions to this problem, but here’s how I solved it simply (I only had 2 files that had this error)

  1. Check in everything except the files that are causing the issue.
  2. Take a fresh checkout to a separate directory
    (right click in a directory -> SVN Checkout)
    Screen should look like the image below:

    image

  3. After TortoiseSVN has checked out everything to the new directory, simply copy across your files that were giving you trouble
  4. Check back in.

That’s how I fixed it.
Hope this helps!

VN:F [1.8.2_1042]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: , ,

Windows Azure – Firefox & “Port Restricted for Security Reasons”

April 12th, 2009 | 5 Comments | Posted in Development

I’ve been starting to delve into the world of Windows Azure this week.

After getting the Azure Samples loaded, I tried running one of them (just by hitting F5 in VS) and, as Firefox is my default browser, got a “Port Restricted For Security Reasons” error.

Of course, it worked fine in IE

Here is what to do –

It turns out, that my application was trying to use port 87 on 127.0.0.1 (127.0.0.1:87)

Load up Firefox. Type about:config in the address bar, and hit enter. You will get something like the following:

image

Right click, and click New -> String.

Set the preference name to: network.security.ports.banned.override

Next, set the value to: 87 (or whatever port your app is using)

Done!

VN:F [1.8.2_1042]
Rating: 10.0/10 (1 vote cast)
VN:F [1.8.2_1042]
Rating: 0 (from 0 votes)
Tags: ,