public Boolean Register(IMessageTarget target)
        {
            String id = target.Id;

            if (String.IsNullOrWhiteSpace(id))
            {
                return(false);
            }
            lock (_regedit)
            {
                if (_regedit.ContainsKey(id))
                {
                    throw new InvalidOperationException($"The id {id} has been Existed");
                }
                _regedit.Add(id, target);
            }
            AddinProxy proxy = target as AddinProxy;

            if (proxy != null)
            {
                proxy.MessageService      = this;
                proxy.Owner.DomainUnload += OnDomainUnload;
            }
            target.MessageDispatcher?.Start();
#if DEBUG
            Console.WriteLine($"Register target.{target.Id}");
#endif
            return(true);
        }
        public void Unregister(String id)
        {
            if (String.IsNullOrWhiteSpace(id))
            {
                return;
            }
            IMessageTarget target = null;

            lock (_regedit)
            {
                if (!_regedit.ContainsKey(id))
                {
                    return;
                }
                target = _regedit[id];
                _regedit.Remove(id);
            }
            target.MessageDispatcher?.Stop();
            AddinProxy proxy = target as AddinProxy;

            if (proxy != null)
            {
                proxy.MessageService = null;
            }
#if DEBUG
            Console.WriteLine($"Unregister target.{target.Id}");
#endif
        }
 private static void SendMessageImpl(Message message)
 {
     if (AppDomain.CurrentDomain.IsDefaultAppDomain())
     {
         MessageService ms = AppDomain.CurrentDomain.GetData("ms") as MessageService;
         ms?.Transmit(message);
     }
     else
     {
         AddinProxy proxy = AppDomain.CurrentDomain.GetData("proxy") as AddinProxy;
         proxy?.Send(message);
     }
 }
        public void Unload(IAddin addin)
        {
            if (addin == null)
            {
                throw new ArgumentNullException("addin");
            }
#if DEBUG
            Console.WriteLine($"Destroy addin {addin.Id}");
#endif
            AddinProxy proxy = addin as AddinProxy;
            if (proxy == null)
            {
                return;
            }
            AppDomain.Unload(proxy.Owner);
        }
        public static Boolean IsFromHost(this IMessageTarget target, Message message)
        {
            if (message.Source == MessageService.AddinHostId)
            {
                return(true);
            }
            String hostActualId = null;

            if (AppDomain.CurrentDomain.IsDefaultAppDomain())
            {
                MessageService ms = AppDomain.CurrentDomain.GetData("ms") as MessageService;
                hostActualId = ms?.GetHostActualId();
            }
            else
            {
                AddinProxy proxy = AppDomain.CurrentDomain.GetData("proxy") as AddinProxy;
                hostActualId = proxy?.GetHostActualId();
            }
            return(hostActualId == message.Source);
        }
        protected virtual IAddin LoadOnIsolatedAppDomain(Type addinType, AddinInfo info)
        {
            String         id    = info.Id;
            AppDomainSetup setup = new AppDomainSetup();

            setup.ApplicationName = id;
            setup.ApplicationBase = Path.GetDirectoryName(addinType.Assembly.Location);
            String selfConfigFile = addinType.Assembly.Location + ".config";

            if (File.Exists(selfConfigFile))
            {
                setup.ConfigurationFile = selfConfigFile;
            }
            else
            {
                setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            }
            AppDomain domain = AppDomain.CreateDomain($"AddinDomain#{id}", null, setup);

            try
            {
                AddinProxy proxy = (AddinProxy)domain.CreateInstanceAndUnwrap(addinType.Assembly.FullName, addinType.FullName + "Proxy");
                if (proxy.Id != id)
                {
                    throw new InvalidDataException("The id in config not match");
                }
                domain.SetData("proxy", proxy);
                domain.SetData("id", id);
                proxy.Owner = domain;
#if DEBUG
                Console.WriteLine($"Created isolated addin {proxy.Id} in {domain.FriendlyName}");
#endif
                return(proxy);
            }
            catch (MissingMemberException ex)
            {
                AppDomain.Unload(domain);
                return(null);
            }
        }