Microsoft jump on the Open Source band wagon


A landmark decision was made last week by Microsoft’s ASP.NET web development team. They announced that three key technologies used to develop web applications, ASP.NET MVC, Web API and Razor would be made fully Open Source1. This is a big step for a company built on the sales of Closed Source software. I’m going to have a look at what I think this decision means for the consumers, who use software built with these technologies, and the stakeholders who commission the software.

Open Sesame

For those those not involved directly in software development, the moniker ‘Open Source’ may conjure up images of magicians pulling rabbits out of a hat2. However the reality could be viewed as the opposite of magic as Open source demystifies the software development process. In the traditional ‘Closed Source’ licencing model, software is made by a company behind closed doors and the code is encrypted before release to the public. The traditional thinking is that this protects the company by preventing third parties from stealing their intellectual property. This is a subject of some debate , which I won’t go into here. However it is clear that many businesses are currently successful without relying on restrictive software patents.

In an Open Source licencing model, the full unencrypted source code will be published at the same time as the software itself (indeed often before-hand in the case of preview and beta releases). Also, as in the case of this announcement, public contributions may be accepted to modify the original source code. Naturally the acceptance process is completely at the discretion of software company, so unhelpful or damaging contributions will not be accepted.

A good example of the Open Source model working commercially is the Android operating system released by Google. A massive range of mobile devices run Android and it is not only the key competitor to Apple’s iOS (iPhones & iPad operating system but has helped ensure that most devices using Microsoft’s Windows Mobile operating system are sitting on the scrap heap. I for one am glad that there is healthy competition to the world of Apple and its clone army of i-device accolytes.

Software that Just Works

From a consumer or stakeholder viewpoint the natural desire is to want software that ‘just works’ and to reach that goal we just need to fix all the bugs in the software. Unfortunately the belief that all software bugs can be fixed is generally infeasible given time and budget restraints, due to the nature of programming the two go hand in hand. In each piece of code there are many paths that can be travelled, much like a maze in a country mansion garden. If you ask 10 different people to walk the maze the chances are each one will set off in a different direction. Most of them will hit a dead end before finding their way through and a nasty bug could be hiding at each of these dead ends!

However Open Source software helps minimize the number bugs by accepting contributions to fix any that are found by the development community. In this way the software benefits from the knowledge of a much wider group of people than could be acheived if it was developed and maintained soley by a single company. Most software developers are driven by that itch to ‘make stuff work’ and are only too happy to contribute to a project which helps make this happen. The end result is a product that is more reliable for the consumers and stakeholders and has the appearance of ‘just working’.

Security concerns

Typically the biggest obstruction to making software Open Source is the perceived risk to security. If anybody can view exactly how the software is operating then it is much easier for hackers to cause the software to break, exploit a vulnerability to propogate a virus or carry out other nefarious acts. The indications by Phil Haack, a former member of the MVC team, are that this decision by Mircosoft is no exception. So a great deal of credit should go to those involved at Microsoft for making change happen in their organization.

Regarding the perceived risks to security, Open Source can actually reduce the risk for similar reasons to the improvement in relibility. Any security vulnerabilities will quickly be picked up by the community and fixed. The Open Source culture of continual improvement in the software makes for a much more secure product.

Take the example of web browsers, a couple of years ago Microsoft’s Internet Explorer browser had massive market share. However over time so many security vulnerabilities were exploited in the browser that the US government themselves warned against using the software!  Along came Mozilla’s Firefox browser to save the day, with much improved security. Naturally Firefox has always been developed under an Open Source licence as explained in the Mozilla manifesto.

Future developments

The likelyhood is that this announcement will not trigger a deluge of community updates and fixes to the frameworks in question. I anticipate that a minority of change submissions will make it back into the frameworks themselves. However it does demonstrate an important change in mindset from Microsoft and may well signal the beginnings of a new approach for the organisation. It is clear that in the fast moving world of software and digital media you need to adapt to survive and it is encouraging that even massive organisations such as Microsoft are able to do this.

Footnote

1While the MVC framework has been open source since its original release, this announcement widens the license model to the Web API and Razor frameworks and also marks the first time that Microsoft will accept public contributions to the open source frameworks. If you are unfamiliar with these technologies then further information is available on the Microsoft blah blah (LINK).

2Or maybe thats just my fertile imagination!

Excel reporting from web apps using EPPlus

I have recently been working on a new feature request for the ability to generate excel reports dynamically from a web application. This kind of feature can add a lot of value to a system as it enables businesses to analyse crucial trends in data on demand.

However it can be challenging to generate reports in an Excel spreadsheet format from a web application. Firstly you need to have the program itself installed on the web server, which can be difficult if you are using third party hosting. Also coding the solution itself is difficult as you either need intimate knowledge of the Excel API or, for versions of office after 2007, in depth knowledge of the OpenXML file format. But fear not there is a third option, open source to the rescue! Some clever chaps have developed the EPPlus library which abstracts away all the nasty low level OpenXML calls and provides a nice object oriented API for dealing with directly the spreadsheet.

Modelling the report data

I won’t go into too much detail on the features of the library as you can read all about it on the EPPlus FAQ page and download the samples. However one of the methods that I found really useful was the LoadFromArrays method. This enables a table of data to be output to the worksheet from a list of object arrays. So you can construct a model of the data you want to display then feed it straight to the worksheet through the list. It may sound quite complicated but here is a simple example that can be easily expanded with more columns / rows:

string firstName = "Joe";
string lastName = "Bloggs";
var dateOfBirth = new DateTime(2000, 1, 1);
var testData = new List<object[]>()
                   {
                       new object[] {"First name", firstName},
                       new object[] {"Last name", lastName},
                       new object[] {"Date of birth", dateOfBirth}
                   };

using (var excelPackage = new ExcelPackage())
{
    ExcelWorksheet excelWorksheet = excelPackage.Workbook.Worksheets.Add("Test worksheet");
    //Loads the summary data into the sheet, starting from cell A1. Prints the column names on row 1
    excelWorksheet.Cells["A1"].LoadFromArrays(testData);
}

Don’t get duped

I generally found that the EPPlus library was reliable, however I did hit a snag during the development. The file would get generated OK but when I tried to open it in Excel a nasty message was shown:

Excel found unreadable content in ‘Test export.xlsx’. Do you want to recover the contents of this workbook?

The file did open OK if you chose to recover, however this obviously isn’t ideal and not acceptable for a client release. The tricky part was that no error was being thrown so I had to use a bit of guess work to track down the issue. Initially there wasn’t much to go on, however after running some more tests on sample data I noticed that the export always worked fine if the file only included a single worksheet. However sometimes multiple worksheets would cause the ‘unreadable content’ error. From there I examined each of the properties that were being set on the worksheets and narrowed the problem down to be the worksheet name itself.

It turns out that excel requires each worksheet to have a unique name. This is backed up in excel itself if you try and manually name to worksheets the same you get a reasonably friendly error message saying:

Cannot rename a sheet to the same name as another sheet

After ensuring my worksheets were uniquely named the reports always opened up without a problem. As an aside I just tried to replicate this bug through EPPlus and this time it returned a much more helpful error message, I wish it had told me this when I was originally working on the feature!

Add worksheet Error: attempting to create worksheet with duplicate name

EPPlus for the win

In conclusion I would recommend EPPlus to anybody who needs to generate an excel report from C# code and doesn’t want to interface with the murky depths of the Excel API. It is a great open source library that does just what you need in the way you would expect it to work.

Quartz makes scheduling jobs easy with EPiServer Commerce

Quartz rockA common requirement for e-commerce applications is the ability to schedule jobs or tasks. I am currently working on a project which uses EPiServer Commerce and I needed to schedule an order export job.

I was pleasantly surprised to find that EPiServer Commerce is supplied bundled with the Quartz.NET enterprise code library to facilitate these job scheduling requirements.

Gentlemen, choose your weapons

I had three main options available for implementing the export job, a separate windows service, an EPiServer CMS scheduled job or a Quartz job. I decided that the Quartz service was better than the option of a separate windows service because the framework was already there and available so would not increase the complexity of deployments. I also felt it was a better option than a EPiServer CMS scheduled job because Quartz runs independently to the website app pool so should not put the web application under greater load while it is running and potentially degrade the performance of the end user website.

A 1000 foot example

I’m not going to go into a massively detailed explanation of how to implement the jobs as there is a already a good example available in the EPiServer Commerce dev guide along with a detailed tutorial on the Quartz .NET site. However a quick overview demonstrates how intuitive it is to implement.

The first thing you need to do is create your job class which will run when Quartz triggers the execution. The class must inherit from the IJob interface which just has a single Execute method to implement. Here is a sample skeleton class:

    public class CommerceOrderExportJob : IStatefulJob
    {
        private readonly ILog _log;

        public CommerceOrderExportJob()
        {
            _log = LogManager.GetLogger(base.GetType());
            //Any other init code
        }

        public void Execute(JobExecutionContext context)
        {
            //Your export methods
        }
    }

You then need to extend the config file to configure Quartz to call your job. I think all the parameters are pretty self explanatory:

<job>
    <job-detail>
      <name>OrderExportJob</name>
      <group>all</group>
      <description>This job exports orders from commerce</description>
      <job-type>Your.Assembly.CommerceOrderExportJob, Your.Assembly</job-type>
      <volatile>false</volatile>
      <durable>true</durable>
      <recover>false</recover>
    </job-detail>
    <trigger>
      <simple>
        <name>OrderExportJobTrigger</name>
        <group>eCommerceFramework</group>
        <description>Fires Order Export Job</description>
        <misfire-instruction>SmartPolicy</misfire-instruction>
        <volatile>false</volatile>
        <job-name>OrderExportJob</job-name>
        <job-group>eCommerceFramework</job-group>
        <repeat-count>RepeatIndefinitely</repeat-count>
        <repeat-interval>300000</repeat-interval>
      </simple>
    </trigger>
  </job>

Hit run on the service and check the event log for any problems. Make sure to include some useful event/error logging in the job otherwise it will just be a mysterious black box! There are a couple of snags you may run into when running the job against EPiServer Commerce:

  • ensure that your compiled assembly is actually available in the Quartz service directory, this is different from the main commerce site bin folder so it is not enough just to include it in the commerce app references
  • Make sure the data context has been initialised and the config file in the Quartz service directory is pointing to your database

Mining for more gems

Ultimately only time will tell if Quartz was the best choice but I feel it has the best chance of high reliability and performance in the long term. I hope that, where appropriate, EPiServer can continue to expand the incorporation of proven open source libraries with their product offerings.