Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:15:30+00:00 2026-05-23T22:15:30+00:00

I am implementing a module system for my C# IRC Bot. The modules are

  • 0

I am implementing a module system for my C# IRC Bot. The modules are .dll assemblies which are stored in a subdirectory, “modules”, and they are used to add functionality to the bot, such as adding extra commands on IRC. These modules are designed to be loaded and unloaded at runtime so I can update the bot or fix bugs, without having to restart the entire application.

Currently the module system creates a new AppDomain for each module to be loaded, and a proxy to be created using CreateInstanceFromAndUnwrap inside a class called ModuleHelper.

AppDomain domain = AppDomain.CreateDomain(name, null, new AppDomainSetup 
{ 
    ApplicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName,
    ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
    DisallowApplicationBaseProbing = true,
    PrivateBinPathProbe = ModuleDirectory,
    PrivateBinPath = ModuleDirectory,
    ShadowCopyDirectories = ModuleDirectory,
    CachePath = Path.Combine(ModuleDirectory, "cache"),
    ShadowCopyFiles = bool.TrueString 
});
ModuleProxy proxy = null;
try
{
    proxy = (ModuleProxy)domain.CreateInstanceFromAndUnwrap(location, AssemblyName.GetAssemblyName(location).Name + ".Module");
    proxy.OnLoad();
}
catch
{
    AppDomain.Unload(domain);
    throw;
}

This proxy inherits from MarshalByRefObject.

public abstract class ModuleProxy : MarshalByRefObject
{
    internal protected virtual void OnLoad()
    {
    }

    internal protected virtual void OnUnload()
    {
    }
}

OnLoad and OnUnload are called when the module is loaded or unloaded.
Modules also inherit from MarshalByRefObject in the external assembly, such as this class in a module, ConfigurationReader.dll.

public class Module : ModuleProxy
{
    private Configuration _configuration = new Configuration();

    protected override void OnLoad()
    {
        string fileName = Path.Combine(ModuleHelper.ModuleDirectory, AssemblyName.GetAssemblyName(Assembly.GetExecutingAssembly().Location).Name + ".conf");
        _configuration.ReadAndLoadConfiguration(fileName);
        IrcBot bot = new IrcBot(_configuration);
        if (_configuration.Perform != null)
        {
            bot.EventManager.OnRegister += PerformOnRegister;
        }
        if (!string.IsNullOrWhiteSpace(_configuration.IdentifyMatchPeer + _configuration.IdentifyMatchText + _configuration.IdentifyPassword))
        {
            bot.EventManager.OnNotice += IdentifyOnNotice;
        }
        IrcBot.Bots.Add(_configuration.Id, bot);
        IrcBot.Bots[_configuration.Id].Start();
    }
...
...
...

The problem is, when I modify something that belongs in the main appdomain (specifically, adding a new bot to the IrcBot.Bots collection, IrcBot.Bots.Add(_configuration.Id, bot);) the IrcBot.Bots count is increased inside the secondary appdomain only, and not the main appdomain as I want it to be.

After doing a bit of Console.WriteLining, I have found that calling IrcBot.Bots.Count after the Add call in the secondary appdomain returns 1, and calling it again straight after the OnLoad call in the main appdomain returns 0. This has a disastrous effect, and causes the other modules that are loaded afterwards to malfunction. How can I update the bot count (among other things) in the main AppDomain after changing it in the secondary AppDomain?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:15:31+00:00Added an answer on May 23, 2026 at 10:15 pm

    As Juliet said, AppDomains are indeed isolated so that “static” variables are not visible from other AppDomains. A solution might be using cross-AppDomain singletons, as explained at http://jonathan.dickinsons.co.za/blog/2010/11/cross-domain-singleton-in-c/ and http://www.dolittle.com/blogs/einar/archive/2007/05/18/cross-appdomain-singleton.aspx.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implementing a 'sandbox' environment in Python used to be done with the rexec module
I'm implementing a tagging system for a website. There are multiple tags per object
In implementing hook_menu for a module, I am trying to put some items into
I'm implementing the auth module in Kohana, and I can't seem to figure out
I'm implementing a event system: Various pieces of code will post events to a
I'm implementing a forum system called rBoard. Code is viewable at http://github.com/radar/rboard . I've
Im working on a project where i am implementing a friendly url system using
I am implementing a complex search module with result page support paging. Most of
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
When implementing a needle search of a haystack in an object-oriented way, you essentially

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.