/// <summary>
 /// Initializes a new instance of the <see cref="BeyovaComponentAttribute" /> class.
 /// </summary>
 /// <param name="id">The identifier.</param>
 /// <param name="version">The version.</param>
 /// <param name="apiTrackingType">Type of the API tracking.</param>
 public BeyovaComponentAttribute(string id, string version, Type apiTrackingType)
 {
     UnderlyingObject = new BeyovaComponentInfo(id, version, apiTrackingType);
 }
예제 #2
0
        /// <summary>
        /// Initializes static members of the <see cref="EnvironmentCore"/> class.
        /// </summary>
        static EnvironmentCore()
        {
            var baseDirectoryByAppDomain        = AppDomain.CurrentDomain.BaseDirectory;
            var baseDirectoryByAssemblyLocation = Path.GetDirectoryName(typeof(EnvironmentCore).Assembly.Location);

            // NOTE:
            // In IIS Express cases, baseDirectoryByAssemblyLocation would be allocated into asp.net tmp folders, by each library.
            // In other cases, IIS or Console or Windows environments, baseDirectoryByAssemblyLocation should be correct.
            DirectoryInfo baseDirectory = new DirectoryInfo((baseDirectoryByAppDomain.StartsWith(baseDirectoryByAssemblyLocation, StringComparison.OrdinalIgnoreCase)) ?
                                                            baseDirectoryByAssemblyLocation
                 : Path.Combine(baseDirectoryByAppDomain, "bin"));

            if (baseDirectory?.Exists ?? false)
            {
                ApplicationBaseDirectory = baseDirectory.ToString();
            }
            else
            {
                throw ExceptionFactory.CreateInvalidObjectException(nameof(baseDirectory), new
                {
                    baseDirectory = baseDirectory?.ToString(),
                    baseDirectoryByAppDomain,
                    baseDirectoryByAssemblyLocation
                });
            }

            LogDirectory  = Path.Combine(ApplicationBaseDirectory, "logs");
            ApplicationId = System.AppDomain.CurrentDomain.Id;

            var dependencyChain = ReflectionExtension.GetAppDomainAssemblies().GetAssemblyDependencyChain(true);

            AscendingAssemblyDependencyChain = new List <Assembly>(dependencyChain).AsReadOnly();
            dependencyChain.Reverse();

            DescendingAssemblyDependencyChain = dependencyChain.AsReadOnly();

            CommonComponentInfo = typeof(EnvironmentCore).Assembly.GetCustomAttribute <BeyovaComponentAttribute>()?.UnderlyingObject;

            try
            {
                MachineName = Environment.MachineName;
            }
            catch { MachineName = string.Empty; }

            try
            {
                LocalMachineHostName = Dns.GetHostName();

                var host = Dns.GetHostEntry(LocalMachineHostName);
                foreach (var ip in host.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        LocalMachineIpAddress = ip.ToString();
                        break;
                    }
                }
            }
            catch { }

            try
            {
                ProductName = FindProductName();

                if (string.IsNullOrWhiteSpace(ProductName))
                {
                    ProductName = Assembly.GetEntryAssembly()?.FullName;
                }

                if (string.IsNullOrWhiteSpace(ProductName) && AppDomain.CurrentDomain != null)
                {
                    ProductName = AppDomain.CurrentDomain.FriendlyName;
                }
            }
            catch { ProductName = string.Empty; }
        }
예제 #3
0
        /// <summary>
        /// Initializes the by custom assembly attributes.
        /// </summary>
        private static void InitializeByCustomAssemblyAttributes()
        {
            string currentAssemblyName = null;

            //Api track type need to stay on top and out of this. It is set in loop and be used after loop, to ensure required parameter from configuration is initialized.
            List <Type> apiTrackTypes = new List <Type>();

            try
            {
                foreach (var assembly in EnvironmentCore.DescendingAssemblyDependencyChain)
                {
                    BeyovaComponentInfo componentInfo = null;
                    var assemblyName = assembly.GetName();
                    currentAssemblyName = assemblyName.Name;

                    if (!assemblyName.IsSystemAssembly())
                    {
                        #region BeyovaComponentAttribute

                        var componentAttribute = assembly.GetCustomAttribute <BeyovaComponentAttribute>();
                        if (componentAttribute != null)
                        {
                            componentInfo = componentAttribute.UnderlyingObject;

                            // APITRACKING
                            if (ApiTracking == null)
                            {
                                apiTrackTypes.Add(componentInfo?.ApiTrackingType);
                            }
                        }

                        #endregion BeyovaComponentAttribute

                        #region DataSecurityAttribute

                        var dataSecurty = assembly.GetCustomAttribute <DataSecurityAttribute>();
                        if (dataSecurty != null)
                        {
                            if (DataSecurityProvider == null)
                            {
                                DataSecurityProvider = dataSecurty.DataSecurityProvider;
                            }
                        }

                        #endregion DataSecurityAttribute

                        #region BeyovaConfigurationLoaderAttribute

                        var configurationLoaders = assembly.GetCustomAttributes <BeyovaConfigurationLoaderAttribute>();
                        if (configurationLoaders.HasItem())
                        {
                            foreach (var one in configurationLoaders)
                            {
                                ConfigurationHub.RegisterConfigurationReader(one?.Loader?.GetReader(currentAssemblyName, componentInfo?.Version));
                            }
                        }
                        else
                        {
                            // To be obsoleted
                            //var configurationAttribute = assembly.GetCustomAttribute<BeyovaConfigurationAttribute>();
                            //if (configurationAttribute != null)
                            //{
                            //    ConfigurationHub.RegisterConfigurationReader(new JsonConfigurationReader(currentAssemblyName, componentInfo?.Version, nameof(JsonConfigurationReader), configurationAttribute.Options));
                            //}
                        }

                        #endregion BeyovaConfigurationLoaderAttribute

                        #region BeyovaCultureResourceAttribute

                        var cultureResourceAttribute = assembly.GetCustomAttribute <BeyovaCultureResourceAttribute>();
                        if (cultureResourceAttribute != null)
                        {
                            if (!string.IsNullOrWhiteSpace(cultureResourceAttribute.UnderlyingObject.DefaultCultureCode))
                            {
                                _resourceHub.DefaultCultureInfo = cultureResourceAttribute.UnderlyingObject.DefaultCultureCode.AsCultureInfo();
                            }

                            cultureResourceAttribute?.UnderlyingObject.FillResources(_resourceHub._cultureBasedResources);
                        }

                        #endregion BeyovaCultureResourceAttribute
                    }
                }

                if (ApiTracking == null && apiTrackTypes.HasItem())
                {
                    foreach (var one in apiTrackTypes)
                    {
                        var instance = one.CreateInstance() as IApiTracking;
                        if (instance != null)
                        {
                            ApiTracking = instance;
                            break;
                        }
                    }
                }

                // To check and ensure
                if (DataSecurityProvider == null)
                {
                    DataSecurityProvider = DefaultDataSecurityProvider.Instance;
                }
            }
            catch (Exception ex)
            {
                throw ex.Handle(new
                {
                    currentAssemblyName
                });
            }
        }