Saturday, October 31, 2009

iTextSharp PDF rendering in a medium trust ASP.Net environment

Rendering a PDF for download or email is a very common task for an e-commerce website. One thing to consider when deciding on a PDF library for rendering is what environment the site is running in. Most ASP.Net shared hosting solutions will restrict their hosted sites to Medium trust to prevent a rogue site from peeking at other sites on the server.

Medium trust environments can cause funny things to happen between the development environment and the production environment. By default the websites created in Visual Studio have Full trust, this can cause security problems after deployment if you haven't setup your development environment to mimic production. A good first step is to add a trust level to your web.config system.web section.

<system.web>
    <trust level="Medium" />
    ...
</system.web>
This will help to find any trust issues while developing.

So to rendering PDF. After trying ReportViewer 2008 in local mode and PDFSharp (both of which are still very good at rendering PDFs), I have found success with iTextSharp. ReportViewer and PDFSharp both require Full trust mode because they use native COM dlls as part of the GDI rendering process. This makes them unsuitable for shared hosting environments unless you can convince your hoster to raise your site's trust level. The PDFSharp Wiki says that release 1.30 solves most medium trust issues and that full support is in the near future, which is promising. My car also has most of it's wheels, but until I put the fourth one on it isn't going to go far on the road.

iTextSharp appears to have less documentation on the web (one of the best being a fairly comprehensive iTextSharp tutorial), but it is just as powerful as PDFSharp or ReportViewer. The best thing about it though is that it can run in Medium trust mode - once a minor change is made to allow partially trusted callers. To make this change download the iTextSharp source distribution (http://sourceforge.net/projects/itextsharp/files/)
Modify the AssemblyInfo.cs file to add the partially trusted callers attribute.
[assembly: AllowPartiallyTrustedCallers()]
Rebuild the iTextSharp assembly and it should be good to go in a Medium trust environment.

Tuesday, May 19, 2009

Unit Testing with Typemock

Unit Testing ASP.NET? ASP.NET unit testing has never been this easy.

Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers.

The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price.

Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit.

The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you'll get a license automatically (even if more than 60 submit) during the first week of this announcement.

Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends.

Go ahead, click the following link for more information on how to get your free license.

Tuesday, April 28, 2009

Comments are the true revealer of a bad coder

Comments that are in the code of the application that you have inherited maintenance of can sometimes very quickly reflect the quality of the codebase. In this case there is obviously a bit of confusion around the subtlety and use of the cast, as and is C# operators. Yes, there is also a grammatical error. And no, I have not removed any code from inside the if block - that is all there was.


if (payment is CreditCardPaymentDto)
{
// For some reason casting this outside the if statement throws and exception
CreditCardPaymentDto creditPayment = (CreditCardPaymentDto)payment;
}

Tuesday, February 17, 2009

Quotes that may indicate your workplace practices waterfall methods


The functional specs will be released to the development team once they are signed off later this week. You can start development next week.

To be fair it was Monday, so I would potentially have up to 4 days as development team lead to prepare the team. </sarcasm>

I guess I'll have to spend the first couple of weeks of development time working out what we are developing...

Monday, January 12, 2009

Don't take shortcuts on script tags!

This problem annoyed me for longer than I would have hoped recently - even though I have seen it before and spent a frustrating time 'fixing' it. I added a script reference (the first for the page) to the head of a html page and lazily wrote it as

<script type="text/javascript" src="scripts/jquery-1.3.1.js" />

Well this broke the ScriptManager, which couldn't hook up the postback methods for the UpdatePanels on the form. Hmm that's odd, even an empty .js file included this way cause a javascript error on page load such as "this._form is null" or "__doPostBack is not defined". And then something twigged from a not-so-distant project and there it was

<script type="text/javascript" src="scripts/jquery-1.3.1.js"></script>

Lesson: Don't take shortcuts when writing script tags!

More explanation of why http://ajaxian.com/archives/why-doesnt-script-work

Wednesday, August 13, 2008

Dynamic Data Changes - EnableQueryStringSelection

With the release of .Net 3.5 Framework SP1, the Dynamic Data tools have changed slightly. If you setup a site with a pre-SP1 version of Dynamic Data then this issue may have bitten you.

Runtime error - "Type 'System.Web.DynamicData.DynamicDataManager' does not have a public property named 'EnableQueryStringSelection'."

This flag on the DynamicDataManager enabled that manager to ensure that when a control such as GridView is associated with another control such as DetailsView that one would update when the other changed and vice-versa. One of the downsides of this behaviour is that your associated control had to be supported by the DynamicDataManager to be able to participate in the synchronisation - so many 3rd party controls missed out. Also all registered controls participated in the synchronisation from the Url.

To reenable this behaviour in SP1 use the DynamicDataManager.RegisterControl(Control control, Boolean setSelectionFromUrl) overload when registering the controls. When true is specified that control will get its selection from the Url. So there is now a lot more flexibility in defining which control is managed from the url, instead of all registered controls being managed.