Where do I bind something that I would normally put in GLOBAL.asax.cs?

Topics: General
Nov 9, 2011 at 11:42 AM

I've seen some general discussion about where we put code normally intended for global.asax.cs, but nothing concrete that I understand or can deploy.  I've noticed that Orchard.Web (1.3) doesn't have a gloabl.asax.cs file, and creating one and then binding to it does not appear to have worked for me.  Is that the right thing to do?

Otherwise, how else might I bind some code for my module. I am using a pre-existing MVC3 project, and the binding code is in the global.asax.cs file. Should I use an App_Start file, and if so, should it be the App_Start at the root of Orchard.Web or should I create an App_Start folder at the root of my module?

Thank you for any guidance.

Nov 9, 2011 at 12:02 PM

 

http://blogs.msdn.com/b/davidebb/archive/2010/10/11/light-up-your-nupacks-with-startup-code-and-webactivator.aspx

Nov 9, 2011 at 12:16 PM

You can also implement IOrchardShellEvents and put code in Activating(). This will get executed one *for each shell* at app startup (this is important because if you have multiple tenants each one is a separate shell, you won't want the code running for tenants where your module is disabled, and you might want it to behave differently based on site settings).

Nov 9, 2011 at 2:28 PM
Edited Nov 9, 2011 at 2:29 PM

@infofromca:

Thanks for the link. I'll check to see if setting that up in App_Start works.

If not...

@randompete:

I see stuff like the following in Orchard.Blogs and Contrib.Taxonomies (both under Routings folder) as an example (following is from Orchard.Blogs):

        void IOrchardShellEvents.Activated() {            Refresh();        }
        void IOrchardShellEvents.Terminating() {        }

Couple of questions:

1) Where exactly would I call Activated()? Does it matter (Models, Bindings, Controllers folder? Or should I create a Routings folder like the examples above)

2) Should I have it in a separate file?

3) Do I need the Terminating() line? If so, I guess I could just leave it blank.

Thanks.

Nov 9, 2011 at 2:41 PM
Edited Nov 9, 2011 at 2:42 PM

You don't ever call Activated(); Orchard calls it for you when the shell starts up. By implementing the IOrchardShellEvents interface, you are hooking your code into Orchard's dependency injection. You can put the class anywhere you like in your module, it just helps to follow standard conventions for certain types of interface, but I haven't seen anything particularly consistent for shell events. I usually put these files in Services.

Terminating() is part of the IOrchardShellEvents interface so you have to implement it in order for you code to compile, but yes you can just leave it blank.

The easy way to implement an interface in VS is as follows. Start writing your class:

public class MyShellEvents : IOrchardShellEvents {
}

Now, if you click on the interface name (IOrchardShellEvents) you'll see a little coloured rectangle underneath the first letter; hover over that and VS will give you an option to automatically implement the interface. You can also do this with Shift+Ctrl+F11. It'll create all the necessary members of the interface, ready for you to add code.

Nov 9, 2011 at 9:18 PM
Edited Nov 9, 2011 at 9:19 PM

I was able to implement the interface as you suggested, but I am still having problems with my code (validation - semi-related to my previous post on validation to which you were responding; except now I'm on to new code and new errors trying to get a simple wizard working so I don't have a long form on one page).

Basically, I'm on my third wizard code that won't work.

Dec 8, 2011 at 9:22 PM

 

http://orchard.codeplex.com/discussions/264519