Transform System.Drawing.ContentAlignment property to System.Drawing. StringFormat Alignment property

Recently we had the need to transform System.Drawing.ContentAlignment property to System.Drawing. Stringformat alignment property for creating a Graphic object with text drawn on it inside a given rectangle. Assuming that you want the text printed from left to right, you can use the ContentAlignment Enum values to obtain the desired StringAlignment Enum value. For example:

[…]

Adding SQLite to a Windows Mobile Application

Using SQLite in a Windows Mobile Application provides a simple way to add pre-populated data for consumption by the application.  There are a couple of configuration considerations when adding the database to the project.
1. Adding reference to SQLite.dll. After installing SQLite there will be a Compact Framework folder in the following path :C:\ProgramFiles\SQLite.NET\bin.  This folder […]

Unit testing: COM object that has been separated from its underlying RCW cannot be used

Unit testing: COM object that has been separated from its underlying RCW cannot be used

Let’s say you’re writing unit tests for code which uses COM objects. You want some initialization and some cleanup to be done. So you set up your test:

private static MyStore store;

[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
    store = new MyStore("some value");
}

[ClassCleanup]
public static void MyClassCleanup()
{
    store.DoWork(someArgument);
}

However, as your ‘store’ instance uses a COM object, you will get a test run error, saying: “System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used..”.

The way to solve this issue is to open up your testrunconfig file using the XML editor and adding the highlighted element to it:

<?xml version="1.0" encoding="UTF-8"?>
<TestRunConfiguration name="Local Test Run" id="fbbfe81f-97fa-4e33-bde1-00b3827b933e" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
  <ExecutionThread apartmentState="MTA" />
  <Description>This is a default test run configuration for a local test run.</Description>
  <TestTypeSpecific />
</TestRunConfiguration>

The ExecutionThread element will instruct the test to use the MTA threading model. This way, the static instance of “store” can be shared across tests.

New learning resources for Azure and BizTalk

A couple of new resources have become available in the past few days that will likely be of interest to you.

 

Azure

The Developer and Platform Evangelism team has just released a new version of the Azure training kit (Dec 23 2008). It’s a great way to learn about Azure. If you’ve not gotten a feel for what’s coming, check out the videos. It’s an easily digestible set of videos that go through the various pieces that make up the Windows Azure platform. Remember folks, this is not science fiction far future stuff, this goes live on Jan 1 2010 (this Friday!), and becomes chargeable on Feb 1.

The training kit includes a series of:

  • hands on labs
  • presentations (PPTX and videos)
  • demos
  • samples and tools

You can get it at http://go.microsoft.com/fwlink/?LinkID=130354

 

BizTalk

There’s a new issue of the always-informative BizTalk Hotrod available, featuring some really cool articles on how to augment the return on your investment in BizTalk, extending its reach, and benefiting from capabilities you may not have known you had.

Among others, this issue includes articles on:

  • ESB Toolkit
  • BizTalk and SharePoint (including one I contributed)
  • BizTalk management with PowerShell
  • HL7
  • BizTalk Adapter Pack 2.0
  • much much more!

Lots of good stuff in there, I’d encourage you to check it out, and if you’ve not seen them, browse through the back issues also.

You can get it as a PDF at http://biztalkhotrod.com/Documents/Issue8_Q4_2009.pdf

Gravatars in ASP.NET MVC using HtmlHelper

Gravatars in ASP.NET MVC using HtmlHelper

I’m working on a side-project right now that is using Gravatars and found the wonderful article by Ryan Lanciaux on creating an HtmlHelper extension.  His extension was good, but did not use integrate with the FluentHtml model of MvcContrib, so I refactored his original into the following class, which does the same thing, but allows for fluent building of all the options.  Someone will undoubtedly point out that this could have used a couple of Enumerations, and their right, but I decided the API was static enough that I’d just create the methods. Anyway, I hope someone else gets some use out of this.

using System.Collections.Generic;
using System.Security.Cryptography;
using System.Web.Mvc;
using System.Web.Routing;
using MvcContrib.FluentHtml.Elements;
using System;
using System.Web;

public class Gravatar : Element
{
    private string _email;
    private string _default;
    private string _rating;
    private int _size;
    private string _alt;

    public Gravatar(string email) : base("img")
    {
        _email = email;
    }

    public Gravatar DefaultToUrl(string url)
    {
        _default = url;
        return this;
    }

    public Gravatar DefaultToIdenticon()
    {
        _default = "identicon";
        return this;
    }

    public Gravatar DefaultToMonsterId()
    {
        _default = "monsterid";
        return this;
    }

    public Gravatar DefaultToWavatar()
    {
        _default = "wavatar";
        return this;
    }

    public Gravatar DefaultTo404()
    {
        _default = "404";
        return this;
    }

    public Gravatar Size(int size)
    {
        if (size < 1 || size > 512) throw new ArgumentException("Gravatars can only be between 1 and 512 in size.", "size");
        _size = size;
        return this;
    }

    public Gravatar GRated()
    {
        _rating = "g";
        return this;
    }

    public Gravatar PGRated()
    {
        _rating = "pg";
        return this;
    }

    public Gravatar RRated()
    {
        _rating = "r";
        return this;
    }

    public Gravatar XRated()
    {
        _rating = "x";
        return this;
    }

    public Gravatar AlternateText(string alt)
    {
        _alt = alt;
        return this;
    }

    protected override TagRenderMode TagRenderMode
    {
        get
        {
            return TagRenderMode.SelfClosing;
        }
    }

    public override string ToString()
    {
        var src = string.Format("http://www.gravatar.com/avatar/{0}?", EncryptMD5(_email));

        if (!String.IsNullOrEmpty(_rating)) src += string.Format("r={0}&", HttpUtility.UrlEncode(_rating));
        if (_size != 0) src += string.Format("s={0}&", _size);
        if (!String.IsNullOrEmpty(_default)) src += string.Format("d={0}&", HttpUtility.UrlEncode(_default));

        base.builder.MergeAttribute("src", src);
        base.Attr("alt", _alt ?? "Gravatar");

        return base.ToString();
    }

    private static string EncryptMD5(string Value)
    {
        using(var md5 = new MD5CryptoServiceProvider())
        {
            byte[] valueArray = System.Text.Encoding.ASCII.GetBytes(Value);
            valueArray = md5.ComputeHash(valueArray);
            string encrypted = "";
            for (int i = 0; i < valueArray.Length; i++)
                encrypted += valueArray[i].ToString("x2").ToLower();
            return encrypted;
        }
    }
}

public static class GravatarHtmlHelper
{
    public static Gravatar Gravatar(this HtmlHelper html, string email)
    {
        return new Gravatar(email);
    }
}

Breeze SharePoint 2010 Bootcamp Details

Breeze SharePoint 2010 Bootcamp Details

Hi folks, we’ve used our SharePoint expertise and knowledge to distil SharePoint
2010
product feature set, to provide a rich 5 day course
for you.

I believe with SharePoint in particular that as a developer you *need*
to know details about the SharePoint environment that is running your code, and as
an Admin, you need to know what and how the developer provides the additions/customisations
that they do.

Check out
the details
and you can register with Microsoft
here
.

We’re really excited about the offering and have a great Christmas break.

See you soon ho ho ho