WSCF.blue Beta Released: Are you doing contract-first?

by Alex Meyer-Gleaves 5 July 2009 - 4:56 PM

WSCF WSCF.blue is a Visual Studio Add-in designed to bring the benefits of contract-first web service development to those working with WCF (Windows Communication Foundation). I recently joined the WSCF.blue project and have enjoyed working with a team of very talented developers. Please be sure to read their blogs and benefit from the experience they offer:

My first foray into the world of contract-first web service development was made all the more pleasurable thanks to the WSCF (Web Service Contract First) tool. This early version of WSCF was targeted at web services developed using ASMX, and was first released by thinktecture, where it was maintained by team members Christian Weyer and Buddhike de Silva. The project later moved to CodePlex where it remains available today; although it has been renamed to WSCF.classic to differentiate it from the new WSCF.blue version designed for use with WCF. I highly recommend taking a look at WSCF.classic if you are developing web services using ASMX and are interesting in the contract-first approach.

For those currently working with WCF, or looking to migrate from ASMX, you will no doubt find WSCF.blue to be a must have tool. It fills a gap in the tooling available to .NET developers that want to embrace the contract-first approach in its true form. If you aren’t familiar with contract-first web service design I recommend reading the articles below. You will notice that the first of these articles was written by team member Christian Weyer (web services guru, co-founder of thinktecture and creator of WSCF):

The contract-first approach guides the developer of a web service into creating an interface (contract) that is interoperable and standards compliant. It requires a shift in focus, moving away from a code oriented perspective to one that is oriented towards data and messages. This type of thinking will greatly benefit those building SOA (Service Oriented Architecture) based systems. The XML Schema and WSDL (Web Services Description Language) specifications are key to achieving the goals of contract-first. These specifications may appear strange at first sight, but better understanding them will allow you to make your web services easier to consume, and more interoperable in the heterogeneous environments we frequently find ourselves forced to work within.

Below are posts from the other WSCF.blue team members regarding the Beta release:

Please download the Beta version, smack it around and put it through its paces. It is the development community that is best positioned to shape the future direction of WSCF.blue; so please provide your feedback, report bugs and help spread the word of contract-first.

Tags: , ,

Categories: WSCF | Web Services | Development Tools

Using REST in NAnt with a custom HTTP Task

by Alex Meyer-Gleaves 18 June 2009 - 12:42 AM

I needed to make HTTP requests to a REST web service from a NAnt script today so I knocked up a custom task. The HttpClient class from the WCF REST Starter Kit that I blogged about previously came in handy to offload most of the heavy lifting, leaving me to worry about the task related implementation details. The task supports all the HTTP methods and allows you to specify the content type and the content itself. You can also retrieve the response content and status code through properties set by the task. This was all achieved with surprisingly little code.

using System;
using System.Collections.Generic;
using System.Net;

using Microsoft.Http;

using NAnt.Core;
using NAnt.Core.Attributes;

namespace AlexMG.NAntTasks
{
    [TaskName("http")]
    public class HttpTask : Task
    {
        private static readonly List<HttpStatusCode> successCodes = new List<HttpStatusCode>
        {
            HttpStatusCode.OK,
            HttpStatusCode.Created,
            HttpStatusCode.Accepted,
            HttpStatusCode.NonAuthoritativeInformation,
            HttpStatusCode.NoContent,
            HttpStatusCode.ResetContent,
            HttpStatusCode.PartialContent
        };

        [TaskAttribute("url", Required = true)]
        [StringValidator(AllowEmpty = false)]
        public string Url { get; set; }

        [TaskAttribute("method", Required = false)]
        [StringValidator(AllowEmpty = true)]
        public string Method { get; set; }

        [TaskAttribute("content", Required = false)]
        [StringValidator(AllowEmpty = true)]
        public string Content { get; set; }

        [TaskAttribute("contenttype", Required = false)]
        [StringValidator(AllowEmpty = true)]
        public string ContentType { get; set; }

        [TaskAttribute("connectiontimeout", Required = false)]
        public int ConnectionTimeout { get; set; }

        [TaskAttribute("responseproperty", Required = false)]
        [StringValidator(AllowEmpty = true)]
        public string ResponseProperty { get; set; }

        [TaskAttribute("statuscodeproperty", Required = false)]
        [StringValidator(AllowEmpty = true)]
        public string StatusCodeProperty { get; set; }

        protected override void ExecuteTask()
        {
            HttpClient client = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage();

            if (!string.IsNullOrEmpty(Method))
            {
                request.Method = Method;
            }

            request.Uri = new Uri(Url);
            
            if (!string.IsNullOrEmpty(ContentType))
            {
                request.Headers.ContentType = ContentType;
            }

            if (!request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase))
            {
                request.Content = (string.IsNullOrEmpty(Content)) ? HttpContent.CreateEmpty() : HttpContent.Create(Content);
                request.Headers.ContentLength = request.Content.GetLength();
            }
            
            if (ConnectionTimeout != 0)
            {
                client.TransportSettings.ConnectionTimeout = TimeSpan.FromSeconds(ConnectionTimeout);
            }

            Project.Log(Level.Info, "Executing HTTP request.");
            Project.Log(Level.Info, "Url: {0}", request.Uri);
            Project.Log(Level.Info, "Method: {0}", request.Method);
            Project.Log(Level.Info, "Content Type: {0}", request.Headers.ContentType);
            Project.Log(Level.Info, "Connection Timeout: {0}", client.TransportSettings.ConnectionTimeout);

            try
            {
                HttpResponseMessage response = client.Send(request);

                if (FailOnError)
                {
                    response.EnsureStatusIsSuccessful();    
                }

                if (!string.IsNullOrEmpty(StatusCodeProperty))
                {
                    Project.Properties[StatusCodeProperty] = response.StatusCode.ToString();
                }

                if (successCodes.Contains(response.StatusCode) && !string.IsNullOrEmpty(ResponseProperty))
                {
                    Project.Properties[ResponseProperty] = response.Content.ReadAsString();
                }

                Project.Log(Level.Info, "Received HTTP response.");
                Project.Log(Level.Info, "Status Code: {0}", response.StatusCode);
                Project.Log(Level.Info, "Content Type: {0}", response.Headers.ContentType);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                string message = string.Format("The HTTP '{0}' request to '{1}' failed:{2}{3}", Method, Url, Environment.NewLine, ex.Message);
                throw new BuildException(message, ex);
            }
        }
    }
}

Using the task is simple. The only mandatory attribute is url and the default HTTP method is GET. Here is a sample NAnt project showing how to use the <http/> task.

<?xml version="1.0"?>
<project name="Http">
  <http url="http://www.howtocreate.co.uk/operaStuff/userjs/samplexml.xml"
        method="GET"
        contenttype="text/xml"
        connectiontimeout="30"
        responseproperty="response"
        statuscodeproperty="status"
        failonerror="true" />

  <echo message="Response: ${response}" />
  <echo message="Status Code: ${status}" />
</project>

I have attached the source code and task assembly below. The compiled assembly has the Microsoft.Http.dll assembly from the WCF REST Starter Kit merged into it using the ILMerge tool. This makes deployment easier by removing the chance of accidentally forgetting to deploy the dependency. If you are compiling from source code you will need to update the xcopy command in the post-build event to point to the location of your NAnt bin folder.

HttpNAnt Binary.zip (139.62 kb)

HttpNAnt Source.zip (1.06 mb)

Tags: , , ,

Categories: Garage Sale Code | Development Tools | Web Development | Web Services

Sysinternals Live

by Alex Meyer-Gleaves 30 March 2009 - 8:14 PM

If you aren’t already familiar with the Sysinternals tools then you must go and check them out. They are of invaluable assistance when it comes to troubleshooting problems and gaining insight into what is happening on your machine.

I use these tools nearly every daily and would often return to the Sysinternals site looking to see if updates were available. With the launch of the Sysinternals Live service the whole suite of tools are now accessible directly off the internet.

Sysinternals Live is a service that enables you to execute Sysinternals tools directly from the Web without hunting for and manually downloading them. Simply enter a tool’s Sysinternals Live path into Windows Explorer or a command prompt as http://live.sysinternals.com/<toolname> or  \\live.sysinternals.com\tools\<toolname>.

You can view the entire Sysinternals Live tools directory in a browser at http://live.sysinternals.com.

Here are some of my favourite tools:

  • If you do any presentation work then ZoomIt is a must have.
  • Monitor the registry, file system and network using Process Monitor.
  • Process Explorer is a great alternative to the Windows Task Manager.
  • TCPView is great for monitoring network connections.
  • Executing a process on a local or remote machine is easy with PsExec.
  • Kill a process on a local or remote machine using PsKill.
  • Shutdown and reboot machines using PsShutdown.
  • See what files other computers have opened on your machine using PsFile.

There are many more tools to play with and you will likely find something you always wanted but never knew existed. Also, check out Mark Russinovich’s blog (one of the creators of Sysinternals) for interesting blog posts on the inner workings of Windows.

Tags:

Categories: Development Tools

SyntaxHighlighter 2.0

by Alex Meyer-Gleaves 20 February 2009 - 1:56 AM

I was pleased to find out that a new version of SyntaxHighlighter has been released. New features including smart line wrapping, 100% standards compliance, additional themes and hosted versions of the source files. The documentation on the new wiki is also very good. It is obvious that Alex Gorbatchev has put a lot of effort into this release and should be commended. Below is a little “hello world” sample highlighted using the new version:

using System;

namespace HelloWorld
{
    /// <summary>
    /// A "Hello World!" program in C#.
    /// </summary>
    class Hello 
    {
        /// <summary>
        /// The main entry point.
        /// </summary>
        static void Main() 
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}

Tags:

Categories: BlogEngine.NET | Web Development

Moving files between projects in TFS

by Alex Meyer-Gleaves 28 January 2009 - 1:25 AM

Team Foundation Server I came across a knowledge base article by Microsoft Support that describes how to move files between projects in TFS in a way that keeps their history. In short, to keep your history you need to perform the move in the Source Control Explorer and then fix up your project files. Performing the move in the Solution Explorer causes the project files to be updated immediately, but the history will not be kept because it results in a delete and add operation.

Strangely, the article recommends that you perform the move and check in your changes before fixing up the affected projects. That would put your version control database into an invalid state, and anyone who performed a Get Latest Version would find themselves with a solution that does not compile.

You can easily perform all the actions required to complete the move in one Change Set. These are the steps I find work well for me:

  1. In the Solution Explorer, right-click the files (or folders) you want to move and select Exclude From Project. This will check out the project file and remove any references to the items you are about to move.
  2. Use the Source Control Explorer to move the items to the new location. The moved items will be checked out and have a pending rename operation.
  3. Back in the Solution Explorer, go the the project you moved the items into and click the Show All Files button.
  4. You will now see the items you moved marked with a plain white document icon. Right-click the items and select Include In Project. This will check out the project file and add a reference to the items you moved.
  5. In your Pending Changes window you will see the two project files have edit operations pending, and the moved items have a rename operation pending.
  6. Make sure the solution builds, your unit tests pass and that the changes look correct in your favourite diff tool.
  7. Check in your Pending Changes. These changes will all go into one Change Set and your version control database will never be in an invalid state. You will of course also have kept your history.

This simple process allows you to keep your source control operations atomic while ensuring you keep your valuable history.

Tags: ,

Categories: Development Tools

Code Tag Plugin for Windows Live Writer

by Alex Meyer-Gleaves 13 January 2009 - 9:21 PM

I made a simple Windows Live Writer plugin that allows you to easily surround text with the <code> HTML phrase tag. Writing a plugin for WLW is fairly straightforward and everything you need to know can be found in the Windows Live Writer SDK documentation on MSDN. The default browser rendering of text inside a <code> tag is to display it using a fixed-width font. You can of course alter its appearance using CSS to make it a little more interesting.

If no text is selected in the editor and the plugin is activated, a small dialog is presented into which you can enter the text that is to be placed inside the tag. You can also select text in the editor and have it immediately placed inside the tag when the plugin is activated. Trailing whitespace in the selected text will not be included inside the tag. Any trailing whitespace inside the tag would also be fixed-width, causing the spacing before and after the text inside the tag to appear uneven.

To install the plugin simply drop the AlexMG.CodeTagPlugin.dll file into the C:\Program Files\Windows Live\Writer\Plugins folder. When building from source code the plugin will be copied to the WLW plugin folder during the post-build event.

<code> Tag Plugin (3.67 kb)

<code> Tag Plugin - Source (5.45 kb)

Tags: ,

Categories: Garage Sale Code

About the author

Alex Meyer-Gleaves I'm a software developer living in Australia (that island like continent in the southern hemisphere). I love Microsoft .NET and C#. I hate early mornings, slow drivers and Lotus Notes.

Google Reader Clips

SpringWidgets
RSS Reader
This widget is the staple of our platform. Read all your feeds right here with thisone widget - Supported feeds are OPML, RSS, RDF, ATOM. Watch your favorite Podcastin the embedded Video Player on the Desktop or publish your own video playlist toyour site for others to view!

Recent Comments

Comment RSS

Links

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008