How to debug a module

Topics: Customizing Orchard, Writing modules
Apr 22, 2013 at 10:28 AM
I really need to debug a module but can't find any information on how to do this. The presumption seems to be that people just know how to work with a project like Orchard. I have done a fair bit of work with ASP.NET MVC, but Orchard is different, and I don't understand how I should go about debugging my modules.

Is there any article I can read up on this?

Many thanks

Apr 22, 2013 at 8:13 PM
I'm not sure what you mean, attach the debugger to IIS, add your breakpoint in the controller or driver class and F10/F11 your way to the problem ^_^

Or am I missing your point?
Apr 23, 2013 at 6:59 AM
So do I attach to the IIS process?
Apr 23, 2013 at 7:12 AM
Apr 23, 2013 at 12:39 PM
Edited Apr 23, 2013 at 12:41 PM
OK, so I need a bit of help with this:

My module works fine once the migration runs. I know this because at present I can:
  1. Download the sql compact db from the live site.
  2. Put the sql compact db in the source code where I developed the module and run it. This runs the migration whereas the package doesn't.
  3. Upload the db back to the live site.
  4. Open the site and voila, my module is running.
Obviously, I don't want to do things this way, I want my package to run properly.

So the problem I have is how do I see where the package is failing? Because before the package runs, I don't have any files to put a break point in. Therefore I can't debug it.

Presumably, the answer to this is:

a. Install the package. Although the migration doesn't run, the package does install the files.

b. Therefore, I can set a break point in the migration.cs and as the migration hasn't run, the breakpoint should reveal the problem.

Any other suggestions welcome.
Apr 24, 2013 at 2:24 AM
I don't understand what prevents you from attaching VS to the process and putting a breakpoint in the migration.
Apr 24, 2013 at 2:29 AM
You cant really debug your site when it is live, if that is what you mean.

If it works locally but is failing on the live site, it isn't likely that the problem is your migrations file. Have you checked the error logs? They are always your best friend in Orchard
Apr 24, 2013 at 6:50 AM

I just need to work on a local copy to see what is going wrong. The situation is as follows:

In the copy of the source code where I develop the module, it works fine. runs the migrations correctly, etc. So there can't be any problem with the migration code.

Next, I package up my module and try to install it on, say, a site run from WebMatrix. Or run from my local pc using Visual Studio. Or on a live site online. It doesn't make any difference. The migration doesn't run. Zilch in the logs. Nada. Rien. My best friend fails me.

Other modules I have developed work fine.

So my problem is down to being able to debug this module.

See my answer to Bertrand below.
Apr 24, 2013 at 6:51 AM
Are you sure that it hasn't, in reality, already run?
Apr 24, 2013 at 6:55 AM

OK, this is my problem:

Initially, the migrations code is in the package, so no code to put a breakpoint in.

Running the package DOES install the files, even if the Migration doesn't run. So AFTER installing the package, yes, of course, I can put a breakpoint in.

Problem is, the breakpoint is never hit. As in never.

So, is there anywhere I can put a break point in code that would call the migrations class?
Apr 24, 2013 at 6:57 AM
Well, no, I'm not. But there is no sign in the db that it has. And as I have no files installed to debug at that point, how would I know?
Apr 24, 2013 at 7:50 AM
Just a thought.... are you able to install the site on a local developer version of sql server then run the sql profiler whilst installing the module. This won't give you any breakpoint availability, but may help in seeing if it tries to run in the migrations.

It does sound a bit suspicious that it works within the source you developed against. Assuming there have been no source changes, and without seeing code / dev setup it smells a bit like a permission issue to me.
Apr 24, 2013 at 8:12 AM
alanevery: this is the migrations code. I can't see how permissions could affect it. All the sites are using, at present, Sql Server Compact Edition.

using System;
using Orchard.ContentManagement.MetaData;
using Orchard.Data.Migration;

namespace Wingspan.CorporateEmails {
public class Migrations : DataMigrationImpl {
    public int Create() 
        SchemaBuilder.CreateTable("CorporateEmailRecord", table =>
                                                                        .Column<int>("Id", col => col.PrimaryKey().Identity())

        SchemaBuilder.CreateTable("EmailMessageRecord", table =>
                                                                    .Column<int>("Id", col => col.PrimaryKey().Identity())

        ContentDefinitionManager.AlterTypeDefinition("CorporateEmailsWidget", cfg => 
                                                                                        .WithSetting("Stereotype", "Widget")
        return 1;
Apr 24, 2013 at 10:44 AM
Sorry I meant file / folder permissions. As you mention it works on your existing source, but not on another installation I wondered if you had issues with App Data permission in relation to the user on the current web thread
Apr 24, 2013 at 2:42 PM
No, because other modules install OK. Thanks for trying.
Apr 25, 2013 at 2:02 AM
You can know if your migration has run (and roll it back to a previous number) by looking at the Orchard_Framework_DataMigrationRecord table.
Apr 25, 2013 at 7:20 AM
That is how I know it hasn't run.
Apr 25, 2013 at 5:19 PM
Does the module build?
Apr 25, 2013 at 8:57 PM
Yes, and once, by hook or by crook, the migration runs, it runs perfectly. Very useful module that all my clients love.
Apr 25, 2013 at 8:59 PM
I'm sorry, I have nothing else to suggest. I've never seen anything like that happen.
Apr 25, 2013 at 9:09 PM
Well, thanks all the same. At least I feel less of a numpty.

Is there anyone who would like to look at the module code and see if they spot the problem? £100 to the winner...
Apr 25, 2013 at 9:21 PM
I'm still not sure what the issue is exactly: is the issue that you don't know how to step-through migration classes, or that the migration doesn't run (and that you use crooks to make it run anyway? :)
Apr 26, 2013 at 8:32 AM
I copied your create code into my current project and reran the installation. As every true developer would say... 'it worked on my box'

On every installation I've managed to hit a breakpoint within the code and it has created the tables as required. Without seeing the full code etc. it really feels like an environment specific thing (permissions) to me as I can't find anything wrong with your code, or replicate the issue
Apr 26, 2013 at 1:53 PM
Ive written a quick guide that may help you get visual studio debugging the code:
Debugging Help

You could get the Logger to write some dubug messages at each migration and the log may reveal how successful it is.
One problem I keep stumbling across is during the migrations cycle lets say 1 to 3 complete (create some tables etc and some part definitions) this is essentially successful but when step 4 fails the migrations reset back to the first one of the run (now with a minefield of completed tables that will stop migrations even lower at the next step). Re-running the module migrations would then obviously fail on the first step at creating the table.

Possible solutions:
Table creation: wrap table creations in try & catch. But i avoid this unless there are tables created and its easier to wrap the migration level than remove x amount of tables.
part and type definitions: check and add/remove as necessary.

Does the content type already have any of those part(s)? Orchard really doesn't like it if you have two parts with the same name autoroutePart and autoRoutePart even if its added accidentally.
Apr 26, 2013 at 3:52 PM

Thanks for trying, much appreciated. However, it fails on every box, web servers, etc, that have .NET 4.5 installed. It works on boxes with .NET 4 (or it used to...). Go figure.

However, in terms of permissions, I can't see that as my other modules install fine.

Just one of those mysteries of (a developer's) life.

But thanks for trying.
Apr 26, 2013 at 4:01 PM

You guide looks interesting, but I am one of those people who combine writing orchard modules and MVC business apps with being a professional artist as well as tree felling (yes, I'm a lumberjack). Today was a chainsaw day, so am knackered.

I will look at getting the logger to do things for me. I like that idea, sounds a great idea and useful for the future.

As regards the parts, the problem is happening with new installs, so I can't see that duplicating things is the cause.

For now I have managed to fudge the whole thing. I use my clients db with the source code installation where the module was developed and just running that installs the module correctly. I then reload the modified db back to the clients site. Very risky, I know, but I did back the db up...

I need to finish a couple of websites for clients, then put some self training time in to make sure that future jobs go smoothly. Your suggestions and guide will be at the top of the list for when I have two weeks of joy.

And if that doesn't work, I will take a chainsaw to the orchard and go learn PHP and WordPress.

Many thanks for your suggestions.
Apr 26, 2013 at 6:15 PM
Edited Apr 26, 2013 at 6:18 PM
Aha! Fails on 4.5, not 4.0? Now that's interesting. In theory they are compatible. One thing to check however would be web.config problems.

(and by the way, he's a lumberjack and he's ok)
Apr 26, 2013 at 10:07 PM

The web.config is unmodified from the download.

Unlikely, therefore.

Also, all other modules install without a hitch.
Apr 26, 2013 at 11:04 PM
Just as a very quick shot in the dark - can you give this a try on the server on a test site possibly:
(it shouldn't break anything and is quite easy to switch back) and I give it a low chance of fixing anything but its my goto setting for things breaking between .NET 4 and 4.5!

Open IIS -> Visit Application Pools -> select the application pool that an Orchard site is running on -> stop the application pool -> Open Advanced Settings -> For the option "Enable 32-Bit Application" set the value to false -> start the application pool.

Reason: Its fixed some problems with Orchard Sites mainly under V1.4 to run once 4.5 is installed but has also allowed me to run some modules of that era that have tagged along that are running with newer versions of Orchard. Its also reminded me of a couple of compiled library dlls that I needed to switch in some ancient modules.
This has also led me on the path to fix a gallery module (i cant remember which one now) which had curious compile settings in the project file for targeting specific architectures which Orchard became a little confused over after compilation.
Apr 26, 2013 at 11:05 PM
Apr 26, 2013 at 11:14 PM
Hazza wrote:
Yes that one i think!
Apr 27, 2013 at 12:36 AM
Not so unlikely as there are known problems with the default web.config on the latest IIS.