예제 #1
0
        internal static void CreateLocalDBInstance(string instance)
        {
            DemandLocalDBPermissions();
            if (s_configurableInstances == null)
            {
                // load list of instances from configuration, mark them as not created
                bool lockTaken = false;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    Monitor.Enter(s_configLock, ref lockTaken);
                    if (s_configurableInstances == null)
                    {
                        Dictionary <string, InstanceInfo> tempConfigurableInstances = new Dictionary <string, InstanceInfo>(StringComparer.OrdinalIgnoreCase);
                        object section = PrivilegedConfigurationManager.GetSection("system.data.localdb");
                        if (section != null) // if no section just skip creation
                        {
                            // validate section type
                            LocalDBConfigurationSection configSection = section as LocalDBConfigurationSection;
                            if (configSection == null)
                            {
                                throw CreateLocalDBException(errorMessage: Res.GetString("LocalDB_BadConfigSectionType"));
                            }
                            foreach (LocalDBInstanceElement confElement in configSection.LocalDbInstances)
                            {
                                Debug.Assert(confElement.Name != null && confElement.Version != null, "Both name and version should not be null");
                                tempConfigurableInstances.Add(confElement.Name.Trim(), new InstanceInfo(confElement.Version.Trim()));
                            }
                        }
                        else
                        {
                            Bid.Trace("<sc.LocalDBAPI.CreateLocalDBInstance> No system.data.localdb section found in configuration");
                        }
                        s_configurableInstances = tempConfigurableInstances;
                    }
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(s_configLock);
                    }
                }
            }

            InstanceInfo instanceInfo = null;

            if (!s_configurableInstances.TryGetValue(instance, out instanceInfo))
            {
                return; // instance name was not in the config
            }
            if (instanceInfo.created)
            {
                return; // instance has already been created
            }
            Debug.Assert(!instance.Contains("\0"), "Instance name should contain embedded nulls");

            if (instanceInfo.version.Contains("\0"))
            {
                throw CreateLocalDBException(errorMessage: Res.GetString("LocalDB_InvalidVersion"), instance: instance);
            }

            // LocalDBCreateInstance is thread- and cross-process safe method, it is OK to call from two threads simultaneously
            int hr = LocalDBCreateInstance(instanceInfo.version, instance, flags: 0);

            Bid.Trace("<sc.LocalDBAPI.CreateLocalDBInstance> Starting creation of instance %ls version %ls", instance, instanceInfo.version);
            if (hr < 0)
            {
                throw CreateLocalDBException(errorMessage: Res.GetString("LocalDB_CreateFailed"), instance: instance, localDbError: hr);
            }
            Bid.Trace("<sc.LocalDBAPI.CreateLocalDBInstance> Finished creation of instance %ls", instance);
            instanceInfo.created = true; // mark instance as created
        } // CreateLocalDbInstance
예제 #2
0
        internal static void CreateLocalDBInstance(string instance)
        {
            if (s_configurableInstances == null)
            {
                bool lockTaken = false;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    Monitor.Enter(s_configLock, ref lockTaken);
                    if (s_configurableInstances == null)
                    {
                        Dictionary <string, InstanceInfo> dictionary = new Dictionary <string, InstanceInfo>(StringComparer.OrdinalIgnoreCase);
                        object obj2 = ConfigurationManager.GetSection("system.data.localdb");
                        if (obj2 != null)
                        {
                            LocalDBConfigurationSection section = obj2 as LocalDBConfigurationSection;
                            if (section == null)
                            {
                                throw CreateLocalDBException(Res.GetString("LocalDB_BadConfigSectionType"), null, 0, 0);
                            }
                            foreach (LocalDBInstanceElement element in section.LocalDbInstances)
                            {
                                dictionary.Add(element.Name.Trim(), new InstanceInfo(element.Version.Trim()));
                            }
                        }
                        else
                        {
                            Bid.Trace("<sc.LocalDBAPI.CreateLocalDBInstance> No system.data.localdb section found in configuration");
                        }
                        s_configurableInstances = dictionary;
                    }
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(s_configLock);
                    }
                }
            }
            InstanceInfo info = null;

            if (s_configurableInstances.TryGetValue(instance, out info) && !info.created)
            {
                if (info.version.Contains("\0"))
                {
                    string errorMessage = Res.GetString("LocalDB_InvalidVersion");
                    string str3         = instance;
                    throw CreateLocalDBException(errorMessage, str3, 0, 0);
                }
                uint flags = 0;
                int  num   = LocalDBCreateInstance(info.version, instance, flags);
                Bid.Trace("<sc.LocalDBAPI.CreateLocalDBInstance> Starting creation of instance %ls version %ls", instance, info.version);
                if (num < 0)
                {
                    string str2         = Res.GetString("LocalDB_CreateFailed");
                    string str          = instance;
                    int    localDbError = num;
                    throw CreateLocalDBException(str2, str, localDbError, 0);
                }
                Bid.Trace("<sc.LocalDBAPI.CreateLocalDBInstance> Finished creation of instance %ls", instance);
                info.created = true;
            }
        }