private void SetWatcher(bool refresh)
        {
            ChoConfigurationMetaDataManager.SetWatcher(this);

            if (ConfigSection != null)
            {
                if (!refresh)
                {
                    //Hookup the configuration watch job
                    if (_watchChange)
                    {
                        ConfigSection.SetWatcher(new ChoConfigurationChangedEventHandler(OnConfigurationChanged));
                    }
                }
                else
                {
                    ConfigSection.StopWatching();
                    if (ConfigObject is ChoInterceptableObject)
                    {
                        ChoInterceptableObject interceptableObject = ConfigObject as ChoInterceptableObject;
                        //((ChoInterceptableObject)ConfigObject).SetInitialized(false);
                    }
                }
            }
        }
Beispiel #2
0
        private ReturnMessage ExecuteMethod(ref IMethodMessage returnMsg, MethodCallMessageWrapper methodCallMsg, MarshalByRefObject owner, ChoMemberInfo memberInfo)
        {
            try
            {
                PreMethodInvoke(owner, methodCallMsg, memberInfo);

                returnMsg = RemotingServices.ExecuteMessage(owner, methodCallMsg);
                memberInfo.ReturnMessage = returnMsg as ReturnMessage;

                if (memberInfo.Info != null)
                {
                    memberInfo.MethodMsg = returnMsg;
                }

                PostMethodInvoke(owner, methodCallMsg, memberInfo);

                return(memberInfo.ReturnMessage);
            }
            catch (TargetInvocationException ex)
            {
                return(new ReturnMessage(ex.InnerException, methodCallMsg));
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                //Trace.TraceError(ex.ToString());
                ChoInterceptableObject interceptableObject = owner as ChoInterceptableObject;
                if (interceptableObject != null && interceptableObject.Initialized)
                {
                    if (interceptableObject.IsConfigObjectSilent)
                    {
                        ChoConfigurationObjectErrorManagerService.SetObjectMemberError(interceptableObject, memberInfo.Name,
                                                                                       String.Format(Resources.ConfigConstructMsg, ChoString.ToString(memberInfo.Value), ex.Message));

                        //PostMethodInvoke(owner, methodCallMsg, memberInfo);

                        //return new ReturnMessage(null, methodCallMsg);
                    }
                }
                string msg = "{0}: {1}".FormatString(memberInfo.Name, String.Format(Resources.ConfigConstructMsg, ChoString.ToString(memberInfo.Value), ex.Message));
                ChoValidationException ex1 = new ChoValidationException(msg);
                if (OnMemberError(owner, memberInfo, ref ex))
                {
                    return(new ReturnMessage(null, null, 0, null, methodCallMsg));
                }
                else
                {
                    return(new ReturnMessage(ex1, methodCallMsg));
                }
            }
        }
Beispiel #3
0
        public virtual bool PreMethodInvoke(object owner, MethodCallMessageWrapper methodCallMsg, ChoMemberInfo memberInfo)
        {
            MethodInfo methodInfo = (MethodInfo)methodCallMsg.MethodBase;

            ChoInterceptableObject interceptableObject = owner as ChoInterceptableObject;

            if (interceptableObject != null)
            {
                if (memberInfo.DirtyOperation && !interceptableObject.Silent)
                {
                    if (interceptableObject.PreInvokeInternal(memberInfo))
                    {
                        if (interceptableObject.Initialized)
                        {
                            interceptableObject.SetDirty(memberInfo.DirtyOperation);
                        }
                        else
                        {
                            interceptableObject.IsModified = true;
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            if (memberInfo.Info != null)
            {
                if (memberInfo.DirtyOperation)
                {
                    ChoValidation.Validate(memberInfo.Info, memberInfo.Value);
                    if (interceptableObject != null && interceptableObject.Initialized)
                    {
                        ChoConfigurationObjectErrorManagerService.ResetObjectMemberError(interceptableObject, memberInfo.Name);
                    }
                }
            }
            return(true);
        }
Beispiel #4
0
        public virtual void PostMethodInvoke(object owner, MethodCallMessageWrapper methodCallMsg, ChoMemberInfo memberInfo)
        {
            MethodInfo methodInfo = (MethodInfo)methodCallMsg.MethodBase;

            ChoInterceptableObject interceptableObject = owner as ChoInterceptableObject;

            //if (memberInfo.Info != null)
            //{
            //    if (memberInfo.DirtyOperation)
            //    {
            //        ChoValidation.Validate(memberInfo.Info, memberInfo.Value);
            //        if (interceptableObject.Initialized)
            //            ChoConfigurationObjectErrorManagerService.ResetObjectMemberError(interceptableObject, memberInfo.Name);
            //    }
            //}

            if (interceptableObject != null)
            {
                interceptableObject.PostInvokeInternal(memberInfo);
            }
        }
        internal void Refresh(bool refresh)
        {
            if (_inLoadingProcess)
            {
                return;
            }

            lock (SyncRoot)
            {
                if (ConfigObject is IChoConfigurationParametersOverridable)
                {
                    ((IChoConfigurationParametersOverridable)ConfigObject).OverrideParameters(this);
                }

                _modifiedStateObject.ResetModified();

                bool isDirty        = false;
                bool errorHandled   = false;
                bool canTraceOutput = true;
                bool hasErrors      = false;
                this[ChoConfigurationConstants.FORCE_PERSIST] = false;

                _inLoadingProcess = true;

                ChoConfigSection prevConfigSection = ConfigSection;

                if (ConfigSection != null)
                {
                    ConfigSection.StopWatching();
                }

                try
                {
                    if (ConfigObject != null)
                    {
                        //ChoConfigurationObjectErrorManagerService.ResetObjectErrors(ConfigObject);

                        if (ConfigObject is ChoInterceptableObject)
                        {
                            ChoInterceptableObject interceptableObject = ConfigObject as ChoInterceptableObject;
                            interceptableObject.SetDirty(false);
                            interceptableObject.SetSilent(false);
                            interceptableObject.SetInitialized(false);
                            interceptableObject.IsConfigObjectSilent = Silent;
                        }
                    }

                    GetConfig(ConfigObjectType, refresh);
                }
                catch (ConfigurationErrorsException configErrorsEx)
                {
                    bool isModified = false;
                    errorHandled = SetNThrowException(configErrorsEx, ref isModified);
                    _modifiedStateObject.SetModified(isModified);
                }
                catch (TypeInitializationException typeEx)
                {
                    bool isModified = false;
                    errorHandled = SetNThrowException(typeEx, ref isModified);
                    _modifiedStateObject.SetModified(isModified);
                }
                catch (ChoFatalApplicationException)
                {
                    canTraceOutput = false;
                    throw;
                }
                catch (Exception ex)
                {
                    bool isModified = false;
                    errorHandled = SetNThrowException(ex, ref isModified);
                    _modifiedStateObject.SetModified(isModified);
                }
                finally
                {
                    if (ConfigSection != null && ConfigObject is ChoConfigurableObject)
                    {
                        //Call Notify Property Changed for all default values
                        CallNotifyPropertyChangedForAllDefaultableMembers();
                        ConfigSection.Initialize();
                    }

                    if (ConfigSection != null && ConfigSection.ConfigLoadException != null)
                    {
                        bool isModified = false;
                        errorHandled = SetNThrowException(ConfigSection.ConfigLoadException, ref isModified);
                        _modifiedStateObject.SetModified(isModified);
                    }

                    ////Update configuration meta data information
                    //if (ConfigSection != null && ConfigSection.MetaDataInfo != null)
                    //    ApplyConfigMetaData(ConfigSection.MetaDataInfo);

                    //Print the output to file
                    if (canTraceOutput)
                    {
                        //Set default trace output file name
                        if (LogFileName.IsNullOrEmpty())
                        {
                            LogFileName = ChoPath.AddExtension(ConfigObject.GetType().FullName, ChoReservedFileExt.Log);
                        }

                        //if (ConfigSection is IChoCustomConfigSection)
                        //{
                        //    if (ConfigSection.ConfigData != null)
                        //        ConfigSection.ConfigObject = ConfigSection.ConfigData as ChoConfigurableObject;
                        //    ChoObjectManagementFactory.SetInstance(ConfigObject);
                        //}

                        if ((ConfigSection == null || ConfigSection.ConfigData == null) && !_defaultable /*&& !_persistable*/)
                        {
                            throw new ChoConfigurationConstructionException(String.Format("Failed to load '[{0}]' configuration section.", this._configElementPath));
                        }
                        else
                        {
                            if (prevConfigSection != null)
                            {
                                prevConfigSection.Dispose();
                            }

                            if (ConfigObject is ChoConfigurableObject)
                            {
                                ((ChoConfigurableObject)ConfigObject).SetReadOnly(true);
                            }

                            bool hasConfigSectionDefined = ConfigSection != null ? ConfigSection.HasConfigSectionDefined : false;

                            try
                            {
                                if (!errorHandled)
                                {
                                    if (!(ConfigSection is IChoCustomConfigSection))
                                    {
                                        if (hasConfigSectionDefined)
                                        {
                                            _modifiedStateObject.SetModified(ExtractNPopulateValues(ref hasErrors, ref isDirty));
                                        }
                                        else
                                        {
                                            _modifiedStateObject.SetModified(AssignToFallbackOrDefaultValues(ref isDirty));
                                        }
                                    }
                                    else
                                    {
                                        if (hasConfigSectionDefined)
                                        {
                                            //isModfied = true;
                                            if (ConfigSection == null)
                                            {
                                                _modifiedStateObject.SetModified(AssignToFallbackOrDefaultValues(ref isDirty));
                                            }
                                            else if (!ChoObject.Equals(ConfigSection, prevConfigSection))
                                            {
                                                _modifiedStateObject.SetModified(true);
                                            }
                                            else
                                            {
                                                _modifiedStateObject.SetModified(AssignToFallbackOrDefaultValues(ref isDirty));
                                            }
                                        }
                                        else
                                        {
                                            _modifiedStateObject.SetModified(AssignToFallbackOrDefaultValues(ref isDirty));
                                        }
                                    }
                                }

                                if (!hasErrors)
                                {
                                    hasErrors = ChoConfigurationObjectErrorManagerService.ContainsObjectError(ConfigObject);
                                }

                                ChoObjectInitializer.Initialize(ConfigObject, false, ConfigSection != null ? ConfigObject : null);

                                SetWatcher(false);

                                if (ConfigObject is ChoInterceptableObject)
                                {
                                    ChoInterceptableObject interceptableObject = ConfigObject as ChoInterceptableObject;
                                    if (interceptableObject.Dirty)
                                    {
                                        isDirty = interceptableObject.Dirty;
                                    }
                                    //if (interceptableObject.IsModified)
                                    //{
                                    //    isModfied = interceptableObject.IsModified;
                                    //}
                                    interceptableObject.SetDirty(false);
                                    //interceptableObject.SetSilent(false);
                                    interceptableObject.SetInitialized(true);
                                }

                                if (ConfigObject is ChoConfigurableObject)
                                {
                                    bool invokeObjectLoaded = /* isDirty || */ _firstTime || _modifiedStateObject.IsModified;
                                    if (invokeObjectLoaded || hasErrors)
                                    {
                                        TraceOutput(false);
                                        if (invokeObjectLoaded)
                                        {
                                            ((ChoConfigurableObject)ConfigObject).RaiseAfterConfigurationObjectLoaded();
                                        }
                                    }

                                    //if (isDirty)
                                    //{
                                    //    TraceOutput(false);
                                    //    ((ChoConfigurableObject)ConfigObject).OnAfterConfigurationObjectLoaded();
                                    //}
                                    //else if (hasErrors)
                                    //    TraceOutput(false);
                                }
                                else
                                {
                                    TraceOutput(false);
                                }

                                if (!isDirty && ConfigSection != null)
                                {
                                    isDirty = ConfigSection.IsMetaDataDefinitionChanged;
                                }
                            }
                            finally
                            {
                                if (prevConfigSection != null)
                                {
                                    prevConfigSection.Dispose();
                                    prevConfigSection = null;
                                }

                                if (ConfigObject is ChoConfigurableObject)
                                {
                                    ((ChoConfigurableObject)ConfigObject).SetReadOnly(false);
                                }

                                if (!hasConfigSectionDefined || isDirty || hasErrors)
                                {
                                    if (_defaultable)
                                    {
                                        Persist(false, null);
                                    }
                                }
                                else if ((bool)this[ChoConfigurationConstants.FORCE_PERSIST])
                                {
                                    Persist(false, null);
                                }

                                _inLoadingProcess = false;
                                if (_watchChange && ConfigSection != null)
                                {
                                    ConfigSection.StartWatching();
                                }

                                ChoConfigurationObjectErrorManagerService.ResetObjectErrors(ConfigObject);
                                _firstTime = false;
                            }
                        }
                    }
                }
            }
        }