コード例 #1
0
        public async Task <ConfigBackupRecord> GetConfigs()
        {
            if (!createConnection())
            {
                // if connection fails
                return(null);
            }

            String serviceAddr = NetworkInterface.
                                 GetAllNetworkInterfaces()
                                 .Where(nic => nic.OperationalStatus == OperationalStatus.Up &&
                                        nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                                 .Select(nic => nic.GetPhysicalAddress()
                                         .ToString())
                                 .FirstOrDefault();

            var collection = database
                             .GetCollection <ConfigBackupRecord>(config.configBackupCollection);

            IAsyncCursor <ConfigBackupRecord> dbCursor = await collection
                                                         .FindAsync(r => r.serviceId == serviceAddr);

            // await dbCursor.AnyAsync<ConfigBackupRecord>()
            // cursor.FirstAsync() called after prev. line will throw
            // invalid operation exception: Cannot access a disposed object

            if (dbCursor != null)
            {
                try
                {
                    var record = await dbCursor.FirstAsync <ConfigBackupRecord>();

                    return(record);
                }
                catch (InvalidOperationException)
                {
                    // most likely sequence contains no elements

                    return(new ConfigBackupRecord(serviceAddr,
                                                  new List <DatedConfigRecord>()));
                }
                catch (TimeoutException)
                {
                    Console.WriteLine($"Failed to connect with the database on: "
                                      + $"{config.dbAddress} ... ");

                    return(null);
                }
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Looks up the <see cref="CommandDocument"/> for the given command.
        /// </summary>
        /// <param name="channelId">The channelId the command is registered to.</param>
        /// <param name="command">The command to lookup.</param>
        /// <returns>The <see cref="CommandDocument"/> for the command, if it exists; <c>null</c> otherwise.</returns>
        public static async Task <CommandDocument> GetCustomCommandAsync(string channelId, string command)
        {
            IMongoCollection <CommandDocument> collection = DatabaseClient.Instance.MongoDatabase.GetCollection <CommandDocument>(CommandDocument.CollectionName);

            FilterDefinition <CommandDocument> filter = Builders <CommandDocument> .Filter.Where(c => c.ChannelId == channelId && !c.IsWhisperCommand && c.Command == command.ToLowerInvariant());

            if (await collection.CountDocumentsAsync(filter).ConfigureAwait(false) == 0)
            {
                return(null);
            }

            using IAsyncCursor <CommandDocument> cursor = await collection.FindAsync(filter).ConfigureAwait(false);

            return(await cursor.FirstAsync().ConfigureAwait(false));
        }