/// <summary>
        /// Adds a settings object from a section in configuration
        /// </summary>
        /// <typeparam name="T">The type of settings object to create</typeparam>
        /// <param name="encryptionKey">The encryption key used for encrypting and serializing the settings object</param>
        /// <param name="sectionName">The name of the section in config</param>
        public void AddSettingsFromConfig <TSettings, TSection>(string encryptionKey, string sectionName)
            where TSettings : IConfigurableSettings <TSection>, new()
            where TSection : ConfigurationSection
        {
            // get the section from config
            TSection section = (TSection)ConfigurationManager.GetSection(sectionName);

            // if the section exists...
            if (section != null)
            {
                // create a new settings object
                TSettings settings = new TSettings();

                // populate the object from the section
                settings.PopulateFromConfigurationSection(section);

                // create new settings data object, with the new settings object serialized
                SectionSettings.Add(new ConfigurableSettingsData()
                {
                    Name = sectionName,
                    TypeAssemblyQualifiedName = typeof(TSettings).AssemblyQualifiedName,
                    TypeFullName   = typeof(TSettings).FullName,
                    SerializedData = DataContractUtility.Serialize(typeof(TSettings), settings, encryptionKey)
                });
            }
        }
Example #2
0
        /// <summary>
        /// Writes a section object to xml and stores it with its type in a ConfigurationSectionData object
        /// </summary>
        /// <typeparam name="T">The type of the configuration section</typeparam>
        /// <param name="encryptionKey">The key to use to encrypt when serializing</param>
        /// <param name="obj">The object to serialize and store</param>
        public void WriteSection(Type sectionType, string encryptionKey, string sectionName, object obj)
        {
            // instantiate section list, if necessary
            if (SectionSettings == null)
            {
                SectionSettings = new List <ConfigurableSettingsData>();
            }

            // add the serialized section with its type
            SectionSettings.Add(new ConfigurableSettingsData()
            {
                Name           = sectionName,
                TypeFullName   = sectionType.AssemblyQualifiedName,
                SerializedData = DataContractUtility.Serialize(sectionType, obj, encryptionKey)
            });
        }
Example #3
0
        /// <summary>
        /// Creates a list of serialized parameters for invocation of a service operation
        /// </summary>
        /// <param name="serializer">The utility used to serialize the parameter objects</param>
        /// <param name="parameters">The list of parameter types and objects</param>
        /// <returns>A list of invoke parameters</returns>
        public static List <InvokeParameter> CreateInvokeParameters(List <Tuple <Type, object> > parameters, string encryptionKey)
        {
            if (parameters != null && parameters.Count > 0)
            {
                // create list of parameters
                List <InvokeParameter> serializedParameters = new List <InvokeParameter>();
                foreach (Tuple <Type, object> parameter in parameters)
                {
                    // get the object type
                    Type objectType = parameter.Item1;

                    // check if the parameter is an enumerable
                    bool isEnumerable = false;
                    if (typeof(IEnumerable).IsAssignableFrom(objectType) && objectType.IsGenericType)
                    {
                        // get the underlying object type
                        objectType   = objectType.GetGenericArguments().First();
                        isEnumerable = true;
                    }

                    // create the parameter with the type information and the serialize object
                    serializedParameters.Add(
                        new InvokeParameter()
                    {
                        TypeName = objectType.FullName,
                        AssemblyQualifiedTypeName = objectType.AssemblyQualifiedName,
                        IsEnumerable        = isEnumerable,
                        SerializedParameter = DataContractUtility.Serialize(parameter.Item1, parameter.Item2, encryptionKey)
                    });
                }

                return(serializedParameters);
            }

            return(null);
        }
Example #4
0
        public InvokeResponse Invoke(InvokeRequest request)
        {
            InvokeResponse response = new InvokeResponse();

            if (!string.IsNullOrEmpty(request.ComponentType))
            {
                IServiceComponentManager componentManager = Container.Resolve <IServiceComponentManager>();
                IServiceComponent        component        = componentManager.ServiceComponents.FirstOrDefault(c => c != null && c.GetType().GetInterface(request.ComponentType) != null);

                if (component != null)
                {
                    MethodInfo operation =
                        component.GetType().GetMethod(request.OperationName,
                                                      request.OperationParameters.Select(op => DataContractUtility.GetType(op.AssemblyQualifiedTypeName, op.TypeName, op.IsEnumerable)).ToArray());
                    if (operation != null)
                    {
                        try
                        {
                            List <object> parameters = new List <object>();
                            foreach (InvokeParameter parameter in request.OperationParameters)
                            {
                                parameters.Add(DataContractUtility.Deserialize(DataContractUtility.GetType(parameter.AssemblyQualifiedTypeName, parameter.TypeName, parameter.IsEnumerable),
                                                                               parameter.SerializedParameter));
                            }

                            object result;

                            try
                            {
                                result = operation.Invoke(component, parameters.ToArray());
                                if (result != null)
                                {
                                    response.OperationResult = DataContractUtility.Serialize(result.GetType(), result);
                                }
                            }
                            catch (Exception ex)
                            {
                                response.Error = new Exception("An exception occurred trying to invoke operation.", ex);
                            }
                        }
                        catch (Exception ex)
                        {
                            response.Error = new InvalidRequestException("An exception occurred trying to deserialize operation parameters.", ex);
                        }
                    }
                    else
                    {
                        response.Error = new InvalidRequestException("Operation not found.");
                    }
                }
                else
                {
                    response.Error = new InvalidRequestException("Component not found.");
                }
            }
            else
            {
                response.Error = new InvalidRequestException("Component type not provided.");
            }

            return(response);
        }