Tuesday, November 4, 2014

How would you work with Office files

MS Word/Excel document in a web application? Look - what Microsoft offers besides office tools.

Problem Statement:

Clients might be interested in managing their data in the form of a Word/Excel/Powerpoint files in their application. You may have options to deal with them in the transportation or application design level. The best bet would be a SharePoint based App on the frontend. But, what would you do if you want to work with these files. For example, edit a word file and add some text into it or validate some input submitted by the user or add a custom macro into a file that will be used by user to do some specific operations. These are some of the examples you could have to do on a Microsoft office document. In such requirements, you will need to open up the file programmatically and get your hands dirty. Thankfully, Microsoft offers libraries to do that and more importantly with recent versions of office releases it has become easier and developer friendly.

File formats change:

Prior to Office 2007, office files used to have extension as .doc/.xls/.ppt etc. In 2007 and onwards, it has changed to .docx/.xlsx/.pptx etc. This is a whole new implementation of office suite by Microsoft. All the office documents are represented and defined by a set of interlinked XML files and flat files such as images. Its not a single binary file anymore. The file format is called Open Office XML(OOXML), also called as OpenXML. With this implementation, Microsoft's office files comply to a standard XML schema and can be read or interpreted by other applications by a different company.

We have two options:

1. Office Web Components (OWC) -  For office documents from 97-2003
2. OpenOfficeXML(OOXML) - For office documents 2007 or later

OWC components require office suite be installed on your server and it acts as just a wrapper between your code and the office installation.
OOXML is a complete support for working with documents with ease considering the fact that you have a specific schema in which the documents are built.
Additional benefits include -
 - OOXML library is a strongly typed set of classes wherein, you will be able to retrieve and insert objects and not just working with XML documents(nodes and attributes).
 - You could convert a 03 file into a 07 file (and vise-a-versa) with OOXML library.

OOXml SDK can be downloaded here.
 

Monday, November 3, 2014

How to handle time consuming tasks on web page

CPU intensive or time consuming operations? Consider the following options -

  • Create a Windows service with a Queuing mechanism and let the service process your jobs one by one and update your application via a database posting. Your application should be devised to look up the updates and show it up on UI as required.
  • Kick off the process in an Asynchronous task(async delegate) with a call-back method. Again call-back method would update the application database or the UI with a asynchronous refresh window.
Tasks which can run on these methods can be those whose results are not required right in the immediate http response(*). So, selecting either of these choices needs careful decision. And it will lead to additional design and implementation changes.

Scenarios:

Suppose we have to process a MS-word document being uploaded and extract data and save in your application database.
Or, You have some plot-point(tabular) data and you want to create a graph out of that.
Or, You want to publish a numerous number of files or strangled data into a PDF file format etc.
 These are some of the examples which takes considerably longer duration and would cause sufficient delays in the response and user will have to keep waiting. In such scenarios one would want to do the intensive processing in the back-end.

Windows service:

Will have to have a queuing mechanism wherein the service will pick the jobs from and process it. It could be as simple as a simple database table with status and the job information for the input and user. When the service sees and entry in the table with a status "Pending", it would take it up and process. If the queue volume increases you would consider more sophisticated queuing system like MSMQ.

Advantages:

 - Job runs on a separate process and could even be on a different server. So, it will not have an impact on the webserver performance.
 - You could have multiple instances of the service and allow them to process multiple jobs simultaneously. This helps to a greater extent when you have voluminous requests and all of them require equal level of priority.

Disadvantages:

This will lead to additional deployment tasks. 
 

Asynchronous Tasks:

This is a simple job delegation with a callback function. Your work is performed right within the ASP.Net process on a separate thread. You will have CPU & memory shared with this. Each one of your requests will kick off a new thread and will get processed right after they are initiated. If your jobs are memory intensive, this is no good choice.
 
(*)In both of these routes application will require a support for delayed response handling since they both work in a fire & forget manner. What does that mean?
In these cases, you will initiate a new job and leave it complete without waiting for it to complete. The w3wp/aspnet process will continue to process the request and send the response to the client. At that point of time there is no results available out of the job being submitted. The application will require a separate mechanism to check status of the job and update the client periodically.