Esempio n. 1
0
        /// <summary>
        /// Caricamento dati da file xml
        /// </summary>
        /// <returns></returns>
        private static ServiceLoadInfo[] LoadData()
        {
            List <ServiceLoadInfo> data = new List <ServiceLoadInfo>();

            if (!string.IsNullOrEmpty(Configurations.XmlFilePath) &&
                System.IO.File.Exists(Configurations.XmlFilePath))
            {
                using (FileStream stream = new FileStream(Configurations.XmlFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    XmlReader reader = new XmlTextReader(stream);

                    ServiceLoadInfo info = null;

                    while (reader.Read())
                    {
                        if (reader.IsStartElement("services"))
                        {
                            // Verifica se i servizi di controllo agli accessi a tutti i servizi sono abilitati o meno
                            bool isEnabled;
                            bool.TryParse(reader.GetAttribute("enabled"), out isEnabled);
                            if (!isEnabled)
                            {
                                break;
                            }
                        }
                        else if (reader.IsStartElement("serviceLoad"))
                        {
                            info = new ServiceLoadInfo
                            {
                                ServiceName         = reader.GetAttribute("serviceName"),
                                ServiceCallInterval = Int32.Parse(reader.GetAttribute("serviceInterval"))
                            };

                            data.Add(info);
                        }
                        else if (reader.IsStartElement("methodLoad"))
                        {
                            MethodLoadInfo mInfo = new MethodLoadInfo
                            {
                                MethodName         = reader.GetAttribute("methodName"),
                                MethodCallInterval = Int32.Parse(reader.GetAttribute("methodInterval"))
                            };

                            List <MethodLoadInfo> methods = new List <MethodLoadInfo>(info.Methods);
                            methods.Add(mInfo);
                            info.Methods = methods.ToArray();
                        }
                    }
                }
            }

            return(data.ToArray());
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="methodName"></param>
        public static void CheckInterval(string serviceName, string methodName)
        {
            ServiceLoadInfo serviceInfo = GetService(serviceName);

            if (serviceInfo != null)
            {
                MethodLoadInfo methodInfo = GetMethod(serviceInfo, methodName);

                if (methodInfo != null)
                {
                    // Reperimento ts memorizzato
                    if (methodInfo.LastMethodExecution > 0)
                    {
                        DateTime now = DateTime.Now;

                        // Differenza tra ultima esecuzione e istante attuale
                        TimeSpan ts = TimeSpan.FromTicks(now.Ticks - methodInfo.LastMethodExecution);

                        if (ts.TotalMilliseconds > ((double)methodInfo.MethodCallInterval))
                        {
                            // salva il TS attuale bloccando la risorsa condivisa per il tempo necessario al salvataggio
                            lock (methodInfo)
                            {
                                methodInfo.LastMethodExecution = now.Ticks;
                            }
                        }
                        else
                        {
                            throw SoapExceptionFactory.Create(new ServiceBusyException(serviceName, methodName));
                        }
                    }
                    else
                    {
                        // Prima esecuzione del check, autorizzazione consentita

                        // salva il TS attuale bloccando la risorsa condivisa per il tempo necessario al salvataggio
                        lock (methodInfo)
                        {
                            methodInfo.LastMethodExecution = DateTime.Now.Ticks;
                        }
                    }
                }
                else
                {
                    // Metodo non definito
                    CheckInterval(serviceName);
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="service"></param>
 /// <param name="methodName"></param>
 /// <returns></returns>
 private static MethodLoadInfo GetMethod(ServiceLoadInfo service, string methodName)
 {
     return(service.Methods.Where(e => e.MethodName == methodName).FirstOrDefault());
 }