Enabling Das Blog To Run in Medium Trust Environment

(By rajivpopat@gmail.com | Version 0.3 - Initial Draft - Not For Release)

DasBlog contains a rich set of features and to take advantage of all of the rich features of DasBlog you need to run in under Full-Trust environment. If you are running on your own Server, or a have a hosting provider that lets you run DasBlog under full trust, this is usually not a problem and this manual is not for you.

If however, you're stuck with a hosting provider that does not let you run in Full Trust environment and you're a .NET developer / Geek, read on. The objective of this Manual is to let DasBlog users (who are running DasBlog on .NET 2.0 Runtime) run under medium trust level.

"If I Run on .NET 1.1 - Is this Manual For Me?"

No. DasBlog uses many external open source and free components. Some of these components require full Trust. The .NET 2.0 runtime allows settings like "requirePermission = false" at the web.config file level which allows DasBlog to run the components which would otherwise require full trust, to run under medium trust. Since settings like these are not available under .NET 1.1 runtime, currently this manual is just being targeted for users who are running on .NET runtime 2.0 and need to run DasBlog in Medium Trust Level.

What Do I Risk by Running DasBlog on Medium Trust?

If you are a serious Blogger; blog Professionally or if you Blog is really important, you risk a lot. I do NOT know the DasBlog code inside out; and I am NOT sure if I have done it justice by making the changes that are described in this manual.

As most developers reading this manual will be able to see, there are some places where I have made changes which cause Features to be reduced.  E.g. I replace the DatePicker used by DasBlog with a Normal Text box and "expect" the user to type date in MM/DD/YYYY format (or not type anything for Current system DateTime). Similarly, I faced problems in getting the blowery compression module to work in Medium trust and therefore I disabled compression and disabled zipping of log files. These things make DasBlog slower and it means you will have to archive and clear log files manually.

In this Manual I'll describe step by step, each line of code that I changed. At the end of the manual I'll also provide the complete dump of DasBlog build where I made changes while I was writing this manual. Developers can individually decide if the changes are "righteous" and if they want to run their Blogs on this version or changes suggested in this manual. Of course, they can make their own changes on the code before they deploy it - e.g. replace my text box with a custom javascript based datepicker etc.

Note: If you are a serious Blogger; or are able to move to a hosting provider which allows you to run under full trust, I HIGHLY recommend that you download Official DasBlog From http://www.dasblog.info/ and DO NOT use this manual / build.

If you decide to use this Manual / Build, please do understand that neither me nor the DasBlog team is responsible for the changes suggested in this manual or made in this build.

Why Make this Manual when Other DasBlog clones / forks Already exist?

I came across a couple of DasBlog forks that compile on .NET 2.0 and run in Medium trust environment. However, the inherent problem with these Forks was that they were not being updated as frequently as DasBlog releases were being updated. As a Geek / Developer, I wanted to take advantage of the rich feature sets of DasBlog 1.9 when it came out but the Fork that I was using just wouldn't update or reflect those changes. To make things worse the author announced that he would be 'moving away' from DasBlog feature set and developing his own features!

There was no manual of this sort or code-comments in the fork to let me help myself and change DasBlog 1.9 by redoing what the author of the fork had done and enable DasBlog 1.9 to run in Medium Trust Environment.

My First attempt was to support Medium trust in both 1.1 and 2.0 runtimes of DotNet. After days of effort I realized that because of the differences between different permissions associate with different trusts in 1.1 and 2.0 it would be slightly difficult to introduce these changes into DasBlog in a short span of time and yet keep DasBlog as stable as it is right now.

Which is why I decided to introduce Medium Trust in DotNet 2.0 only and document each and every change that I make so that anyone interested in making these changes himself for future releases will be able to help himself and would not get stuck like I had got stuck with the fork I had used.

Is This Another Fork?

No! I have no intentions of adding my own features or moving away from DasBlog feature set / code. This is just a Manual that shows you how you can make DasBlog compile on DotNet 2.0 and make it run on Medium Trust if you really need to; and if provides you a 1.9.x build checked out of SVN trunk on SourceForge on October 15th 2006 along with all changes described in this manual as 'Sample Code'.

Because my own blog www.thousandtyone.com/blog now runs on DasBlog with Medium Trust there will be a shellfish interest in updating this manual each time a new release of DasBlog with considerable new feature that rock, comes out :)

Slowly and steadily, in the Distant future, if time (and my knowledge :)) permits and if I can make these changes worthy enough to be a part of DasBlog code base, without impacting it's rich feature sets and stability, I would be trying my best to merge these changes into the DasBlog code base IF possible, so that this manual does not have to be maintained separately and developers don't have to make these changes themselves each time they grab hold of the the latest DasBlog build from SourceForge DasBlog SVN.

Till that happens, this manual, frequent parallel builds and updates to this manual with Every major release of DasBlog will be continued as and when I get free time.

What will Developers Making these changes need?

Apart from IIS 6.0 and .NET 2.0 Runtime you will also need the Visual Studio.NET 2005 IDE.

Let's Get Started!

Getting the Code!

Our First objective is to get the latest DasBlog Source Code. You can either get it from Source Forge web site or refer to Scott Hanselman's post on how you can get the latest build from SVN and build it.

Breaking the Code!

The DasBlog is .NET 1.1 code. So, let's start out by breaking the code by 'trying' to convert it to .NET 2.0 and make it run in .NET 2.0. Let's use Visual Studio.NET 2005 IDE to open the solution file (DasBlog.sln).

Note: This Manual assumes that you are familiar with basics of .NET and IIS along with the steps required to setup DasBlog; e.g. It does not describe the process of Creating the DasBlog virtual Directory etc. and assumes that these steps have already been performed.

The Visual Studio.NET 2005 Conversion Wizard Starts Up. Continue to click next (till Finish) and let the wizard convert the DasBlog code to build on Visual Studio.NET 2005 IDE. (A Lot of the code will be messed up due to this conversion which we will fix later).

Congratulations you've successfully messed up a perfectly well-written piece of software :) - Now let's fix the things the wizard broke.

Let's Fix things Step By Step

Now as you try to build the converted code, you will get compile time errors. The following Table shows the error and how I fixed each one of those errors [Any better ways of fixing these errors are always welcome. Please feel free to email me at rajivpopat@gmail.com and I will update the Manual with the better fix].

Error Old Code New Code / Fix Remarks
Compile Error: Error 1 'Profile': member names cannot be the same as their enclosing type c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\dasblog\360428d9\9a5467c9\App_Web_n-tl1cb0.6.cs 24 I suspect the Profile Class (Code Behind for Profile.aspx is creating a conflict with ASP.NET 2.0 Profile Class.) Renamed The Class and Updated The ASPX file to point to the new class.
Files Changed:
Profile.aspx.cs
Profile.aspx
Changes commented in the files.
None.
Runtime Exception: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "newtelligence.DasBlog.Web.StringTables.StringTables.resources" was correctly embedded or linked into assembly "App_Code.gxesyo_b" at compile time, or that all the satellite assemblies required are loadable and fully signed. The Wizard Renamed The Resource Files DasBlog uses for internationalization and placed them in App_GlobalResources folder

Naming convention - stringtables.stringtables.[locale].resx In this step I renamed them to -
stringtables.ar.resx
Files Changed other than Resource File Names:
ApplicationResourceTable.cs
Changes commented in the file.

The Renaming of the files may not be easy to detect with SVN diff.

Runtime Exception: [HttpException (0x80004005): A page can have only one server-side Form tag.]
This is the problem with the ThirdParty Control - FreeText Box. The one shipped with DasBlog builds on 1.1.

Obtained the latest version which is built for .NET 2.0 from http://freetextbox.com/download/.

Replaced DLL. Restarted IIS. Cleaned Up C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\
Temporary ASP.NET Files

This step along with the next step seemed to Do the Trick and fix the problem 'mysteriously'. Need to Evaluate - Exactly what caused this problem and exactly what fixed it.
Logical Errors: visual studio turned Autowireup = true by removing autowireup = false. that means all explicit event delegation information in DasBlog code was lost. Let's Undo what the wizard did.
Looked for [<%@ Page language="c#"] - and [<%@ Control Language="c#"] Find in all files

Did Global Replace for [<%@ Page language="c#" AutoWireup = "false"].

And A Global Replace - [<%@ Control Language="c#" AutoEventWireup="false"]

Then went into each and everyone of the files that were changed by this global replace (specially code-behind files) and replaced the InitializeComponent of each and every code behind with the original InitializeComponent function of DasBlog Code, code to properly wire-up events of controls .

Tested thoroughly to see nothing broke.

Now we Build and Run DasBlog - Congratulations! We're Half way there! We have a version that compiles on 2.0! Took a Backup and Proceeded to Next Step!

Let's Set Trust Level to Medium and Web.Config and See What Breaks!

Let's set the trust level to Medium by Modifying the Web.config file and see what breaks. The following Table shows the error and how I fixed each one of those errors [Any better ways of fixing these errors are always welcome. Please feel free to email me at rajivpopat@gmail.com and I will update the Manual with the better fix].

Error Old Code New Code / Fix Remarks
Compile Error: Error 101 The current trust level does not allow use of the 'compilerOptions' attribute D:\opensource\dasblog\source\newtelligence.DasBlog.Web\Web.config 83
<compilers>
<compiler language="c#" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" extension=".cs" compilerOptions="/d:DEBUG;TRACE"/></compilers>
<assemblies>
Commented This out. None.
Runtime Exception: GetSiteConfig causes an exception
code changed:
public static SiteConfig GetSiteConfig(string configPath)
{
...;
}
changes extensively commented in: SiteConfig.cs
Exactly Similar changes in RobotDefinition.cs
(This is a problem with Inheritance demand being placed)
Not sure of the impact this has on entire code base. Did some basic testing and it seemed to work fine.
Runtime Exception: XML Document is Invalid XML Deserializtion Error - XML Document is Not Valid at line - config = ser.Deserialize(reader) as SiteConfig; Changed Build To Release Mode. This looked like a configuration issue on the development box. Need to investigate.
Runtime Exception: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. Issue with the DatePicker Replaced the DatePicker with a Normal TextBox. You may prefer to replace with a JavaScript DatePicker; my objective was to analyze what it would take to move DasBlog To medium trust and I didn't want to digress by developing a new DatePicker.
Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed -

timeZones = LoadTimeZonesFromRegistry();
This isn't allowed in Medium Trust.

Made changes to Read Time Zones Only From XML.

Added a Public Constructor for TimeZones

Converted the Internal Constructor of public WindowsTimeZoneCollection to public.

Changed:
TimeZones.cs
WindowsTimeZoneCollection.cs
Changes commented in File.

Not sure if this has impact on other parts of the code base.
Runtime Exception: That assembly does not allow partially trusted callers The Blowery compression module which does not seem to work under medium trust. Removed all sections pertaining to Blowery Compressions from Web.config Other clones like thinkJot are running Blowery compression in Medium trust. Need to spend more time to figure out how.
Runtime Exception: 'drpEntryEditControl' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Iteration through DLLs and use of reflection for other external components that might be creating a problem in Medium Trust Level Environment. Changed EditConfigBox.cs - Changes commented in file. May have impact on the rest of the code base. Features Impacted.
Runtime Exception: System.Security.SecurityException: Request failed.

XmlNamespaceUpgradeReader places inheritance demand which causes problem in medium trust.

DayEntry.cs - Similar problem with Inheritance Demand on XmlNamespaceUpgradeReader

DayExtra.cs - Same as above. (3 Changes - Commented).

RobotDefination.cs - Same as above.

Not sure if not changing the XML Namespace will have impact on the rest of the code base.
Runtime Exceptions with SharpZipLip: (Based on comments provided by other Forks) Logging Data Service which uses: ICSharpCode.SharpZipLib Commented Archiving of logs. Occurs in loss of functionality.
Runtime Exception: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed Trace.Write creates problem at Medium Trust Level. Commented all Trace.Write lines in ErrorTrace.cs Debugging at Medium Trust Level Might be difficult.
Runtime Exception noticed by Marc Mercuri

Running DasBlog on Vista or on some hosts throws an exception when the Add Entry in the Admin Section is clicked. This is because CultureInfo.GetCultures(CultureTypes.AllCultures) seems to return two Culture Info's with the same NativeName (which is being returned in Chinese).
EditEntryBox.ascx.cs
sortedList.Add(ci.NativeName, new ListItem(langName, ci.Name));
As suggested by Marc - changed the code to:
if (!sortedList.Contains(ci.NativeName))
{
sortedList.Add(ci.NativeName, new ListItem(langName, ci.Name));
}
This fixes the problem. Can someone who understands Globalization (and Chinese) better than me, do a root-cause analysis and provide help? For now, the fix works just fine.

Note: All changes Made in the code base have been Uploaded to [http://www.thousandtyone.com/externalsupport/DasBlogBuilds/DasBlog-For-DotNet2.0-MediumTrust(Unofficial-Build-1.9.x).zip]. The Zip also contains corresponding SVN folders so that developers can easily Diff my changes with the DasBlog code base.

Each line of code I've changed contains a comment that starts with "rajivpopat@gmail.com" - so it's easier for developers to do a global search on the entire solution where changes have been made.

Thanks

Special Thanks to Scott Hanselman for helping me in getting started with this and then helping me with tips and ideas when I got stuck. Thanks to the entire DasBlog team for writing a great piece of software!

Comments?

Questions? Comments? Suggestions? Ideas? I would love to hear from you. Are there some of these changes described in this manual that you feel can be done better? Are there some stupid mistakes I've made? Things I've overlooked? Major DasBlog Features I've broken? Please drop me a line at rajivpopat@gmail.com - I would love to hear some comments, criticisms and how this manual and code changes can be improved.

Next Steps?

I'll be trying my best to see if these changes can be pushed into .NET 1.1 version of DasBlog and can be made a part of the DasBlog code base so that this manual does not have to be continued.  I've spent quite a bit of time on this already and haven't found an easy answer yet. If there's anyone who feels this can be done, I would like to work with him / her and help in anyway I can.