Fixing Tridion Content Delivery Deployment Errors: “Unable to load DLL ‘xmogrt’” and “System.BadImageFormatException”

I’ve recently helped a colleague with an issue when trying to deploy our Tridion DD4T .NET project to a Dev machine.   The issue was a “System.BadImageFormatException” related to the Tridion’s Java wrapper Juggernet.  

However, at first we were getting the error: “Unable to load DLL ‘xmogrt’: The specified module could not be found. (Exception from HRESULT: 0x8007007E)”.  It was weird because on my VM, with the exact same files in the bin, it ran fine.  Turns out, it worked on my VM because my %TRIDION_HOME% was set with the file in it.  

We followed the answers in this post (which you’ve probably read already before coming to this article: http://stackoverflow.com/questions/13918310/unable-to-load-dll-xmogrt-from-tridion-metadata-query) and placed the xmogrt.dll and its dependencies in the bin.  However, we now ended with the “System.BadImageFormatException”:

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Server Error in '/' Application.

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Here is how we solved the problem: 

  1. Uninstall all Java (you may have several versions of the JVM installed.  Uninstall all of them)
  2. Install the latest Java 6 JRE (though we installed the JDK 1.6_45).
  3. Check that it’s successfully installed by typing java –version from the command line.
  4. Verify that ‘xmogrt.dll’ is in the bin folder of your app and is not corrupt.  In our case the ‘xmogrt.dll’ file was only 840kb for some reason.  It should be 1630 KB.
  5. Do an iisreset.

Here is the long answer

xmogrt.dll is an unmanaged C++ assembly that is largely the Juggernet Java wrapper used by Tridion (if you’ve wondered about those jar files in the bin/lib).  This compilation version of this DLL must match the CPU instructionset, i.e. x86 or x86-64).  It must also match the latest Tridion-supported Java version, i.e. 1.6_xx x86 or 1.6_xx 64-bit (of java 1.7 for Tridion 2013).  If there is a mismatch in either, then you’ll receive the “System.BadImageFormatException” error.Look at the Juggernet documentation for more details about how Juggernet works and this error here: http://codemesh.com/products/vfour/tutorial/dotnet/v3/L_02_run_app.html, scroll down to the section “Common Runtime Issues You Might Encounter”.  Also, the Juggernet architecture diagram (found here: http://codemesh.com/products/vfour/dotnetruntimes.html) provides a nice deal of insight into understanding how the Tridion CD API is converted to .NET.

 

Issues with the TridionRsaContainer registration!

Scenario

We’d copied a CM instance (VirtualMachine) from the PRD environment and placed this into the UAT environment, all configurations done as we’d tested and expected.

To avoid getting the error stating that the [clientUATdomain]\MTSUser does not have access to the tridion.security configuration setting we executed the TridionRsaContainer command

aspnet_regiis -pa "TridionRsaKeyContainer" "[clientUATdomain]\MTSUser"

This failed with the MTSUser. On confirming we had the correct [domain]/username/password and further investigation we found that no-one knew the actual account that had been used to install SDL Tridion 2011 CM. We needed this account so we could log into the UAT CM machine in order to give the MTSUser access to the tridion.security configuration setting. This is because this config setting is protected using this windows functionality : http://msdn.microsoft.com/en-us/library/yxw286t2(v=vs.100).aspx

Solution
We could see that file in question is actually a file in c:\programdata\microsoft\crypto\rsa

This file is accessible only to a small number of users (the production domain mtsuser had access to it). So we tried this:

  • log in on the PRD machine as the production domain mtsuser
  • export the rsa key via this command
aspnet_regiis -px "TridionRsaKeyContainer" keys.xml -pri

We then placed this keys.xml file onto the UAT CM box and then executed the import command

aspnet_regiis -pi "TridionRsaKeyContainer" c:\temp\keys.xml

and finally, the following command

aspnet_regiis -pa "TridionRsaKeyContainer" "[clientUATdomain]\MTSUser"

So, on booting up the CM Browser… boom… We got an error in the GUI after a quick dig into the respective ‘Tridion Configuration’ error in the Event system … we then executed

aspnet_regiis -pa "TridionRsaKeyContainer" "nt authority\network service"

Quick restart of the services and all is well again.

The motto of the story – DO NOT ‘LET SLIP‘ WHICH USER INSTALLED THE CM - especially if you want to clone the machine during a setup!

If you’ve had a similar experience we’d be very interested to hear if there are other ways around this – other than simply re-installing the CM with a noted user :)

Special thanks to Harald Hoffelinck on this solution!

Extracting Keywords from PDF on Multimedia upload

I came across a nice library that allows you to create and manipulate PDF documents. Apart from creating PDF documents from scratch, you can also read existing ones, convert XML to PDF, fill out interactive PDF forms, stamp new content on existing PDF documents, split and merge existing PDF documents, and much more. The best part of it is that there is a C# port available which is open source, it’s called iTextSharp. Now I haven’t explored all features of it, like PDF creation, but so far it already looks very usable.

Continue reading

Translating Page URLs without Localizing Pages

A piece of advise that I learned from a wise Tridion sensei is that it’s not a good practice to localize pages in Tridion if you have a multi-lingual site (though there is an exception to this rule which I’ll discuss below).  The reason for this is simple: you lose the ability to centrally manage your site from the Master web publication.  Once a page is localized, you have to manage it individually, which is not ideal if you want to centrally update component presentations on a page across all the language sites.  So what do you do if you want to obey the rule of not localizing pages and your customer asks to have localized/translated URLs? 

Continue reading

Creating dynamic labels within SDL Tridion content

Recently a client required a way of creating and storing ‘dynamic’ values within the content managed in SDL Tridion.  By ‘dynamic’, this could be anything from loan rates, petrol prices or the cost of a pint of beer.  So today ‘pint_beer_cost’ could be $3.50, tomorrow it could be $4.50.  Having to go through all stored content and replace $3.50 with $4.50, every time there is a need to change a value would be quite painful.

Continue reading

Where am I? Skinning the SDL Tridion Content Manager Explorer.

To provide context between similarly-looking SDL Tridion environments, consider Skinning the Content Manager Explorer as described on SDL Live Content.

Simplified, slightly modified, and with some pics. This is a fairly quick and easy way to make your CMS environments stand out from each other. For the full power of CME Themes, head on over to YATB.
Continue reading

Extending the SDL Tridion dashboard in both the CME and Experience Manager

When you open up the SDL Tridion UI for the first time you are directed to the SDL Tridion tab which shows you a welcome screen, this is sometimes also referred to as the SDL Tridion dashboard. If you open the Experience Manager view, you actually see that the first tab is called “Dashboard”. This dashboard contains useful information for an editor and is even extensible, allowing for a rich user experience on your extensions. Typical things are settings for your own extensions, or perhaps user specific pages (like a custom page, but then tailored to the logged in user) since you have all the UI capabilities available here.

Extending the SDL Tridion dashboard is all about DeckPages, OptionsPanels and ExtendedAreas, basically nothing more than a couple of TridionUserControls or .ascx pages. To explain the possibilities I’ve created an example UI extension, which adds a new option to the SDL Tridion dashboard OptionsPanel (menu item on the left hand side of the dashboard) in both the Content Manager Explorer (CME) and the Experience Manager (SiteEdit). This option loads a DeckPage when selected, and inside that DeckPage you can add user controls through ExtendableAreas. Below is a screenshot of how this extension looks in the CME.

Stack Overflow dashboard

Now let’s start with taking apart the main deckpage, it is a very simple user control which exposes two extendable areas, one for the buttons and body part for the content. The html of this deckpage (an .ascx) looks as follows:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="Example.Controls.DeckPages.MainDeckPage" %>
<div id="MainDeckPageOverlay" class="stack horizontal">
  <div class="label stack-elem">
    <asp:Label runat="server" Text="Stack Overflow" />
  </div>
  <div class="separator line stack-elem"></div>
  <div id="MainDeckPageButtonsHolder" class="line stack-elem">
    <c:extendablearea id="MainDeckPageButtons" runat="server"></c:extendablearea>
  </div>
  <c:extendablearea id="MainDeckPageBody" runat="server"></c:extendablearea>
</div>

To add the buttons to the page, we need to look at the configuration of the UI extension (an editor extension). Since our deckpage defined two extentable areas, we can now simply add a button under the ext:extendableareas element in the configuration, applying it to the TridionDashboard view (or DashboardView for SiteEdit) and for the control, we reference the id of the extendablearea control (MainDeckPageButtons in this example). See below a snipped of the configuration, adding the first button.

<ext:extendedareas>
  <ext:add>
    <ext:extension assignid="FirstButton" name="Newest">
      <ext:control>~/Controls/ExtendedAreas/FirstButton.ascx</ext:control>
      <ext:pagetype></ext:pagetype>
      <ext:renderinblock>false</ext:renderinblock>
      <ext:apply>
        <ext:view name="TridionDashboard">
          <ext:control id="MainDeckPageButtons" />
        </ext:view>
      </ext:apply>
    </ext:extension>
  </ext:add>
</ext:extendedareas>

The button itself is actually another deckpage or user control, containing a button control in its html. Below the buttons there is another extendable area in which we can place the content for the selected button. This is the same as with the button, but now we apply it to the control with the id: MainDeckPageBody. This user control loads another deckpage which contains our actual content, it does that by adding a deckpage control:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="Example.Controls.ExtendedAreas.FirstButtonWidgetsView" %>
<div class="widgetsviewfilter" id="FirstButtonWidgetsViewFilter" style="height: 25px;">
    <div id="FirstButtonWidgetsViewFilter_addressBar" class="addressbar" c:separator="" c:itemsize="20">
        <span class="addressbaritem home" c:uri="edu:training" title="First (edu:training)" tabindex="1">First View</span>
    </div>
</div>
<c:deck runat="server" id="FirstButtonPages" class="stack horizontal">
    <c:DeckPage runat="server" id="FirstButtonPage" SourceEditor="Example" ExternalControl="~/Controls/DeckPages/FirstButtonPage.ascx" PageType="FirstButtonPage" />
</c:deck>

So far we have used 4 different deckpages (user controls) and with that, we have extended the options panel (left hand navigation) and added a button with a content area below it. Inside the actual page for the first button (FirstButtonPage.ascx) there is an iframe in which I load my content to keep this example simple. But basically you can build up this page like any other UI page, with (custom) user controls.

To wire it all up, there is a JavaScript and Stylesheet file available for all user controls (.ascx pages). These files are grouped together using the editor configuration file again. Here is an example of the group for the main deckpage.

<cfg:group name="Example.Controls.DeckPages.MainDeckPage">
  <cfg:fileset>
    <cfg:file type="script">/Controls/DeckPages/MainDeckPage.ascx.js</cfg:file>
    <cfg:file type="style">/Controls/DeckPages/MainDeckPage.ascx.css</cfg:file>
  </cfg:fileset>
</cfg:group>

The name of the group is used in the code behind (.cs) file for the MainDeckPage.ascx, to reference its control resources, through this mechanism the JavaScript and Stylesheet files are loaded inside the SDL Tridion UI and made available. The ControlResources attribute is used for this as follows.

using Tridion.Web.UI.Controls;
using Tridion.Web.UI.Core.Controls;

namespace Example.Controls.DeckPages
{
  [ControlResources("Example.Controls.DeckPages.MainDeckPage")]
  public class MainDeckPage : TridionUserControl
  {
  }
}

As you can see there is no further C# code for the deckpage, all of the logic is handled in the JavaScript file. I won’t try to explain all the JavaScript code in this article, but will let the code speak for itself. I’ve placed the entire example UI extension on SDL Tridion World, complete with all source code, so you can install it and look for yourself what does what. Feel free to take it apart completely or reuse as a basis for your own UI extensions.

What was most revealing to me, while building this extension, was the fact that it shows how the UI extension framework really works. By adding extendable areas to your own deckpage, you are making your extension extensible itself. It’s directly used for adding the buttons and deckpages for the button content, but you also made it possible for somebody else to extend your extension again.

Avoid Static Variables in Tridion Templates

I’ve recently helped a client resolve some interesting issues with their Tridion template building blocks. The issue was that when publishing the first time desired results were produced by the template. However, when publishing many pages, or republishing the same page/component presentation, subsequently produced strange output results.

The logic in our TBB parsed the Output item in the package and made various manipulations to it. I won’t get into the details of that. The issue was due to having static private variables declared within the TBB. These variables were various data structures and primitives manipulated by methods within our ITemplate-inherited class. The methods themselves had to also be declared as static.  Refactoring the code away from “static” resolved the issue.

Here is why you cannot use static variables in Tridion templates:  Primarily because they become global shared variables across all publish threads.  Here is what the Tridion TOM.NET Session and Engine class API states regarding this matter:

IMPORTANT NOTE: Session objects are not thread-safe; you must not share Session objects across threads. Furthermore, the thread that creates a Session object should be in a Single Threaded Apartment (STA), since the Session will internally create an Apartment Threaded COM object. See SetApartmentState(ApartmentState). 

The Tridion Session object, which the Engine uses (or, as the docs put it, “aggregates”) subscribes to the Single Threaded Apartment model (STA).

An STA is basically a model where the server controls the threads, and each individual thread must not spin off additional sub-threads that depend on the STA-controlled objects. So in our case: the Tridion CM is the server that spins off and controls threads (e.g. we can publish many items simultaneously), and each template is executed within it’s own thread. So if we develop a template that spins of other threads, the common memory across threads, i.e. static variables, isn’t managed.  So STA threads can overwrite each other.

Here is a snippet from an article explaining STAs in much more detail:

…all access to global variables and functions of the server will need to be serialized properly because more than one object may try to access these from different threads. This rule also applies to class static variables and functions. [http://www.codeproject.com/Articles/9190/Understanding-The-COM-Single-Threaded-Apartment-Pa]

So in a common scenario, when is it appropriate to use “static” within a TBB? Well, how about in procedural methods that take some inputs, create new objects, massage them and return them. In other words, if the method is self-contained. A Utility class is a common such scenario.

In conclusion, don’t use static variables inside your TBBs or Event System code.

Quick Tip: Turn off verbose template logging in Tridion Event Log

As a good developer, you write out useful information and debug messages in your .NET TBBs using the built in templating logger. This is great when running the TBBs in the template builder to give a bit of extra info about the processing. However, by default info messages will be logged in the Tridion Event Log of your Publisher server, causing log bloat, and making it harder to see important Error and Warning messages. Its really easy to turn this off, but I am not sure I have seen it documented anywhere. Heres how…

Continue reading