tags:

views:

631

answers:

7

A friend of mine is building a Content Management System in PHP.

PHP is a scripting language where code is evaluated at run-time, and therefore it is fairly simple to add code.. at run-time (think: a forum, a contact form, etc.)

Doing the same in ASP.NET (which is compiled).. not as simple.

Have you guys got any thoughts on how you would load code dynamically and use it within a live web application?

+2  A: 

I think you can use the App Code folder for this. I heard of a solution where the developers weren't allowed to upload dlls so they used the app code folder which is compiled just in time. So you can add classes to it and they will be compiled on demand.

See here

Mark Dickinson
A: 

The open source BlogEngine.NET has some code in it to allow developers to write extensions that you might find useful. The extensions get loaded in on application start via the Global.asax.

Rob West
+1  A: 

What you have to do is to make a plugin based structure

Plugin based programming is not something I can explain to you in an answer right here, but you create a Interface which the assemblies you want to use as modules (plugins) has to implement. You then load all the dll's in your plugin folder at runtime and use C# reflection, more specificly the Activator class.

Best of luck with this, it is the same principle as for Windows programming.

The real napster
+1  A: 

If you're using Visual Studio 2008, the default Web Site model doesn't use compiled dlls and just compiles the code-behind on the fly I believe. You can also use App Code folder as mentioned. Checking out BlogEngine.NET as mentioned is probably a pretty good starting point as well as they have a nice extension architecture that is compiled on the fly.

You can also just not have code-behind files and place the code in a <script language="C#" runat="server"></script> which I believe the Visual Studio Express editions do by default.

Blake
Yep, the "File | New... | Web Site" model will give you a "compiled on the fly" application.If you go for "File | New... | Project | Web | Web Application" then you will have a "compiled before deployment" web application.There are pros and cons to both really.
Zhaph - Ben Duguid
+1  A: 

I do it all the time for configuring providers.

try 
{
    string assembly  = ConfigurationSettings.AppSettings["DataProvider"].Split(',')[0];
    string classname = ConfigurationSettings.AppSettings["DataProvider"].Split(',')[1];
    m_Instance = (SqlDataProvider)Activator.CreateInstance(assembly,classname).Unwrap();
}
catch(Exception ex) 
{
    Common.Error.Write("Getting Data Provider From Config");
}

The config setting looks like :

<add key="DataProvider"   value="MyDll,MyDll.Data.SqlDataProvider"/>

But since it is a strongly typed language, your code would have to have references to the types or interfaces. You can't bring in code it doesn't know how to deal with. Unless you're looking to be coding all day in reflection.

Chad Grant
A: 

I answered my own question here.

roosteronacid