DocumentDB .Net client using connection string

后端 未结 4 1639
野性不改
野性不改 2021-01-17 14:37

I checked the MSDN on DocumentDB for .Net (here) and found 3 valid constructors. However none of them makes use of connection strings, which sounds strange for me.

I

4条回答
  •  渐次进展
    2021-01-17 15:34

    I created a class for parsing connection string similar to how the CloudStorageAccount.Parse works. I tried to follow their pattern as closely as possible in case they ever decide to open source it, this could hopefully be contribed without much change.

    public static class DocumentDbAccount
    {
        public static DocumentClient Parse(string connectionString)
        {
            DocumentClient ret;
    
            if (String.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentException("Connection string cannot be empty.");
            }
    
            if(ParseImpl(connectionString, out ret, err => { throw new FormatException(err); }))
            {
                return ret;
            }
    
            throw new ArgumentException($"Connection string was not able to be parsed into a document client.");
        }
    
        public static bool TryParse(string connectionString, out DocumentClient documentClient)
        {
            if (String.IsNullOrWhiteSpace(connectionString))
            {
                documentClient = null;
                return false;
            }
    
            try
            {
                return ParseImpl(connectionString, out documentClient, err => { });
            }
            catch (Exception)
            {
                documentClient = null;
                return false;
            }
        }
    
        private const string AccountEndpointKey = "AccountEndpoint";
        private const string AccountKeyKey = "AccountKey";
        private static readonly HashSet RequireSettings = new HashSet(new [] { AccountEndpointKey, AccountKeyKey }, StringComparer.OrdinalIgnoreCase);
    
        internal static bool ParseImpl(string connectionString, out DocumentClient documentClient, Action error)
        {
            IDictionary settings = ParseStringIntoSettings(connectionString, error);
    
            if (settings == null)
            {
                documentClient = null;
                return false;
            }
    
            if (!RequireSettings.IsSubsetOf(settings.Keys))
            {
                documentClient = null;
                return false;
            }
    
            documentClient = new DocumentClient(new Uri(settings[AccountEndpointKey]), settings[AccountKeyKey]);
            return true;
        }
    
        /// 
        /// Tokenizes input and stores name value pairs.
        /// 
        /// The string to parse.
        /// Error reporting delegate.
        /// Tokenized collection.
        private static IDictionary ParseStringIntoSettings(string connectionString, Action error)
        {
            IDictionary settings = new Dictionary();
            string[] splitted = connectionString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    
            foreach (string nameValue in splitted)
            {
                string[] splittedNameValue = nameValue.Split(new char[] { '=' }, 2);
    
                if (splittedNameValue.Length != 2)
                {
                    error("Settings must be of the form \"name=value\".");
                    return null;
                }
    
                if (settings.ContainsKey(splittedNameValue[0]))
                {
                    error(string.Format(CultureInfo.InvariantCulture, "Duplicate setting '{0}' found.", splittedNameValue[0]));
                    return null;
                }
    
                settings.Add(splittedNameValue[0], splittedNameValue[1]);
            }
    
            return settings;
        }
    }
    

提交回复
热议问题