Accessing item definition from another assembly in console application

Topics: Developer Forum
Dec 21, 2008 at 10:36 AM
Edited Dec 21, 2008 at 10:53 AM
Hello people,

got the following problem:

I'm building a console application for the purpose of data loading (you can think of it as a simplified case of N2.Test project). Because i want to access an ordinary preconfigured N2 database from there, i need to have item definitions from N2.Templates loaded. In my console app i'm using the same .config as my web app. Also, i am accessing N2 Engine via N2.Context.Current property, which should work normally, as there is a code piece which tries to resolve a configuration correctly depending on whether or not it is running under ASP.Net runtime.

I've added a reference to N2.Templates assembly from my console app, but when trying to access N2.Context.UrlParser.StartPage property, i'm getting a familiar exception:

Object with id: 2 was not of the specified subclass: N2.ContentItem (Discriminator was: 'StartPage')

I've traced this problem to the GetAssemblies() method of N2.Engine.AppDomanTypeFinder class -- evidently, a referenced N2.Templates assembly gets not loaded into AppDomain upon start up. I could easily forge a patch for N2 to make my scenario working, but first i'd like to know: 

Am i missing something obvious to make a definitions from another assebly be available in a console application ?

Update: there's no need to fix N2, the quickest hack is to prepend these two lines to Main(..):
System.Reflection.Assembly.Load("N2.Templates");
System.Reflection.Assembly.Load("N2.Edit");
Coordinator
Dec 21, 2008 at 3:27 PM
Since you already have a working solution I'll just fill in with an alternative approach, configuration:

<n2>
  <engine>
    <assemblies>
      <add assembly="N2.Templates" />

In the web scenario WebAppTypeFinder will search for dlls in ~/bin and load them into the app domain. Outside ASP.NET it's much less clear where to look for plugins.