Archive for the ‘.NET’ category

Using IronRuby and Rake for .NET Builds

January 11th, 2011

I have been working on getting Hudson CI slave server setup at Integrum for all of our .NET builds and during the process I came across a problem with the MSBuild plugin for Hudson that caused me just to go grab NAnt and start creating the XML build files that we needed. While I was working on the first files I was thinking of the tools that the Rails developers at Integrum used for their builds and heard the discussions about Rake tasks. So after doing a little bit of investigation I found that plenty of others had done it so I started down the path of using IronRuby and Rake for our builds. The goal of this post is to get you introduced and setup with running your first simple build using IronRuby, Rake and Albacore.

Install IronRuby

First off you are going to need to get IronRuby installed. IronRuby is a version of the Ruby language that targets the .NET framework and is built using the Dynamic Language Runtime (DLR). I have spent very little time in Ruby and figured all this would be a great way to get started.  Head on over to the project on codeplex and download the install package, I used the latest version (v.1.1.1).

After the install is completed you will need to reboot your system for the path changes to take effect or until then you can open a command prompt and enter the following to update the path:

SET PATH=%PATH%;"C:\Program Files (x86)\IronRuby 1.1\bin\"

Now inside the same command prompt window go ahead and enter “ir” and if everything has been successful you will be prensented with the IronRuby interpreter. Go ahead and enter the following to give it a go:

puts "Hello world"

After hitting enter you should be presented with the output. You can type “exit” at anytime to leave the interpreter.

Installing Rake

Now that we have IronRuby installed we are ready to get Rake installed. Rake (Ruby Make) was created to provide an alternative for building Ruby applications and aimed to provide the same capabilities for builds as make does. One of the primary attractions to it is the fact that it does not use XML files for defining your builds. You will be instead creating Rake files (Rakefile.rb) that define the builds via code.

Rake is installed using the gem installs which is done by using the igem command for IronRuby. To install the Rake gem run the following command:

igem install rake

This will take care of downloading and installing the library for you. Once the install has completed you can test the install by entering “rake” at the command line. You should be presented with an error “rake aborted” because no rake file was found.

Now lets build a simple hello world example using rake. To do this we are going to be creating a file named rakefile.rb so you may want to switch over to a temp directory. Create the rakefile.rb file and enter the following:

task :default => [:hello_world]

desc "Just says hello to the world"
task :hello_world do
	puts "Hello world from Rake"
end

After you have saved the file as “rakefile.rb” you can process it using Rake by just entering rake in the command line while still in the directory that you created the file in. You should be presented with the expected output “Hello world from Rake”. The first line in this file defines the default task to run if none is specified and then starting on line 3 we have the task of just outputting text. This simple rake file only performs one task and you can simple include multiple tasks in a single file. Change the rakefile.rb to match the following and then run rake:

task :default => [:hello_world]

desc "Just says hello to the world"
task :hello_world do
	puts "Hello world from Rake"
end

desc "Just hello to you"
task :hello_you do
	puts "Hello you"
end

desc "Hello to you and the world"
task :hello_to_all =>
	[:hello_world,:hello_you] do
	puts "Hello to everyone"
end

Since by running just rake you are only running the default task specified all you will get is the same “Hello world from rake” output. But if you enter the following:

rake hello_to_all

It will run that task which in turn executes the other tasks specified and you will get all 3 lines written out.

Albacore for .NET Rake Tasks

Rake is used to run the tasks that we need but where do these tasks come from? This is where the Albacore library comes in. The tasks that are provided with the library are to access and use the common tools for building and running tasks for .NET projects. Instead of you having to learn how to build your own tasks the library provides many. To install Albacore we will use igem just like we did for rake:

igem install albacore

Now that the library is installed we can create a simple console project in a solution and then use rake to run a build on the solution. I have attached the project to this post so we do not have to go through the standard Hello World app building. There is a project folder that contains the RakeFile.rb and a src folder that contains the solution to be built. Here is the contents of the RakeFile.rb used to build the solution.

require 'rake'
require 'albacore'

task :default => [:full]
task :full => [:standard_build]

msbuild :standard_build do |msb|
  msb.properties :configuration => :Release
  msb.targets :Build
  msb.solution = "src/BuildHelloWorld.sln"
end

This file uses the albacore task to run MSBuild against our solution. The task requires that you specify a couple pieces of configuration information like the configuration to be built, the target (such as build, clean etc) and then the location of the Solution file to be used. There are many other configuration options available and you can get more info over at the wiki pages on github. The wiki provides information on the tasks that are available such as NUnit, SQLCmd and PLink.

So far I have found using this method very easy to manage and also very easy to read. Working with the tasks instead of XML just takes pain out of building upon the files and makes them easier to follow. I will be doing some more posts on using Hudson and going further with builds using Albacore tasks.

Download Project

Issues with Streaming Video in the WP7 Emulator – ag_e_network_error mediaelement

December 15th, 2010

I have been working on a Windows Phone 7 application for viewing and consuming web content, including videos. During testing of the application, I came across a problem that was driving me crazy: Videos would not play in the emulator. While I was working on this, I was also testing new video links, so I was thinking there was a problem with the video content or a incompatibility I was not aware of. Needless to say, I spent a decent amount of time researching and digging for answers… and then it hit me. I tried it on the phone to no avail. Then I finally disconnected the sync cable from the phone and boom, it starts working perfectly — no errors to be seen.

So the quick point is, when running across a problem that just seems crazy, deploy to device and disconnect.

To explain the problem, I will just give short example that allows you to dupe the issue. I have not yet been able to nail down completely what the problem is, but it does seem that all videos having the issue are VC-1 encoded for video. I have tested this on three different dev machines and had the same issue across all.

In your WP7 application you can just add a MediaElement to any page.

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY Video Test" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Video Viewer" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
        <MediaElement x:Name="MediaElement" Grid.Row="2"/>
    </Grid>

Next, in the cs file for the page you can set the source for the MediaElement, and by default the control is set to AutoPlay. Once the source is set, it should start playing when you run the app in the emulator.

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            ConfigureVideoPlayer();
        }

        public void ConfigureVideoPlayer()
        {
            MediaElement.Source = new Uri("http://content5.catalog.video.msn.com/e2/ds/b2ee41d1-8d9f-4c70-b40d-303ebe154f6f.wmv", UriKind.Absolute);
        }
    }

The video in this sample is a WMV that is using Windows Media Audio 10 Professional for audio and VC-1 video. If you deploy this application to a device for testing, it will not work until you remove the device from its connection to the host computer. Also, if you want to see a video that plays with no problems, just change the Uri to use http://download.microsoft.com/download/B/7/4/B741136A-7672-4B74-88A1-04DE9443574F/Springboard_061809_MBR.wmv and then run the app. This second video is Windows Media Audio 9.2 and Windows Media Video 9.

Now with the new video lets hook up to the MediaFailed event and take a look at what is getting reported back:

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            ConfigureVideoPlayer();
        }

        public void ConfigureVideoPlayer()
        {
            MediaElement.Source = new Uri("http://content5.catalog.video.msn.com/e2/ds/b2ee41d1-8d9f-4c70-b40d-303ebe154f6f.wmv", UriKind.Absolute);
            MediaElement.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(MediaElement_MediaFailed);
        }

        void MediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            var errorException = e.ErrorException;
        }

When running this code and looking the exception being reported, we see that the message is “AG_E_NETWORK_ERROR” and appears to be a generic error that can be given when there are authentication issues, cross domain access issues, or just problems opening files.

Microsoft Entity Framework in Action Review

June 15th, 2010

I have been dipping into Microsoft’s Entity Framework

Microsoft Entity Framework in Action

Microsoft Entity Framework in Action

for a little while, but until now most of my EF applications have been pretty simple. For some of my current projects,I’ve wanted to get a better understanding of how the new version of EF works and gain insight into how it managed my data for me. I decided to get the MEAP version of Manning’s Microsoft Entity Framework in Action book by Stefano Mostarda and Marco De Sanctis and see what all it covered. After purchasing and using several MEAP books from Manning, I really think they are a great value and allow you to gain access to the texts before they have been published.

The book is broken into four parts, each building on the knowledge gained from the previous section. Part 1 of the book is an introduction to O/RMs and a quick guide as to what they provide developers. The coverage in this section is to help you get a better understanding of the Object/Relational Mismatch that occurs in applications and how solutions such as EF are developed to help with this mismatch. At the end of this section you will build the structure for a sample app that will be referenced throughout the book. This is a simple order application but it is used intelligently and explained in such a way that makes referencing it later helpful for understanding how EF usage is a benefit to the project.

Part 2 quickly starts diving into EF and guiding the reader through querying and understanding the model. In addition, it goes into the mechanics used for persistence and concurrency via EF. I’ll be referencing this section quite often due to all the examples that are included for dealing with data via querying, such as grouping, sorting and projecting results for ease of use. After the querying sections you are guided through the Entity Data Model (EDM) and how it is constructed and described using its different parts: Conceptual Model (CSDL), Storage Model (SSDL) and the Mapping (MSL). In most cases developers will use the Designer and the EDMX file generated in Visual Studio. Having an understanding of how the XML is built and how to build one manually, however, can really help when you want to make tweaks outside of the designer, such as extending the EDM with custom annotations.

In the last two chapters of this section the book goes into Persistence and Concurrency via EF. The persistence was one of the most interesting areas of the book for me (due to my DBA experiences). I think that too many times we use frameworks like EF without understanding the impact that some of our coding makes on the data store of our applications. These two sections give the reader a better understanding of how,and when, entities are marked in such a way that SQL calls will be made to the database for managing them. Having a better understanding of EntityState and ObjectStateManager can help a developer in mitigating the number of calls that will be made to the database by making sure that objects are marked correctly before calling SaveChanges methods. Also, through some of the techniques described for managing concurrency and transactions in your EF application, including managing entities in a disconnected manner, you can help ensure that the application works as expected.

Part 3 of the book focuses on some of the more advanced topics of EF including Entity SQL, usage of Functions and Stored Procedures in your model, querying the EDM programmatically, and customizing the EF code and designer. I was mainly interested the usage of Functions and Stored Procedures here. The book does a good job of providing the reader with several examples for mapping and consuming SPROCs from EF; including selecting data, updating entities and building functions that can be used against the inheritance hierarchy that you defined in your application.

The final section of the book walks you through bringing all of this new found EF knowledge together for building the order application. The authors did a good job of presenting not only their example application, but also providing the reader with meaningful information that can be used for building application architectures that use EF. Chapters include ASP .NET, Windows Applications and how to integrate EF with a traditional n-tier application. The last chapter provides helpful information on building unit tests against your EF enabled application.

I would recommend the book for those who are looking to increase their understanding of Microsoft’s Entity Framework. The book is not only a walk-through of how to build and use EF, it also provides the reader with a lot of reference material that can be used in day-to-day development work. I plan on referencing the “Working with Entity Framework” section quite often on my next project with EF in my attempts to streamline all data transactions that occur in my application when dealing with my entities.