Linq2Azure 1.1.0.0 released!

Linq2Azure 1.0.0.7 released!

Linq2Azure – NuGet Package Released!

image

We’ve just released the Linq2Azure API to the NuGet Gallery, this is targeted at people who are trying to build automation into their applications or deployment scripts.

If you just want to try out Linq2Azure, I’d recommend the Linq2Azure LINQPad driver instead.

NOTE: At this stage .NET 4.5 is required!

We will consider releasing a .NET 4.0 version of the API if the community requires.

Linq2Azure 1.0.0.1 – LINQPad driver update!

This update contains a bug fix for configuration serialization. The bug would cause cloud deployments & configuration updates to fail as the XML was invalid.

Just working out the LINQPad driver deployment process more than anything else.

More soon!

Linq2Azure 1.0.0.0 – LINQPad driver released!

image

I’m happy to announce that you can start using Linq2Azure now!

At the moment the API is only available in LINQPad driver form. You will have to wait a bit longer for the NuGet package & direct download.

5 Easy Steps to Query & Manage “The Cloud”

Step 1: Open LINQPad and click “Add connection”

image

Step 2: Click “View more drivers…”

image

Step 3: Download & Enable Driver

image

Step 4: Create a Linq2Azure connection

image

Step 5: Select a .publishsettings file from your hard-drive.

image

If everything goes smoothly, you will now be able to query & manage your cloud environment from the safety of LINQPad!

image

Stay tuned to find out how you can:

  1. get the NuGet package
  2. download the source
  3. help contribute to the ultimate cloud management API!

More soon…

Linq2Azure – What is it?

image

Linq2Azure – Azure Management API

Thanks to everyone who made it to the presentation last night.

Linq2Azure is a “cloud management” API that gives .NET developers a familiar programming model for querying and managing their Windows Azure environment.

Linq2Azure is statically typed and includes Code Contracts, meaning .NET developers can reliably automate the management of their cloud environments with maintainable programs.

// Simple Example: reboot all role instances in your environment!
var query = 
    from c in CloudServices.AsObservable()
    from d in c.Deployments.AsObservable()
    from i in d.RoleInstances.AsObservable()
    select i;

foreach(var instance in query) instance.Reboot();

LINQPad Driver

In addition to this, fans of LINQPad will benefit as we are publishing the “Linq2Azure – Azure Management Driver” for LINQPad.

image

As discussed, our new API will be available to the general public very soon.

Stay tuned!

Asynchronous Commands in Metro, WPF & Silverlight

I’ve seen quite a few examples demonstrating the new async/await language features (C# 5 & VB next) with button click events;

private async void button1_Click(object sender, RoutedEventArgs e)
{
    string url = "http://reedcopsey.com";
    string content = await new WebClient().DownloadStringTaskAsync(url);
    this.textBox1.Text = string.Format("Page {0} supports XHTML 1.0: {1}",
      url, content.Contains("XHTML 1.0"));
}

If you are using an architectural pattern like MVVM it’s unlikely that you’re writing code like this. In WPF, Silverlight & Metro you can bind buttons directly to an object implementing the ICommand interface.

// WPF ICommand interface
public interface ICommand
{
    /// <summary>
    /// Defines the method to be called when the command is invoked.
    /// </summary>
    /// <param name="parameter">Data used by the command.
    /// If the command does not require data to be passed, this object can be set to null.</param>
    void Execute(object parameter);

    /// <summary>
    /// Defines the method that determines whether the command can execute in its current state.
    /// </summary>
    /// 
    /// <returns>
    /// true if this command can be executed; otherwise, false.
    /// </returns>
    /// <param name="parameter">Data used by the command.
    /// If the command does not require data to be passed, this object can be set to null.</param>
    bool CanExecute(object parameter);

    /// <summary>
    /// Occurs when changes occur that affect whether or not the command should execute.
    /// </summary>
    event EventHandler CanExecuteChanged;
}

The nice thing about commands vs. a simple click event is that they encapsulate the logic informing the button wether or not it can be executed. This is particularly useful when we start talking about asynchronous operations as we might like to disable the button while the asynchronous request is in flight.

Example

    public bool CanExecute(object parameter)
    {
        return !isExecuting;
    }

    public async void Execute(object parameter)
    {
        isExecuting = true;
        OnCanExecuteChanged();
        try
        {
            // await some asynchronous operation
        }
        finally
        {
            isExecuting = false;
            OnCanExecuteChanged();
        }
    }

What About Errors?

Note that commands are generally executed by the UI frameworks message loop, meaning that any unhandled exceptions will be posted onto the relevant synchronisation context.

AsyncCommand

This pattern is easily captured in a reusable object that we can use to build all our asynchronous commands.

    // a reusable asynchronous command
    public class AsyncCommand : ICommand
    {
        private readonly Func<Task> execute;
        private readonly Func<bool> canExecute;
        private bool isExecuting;

        public AsyncCommand(Func<Task> execute) : this(execute, () => true) { }

        public AsyncCommand(Func<Task> execute, Func<bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            // if the command is not executing, execute the users' can execute logic
            return !isExecuting && canExecute();
        }

        public event EventHandler CanExecuteChanged;

        public async void Execute(object parameter)
        {
            // tell the button that we're now executing...
            isExecuting = true;
            OnCanExecuteChanged();
            try
            {
                // execute user code
                await execute();
            }
            finally
            {
                // tell the button we're done
                isExecuting = false;
                OnCanExecuteChanged();
            }
        }

        protected virtual void OnCanExecuteChanged()
        {
            if (CanExecuteChanged != null) CanExecuteChanged(this, new EventArgs());
        }
    }

Usage

In your view model you can now create asynchronous commands like this;

// example command, simulate an operation that takes 2 seconds.
new AsyncCommand(() => TaskEx.Delay(2000));

// example command, with some custom can execute logic
new AsyncCommand(() => TaskEx.Delay(2000), () => IsValidInput());

 

Memory Leaks

A word of warning… If your command object’s lifetime extends beyond that of the UI element (Button) that is subscribing to the CanExecuted event you should implement a weak event pattern in here. I think that is outside the scope of this article. I’ll follow up shortly.

In Conclusion

This is a great example of why async void methods are required in C#. Commands are like a bridge between synchronous UI elements like buttons and your view models asynchronous operations like web requests. Enjoy!

Follow

Get every new post delivered to your Inbox.