private static string LookupString(string connectionStringName)
        {
            if (ConfigurationManager.ConnectionStrings[connectionStringName] == null)
            {
                Nlog.Error("No connection string found matching given connection string name, returning null...");
                return(null);
            }

            VcapServices vcap = VcapServices.Instance();

            if (!vcap.IsCF)
            {
                Nlog.Info("VCAP_SERVICES not set - returning connection string from app config...");
                return(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString);
            }

            Nlog.Trace(String.Format("Checking for {0} in VCAP_SERVICES", connectionStringName));
            JToken service = vcap.GetService(connectionStringName);

            if (service != null)
            {
                Nlog.Trace(String.Format("Found service named {0} in VCAP_SERVICES.  Reconfiguring for bound services.", connectionStringName));
                JToken creds     = service["credentials"];
                string conString = "";
                if (creds["connectionString"] != null)
                {
                    conString = creds["connectionString"].ToString();
                }
                else
                {
                    conString = String.Format("server={0};port={1};database={2};uid={3};password={4};",
                                              creds["hostname"],
                                              creds["port"],
                                              creds["name"],
                                              creds["username"],
                                              creds["password"]);
                }
                Nlog.Debug(conString);
                return(conString);
            }
            else
            {
                Nlog.Info("Couldn't find bound service - returning connection string from app config...");
                return(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString);
            }
        }
Example #2
0
        public static void Configure()
        {
            VcapServices vcapServices = VcapServices.Instance();

            if (vcapServices.IsCF)
            {
                JToken service = vcapServices.GetService("BookServiceContext");

                IEnumerator <JToken> tags = service["tags"].Values().GetEnumerator();
                while (tags.MoveNext())
                {
                    if ("mysql".Equals(tags.Current.ToString()))
                    {
                        DbConfiguration.SetConfiguration(new MySqlEFConfiguration());
                    }
                }
            }
        }
        public static VcapServices Instance()
        {
            if (instance == null)
            {
                instance = new VcapServices();
                Nlog.Trace("Checking for VCAP_SERVICES");
                instance.rawEnv = Environment.GetEnvironmentVariable("VCAP_SERVICES");
                if (instance.rawEnv != null)
                {
                    Nlog.Trace("Parsing VCAP_SERVICES");
                    instance.parsedEnv = JObject.Parse(instance.rawEnv);

                    Nlog.Trace("Getting services from parsed VCAP_SERVICES");
                    instance.serviceDictionary =
                        instance.parsedEnv.Children().Children().Children().ToDictionary(val => val["name"].ToString(), val => val);
                }
            }
            return(instance);
        }
        public static VcapServices Instance()
        {
            if (instance == null)
            {
                instance = new VcapServices();
                Nlog.Trace("Checking for VCAP_SERVICES");
                instance.rawEnv = Environment.GetEnvironmentVariable("VCAP_SERVICES");
                if (instance.rawEnv != null)
                {
                    Nlog.Trace("Parsing VCAP_SERVICES");
                    instance.parsedEnv = JObject.Parse(instance.rawEnv);

                    Nlog.Trace("Getting services from parsed VCAP_SERVICES");
                    instance.serviceDictionary =
                        instance.parsedEnv.Children().Children().Children().ToDictionary(val => val["name"].ToString(), val => val);
                }
            }
            return instance;
        }