本文整理汇总了C#中MongoConnection类的典型用法代码示例。如果您正苦于以下问题:C# MongoConnection类的具体用法?C# MongoConnection怎么用?C# MongoConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MongoConnection类属于命名空间,在下文中一共展示了MongoConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Initialize
/// <summary>
/// Initializes migration with the connection object
/// </summary>
/// <param name="connection">MongoConnection object</param>
public void Initialize(MongoConnection connection)
{
if (connection == null)
throw new ArgumentNullException("connection");
Connection = connection;
}
示例2: MongoInsertMessage
internal MongoInsertMessage(
MongoConnection connection,
string collectionFullName
)
: base(connection, MessageOpcode.Insert) {
this.collectionFullName = collectionFullName;
}
示例3: MongoDbDataTest
public MongoDbDataTest()
{
var connection = new MongoConnection();
DataContext = new DataContext(typeof(MongoConnection).Assembly);
DataContext.SetRepositoryCreator(type => Activator.CreateInstance(type, connection));
}
示例4: MongoMessage
protected MongoMessage(
MongoConnection connection,
MessageOpcode opcode
) {
this.connection = connection;
this.opcode = opcode;
}
示例5: Authenticate
// public methods
/// <summary>
/// Authenticates the connection against the given database.
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="credential">The credential.</param>
public void Authenticate(MongoConnection connection, MongoCredential credential)
{
string nonce;
try
{
var nonceCommand = new CommandDocument("getnonce", 1);
var nonceResult = RunCommand(connection, credential.Source, nonceCommand);
nonce = nonceResult.Response["nonce"].AsString;
}
catch (MongoCommandException ex)
{
throw new MongoAuthenticationException("Error getting nonce for authentication.", ex);
}
try
{
var passwordDigest = ((PasswordEvidence)credential.Evidence).ComputeMongoCRPasswordDigest(credential.Username);
var digest = MongoUtils.Hash(nonce + credential.Username + passwordDigest);
var authenticateCommand = new CommandDocument
{
{ "authenticate", 1 },
{ "user", credential.Username },
{ "nonce", nonce },
{ "key", digest }
};
RunCommand(connection, credential.Source, authenticateCommand);
}
catch (MongoCommandException ex)
{
var message = string.Format("Invalid credential for database '{0}'.", credential.Source);
throw new MongoAuthenticationException(message, ex);
}
}
示例6: Execute
// public methods
public WriteConcernResult Execute(MongoConnection connection)
{
var serverInstance = connection.ServerInstance;
if (serverInstance.Supports(FeatureId.WriteCommands) && _args.WriteConcern.Enabled)
{
var emulator = new UpdateOpcodeOperationEmulator(_args);
return emulator.Execute(connection);
}
SendMessageWithWriteConcernResult sendMessageResult;
using (var buffer = new BsonBuffer(new MultiChunkBuffer(BsonChunkPool.Default), true))
{
var requests = _args.Requests.ToList();
if (requests.Count != 1)
{
throw new NotSupportedException("Update opcode only supports a single update request.");
}
var updateRequest = (UpdateRequest)requests[0];
var flags = UpdateFlags.None;
if (updateRequest.IsMultiUpdate ?? false) { flags |= UpdateFlags.Multi; }
if (updateRequest.IsUpsert ?? false) { flags |= UpdateFlags.Upsert; }
var maxDocumentSize = connection.ServerInstance.MaxDocumentSize;
var query = updateRequest.Query ?? new QueryDocument();
var message = new MongoUpdateMessage(WriterSettings, CollectionFullName, _args.CheckElementNames, flags, maxDocumentSize, query, updateRequest.Update);
message.WriteTo(buffer);
sendMessageResult = SendMessageWithWriteConcern(connection, buffer, message.RequestId, ReaderSettings, WriterSettings, WriteConcern);
}
return WriteConcern.Enabled ? ReadWriteConcernResult(connection, sendMessageResult) : null;
}
示例7: Execute
// public methods
public BulkWriteResult Execute(MongoConnection connection)
{
var serverInstance = connection.ServerInstance;
if (!serverInstance.Supports(FeatureId.WriteOpcodes))
{
throw new NotSupportedException("Write opcodes are not supported.");
}
var batchResults = new List<BulkWriteBatchResult>();
var remainingRequests = new List<WriteRequest>();
var hasWriteErrors = false;
var originalIndex = 0;
foreach (WriteRequest request in _args.Requests)
{
if (hasWriteErrors && _args.IsOrdered)
{
remainingRequests.Add(request);
continue;
}
var batchResult = EmulateSingleRequest(connection, request, originalIndex);
batchResults.Add(batchResult);
hasWriteErrors |= batchResult.HasWriteErrors;
originalIndex++;
}
var combiner = new BulkWriteBatchResultCombiner(batchResults, _args.WriteConcern.Enabled);
return combiner.CreateResultOrThrowIfHasErrors(remainingRequests);
}
示例8: Execute
// public methods
public virtual BulkWriteResult Execute(MongoConnection connection)
{
var batchResults = new List<BulkWriteBatchResult>();
var remainingRequests = Enumerable.Empty<WriteRequest>();
var hasWriteErrors = false;
var decoratedRequests = DecorateRequests(_args.Requests);
using (var enumerator = decoratedRequests.GetEnumerator())
{
var originalIndex = 0;
Batch<WriteRequest> batch = new FirstBatch<WriteRequest>(enumerator);
while (batch != null)
{
if (hasWriteErrors && _args.IsOrdered)
{
remainingRequests = remainingRequests.Concat(batch.RemainingItems);
break;
}
var batchResult = ExecuteBatch(connection, batch, originalIndex);
batchResults.Add(batchResult);
hasWriteErrors |= batchResult.HasWriteErrors;
originalIndex += batchResult.BatchCount;
batch = batchResult.NextBatch;
}
}
var combiner = new BulkWriteBatchResultCombiner(batchResults, _args.WriteConcern.Enabled);
return combiner.CreateResultOrThrowIfHasErrors(remainingRequests);
}
示例9: Execute
// public methods
public BulkWriteResult Execute(MongoConnection connection)
{
var batchResults = new List<BulkWriteBatchResult>();
var remainingRequests = Enumerable.Empty<WriteRequest>();
var hasWriteErrors = false;
var runCount = 0;
foreach (var run in FindRuns())
{
runCount++;
if (hasWriteErrors && _isOrdered)
{
remainingRequests = remainingRequests.Concat(run.Requests);
continue;
}
var batchResult = ExecuteBatch(connection, run);
batchResults.Add(batchResult);
hasWriteErrors |= batchResult.HasWriteErrors;
}
if (runCount == 0)
{
throw new InvalidOperationException("Bulk write operation is empty.");
}
var combiner = new BulkWriteBatchResultCombiner(batchResults, _writeConcern.Enabled);
return combiner.CreateResultOrThrowIfHasErrors(remainingRequests);
}
示例10: ImportToSqlAndMongo
public static void ImportToSqlAndMongo(BoardgameSimulatorData data, MongoConnection mongoData, string filePath = "../../../DataSources/xml/")
{
if (!Directory.Exists(filePath))
{
Console.WriteLine("There are no xml files present in " + filePath + " suitable for importing!");
return;
}
var files = Directory.GetFiles(filePath).Select(x => x).Where(x => Path.GetExtension(x) == ".xml");
Console.WriteLine("Importing from xml into Sql and MongoDb initialized.");
foreach (var file in files)
{
var heroes = GetHeroes(file);
var items = GetItems(file);
Console.WriteLine("importing into sql...");
AddHeroesToSql(heroes, data);
AddItemsToSql(items, data);
Console.WriteLine("importing into mongodb...");
AddHeroesToMongo(heroes, mongoData);
AddItemsToMongo(items, mongoData);
}
Console.WriteLine("Importing from xml into Sql and MongoDb completed!");
}
示例11: Authenticate
// public methods
/// <summary>
/// Authenticates the connection against the given database.
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="credential">The credential.</param>
public void Authenticate(MongoConnection connection, MongoCredential credential)
{
var nonceCommand = new CommandDocument("getnonce", 1);
var commandResult = connection.RunCommand(credential.Source, QueryFlags.None, nonceCommand, false);
if (!commandResult.Ok)
{
throw new MongoAuthenticationException(
"Error getting nonce for authentication.",
new MongoCommandException(commandResult));
}
var nonce = commandResult.Response["nonce"].AsString;
var passwordDigest = MongoUtils.Hash(credential.Username + ":mongo:" + ((PasswordEvidence)credential.Evidence).Password);
var digest = MongoUtils.Hash(nonce + credential.Username + passwordDigest);
var authenticateCommand = new CommandDocument
{
{ "authenticate", 1 },
{ "user", credential.Username },
{ "nonce", nonce },
{ "key", digest }
};
commandResult = connection.RunCommand(credential.Source, QueryFlags.None, authenticateCommand, false);
if (!commandResult.Ok)
{
var message = string.Format("Invalid credential for database '{0}'.", credential.Source);
throw new MongoAuthenticationException(
message,
new MongoCommandException(commandResult));
}
}
示例12: Initialize
/// <summary>
/// Initializes the mechanism.
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="credential">The credential.</param>
/// <returns>The initial step.</returns>
public ISaslStep Initialize(MongoConnection connection, MongoCredential credential)
{
var serviceName = credential.GetMechanismProperty<string>("SERVICE_NAME", "mongodb");
var canonicalizeHostname = credential.GetMechanismProperty<bool>("CANONICALIZE_HOST_NAME", false);
var hostname = connection.ServerInstance.Address.Host;
if (canonicalizeHostname)
{
var entry = Dns.GetHostEntry(hostname);
if (entry != null)
{
hostname = entry.HostName;
}
}
// TODO: provide an override to force the use of gsasl?
if (__useGsasl)
{
return new GsaslGssapiImplementation(
serviceName,
hostname,
credential.Username,
credential.Evidence);
}
return new WindowsGssapiImplementation(
serviceName,
hostname,
credential.Username,
credential.Evidence);
}
示例13: MongoKillCursorsMessage
internal MongoKillCursorsMessage(
MongoConnection connection,
params long[] cursorIds
)
: base(connection, MessageOpcode.KillCursors) {
this.cursorIds = cursorIds;
}
示例14: AddItemsToMongo
private static void AddItemsToMongo(List<DummyItem> items, MongoConnection mongoData)
{
mongoData.Database.DropCollection("items");
var itemsCollection = new GenericData<DummyItem>(mongoData.Database, "items");
itemsCollection.Collection.InsertBatch(items);
}
示例15: CreateConnectionPool
private static MongoConnection[] CreateConnectionPool(Int32 count)
{
var connections = new MongoConnection[count];
for (var i = 0; i < count; i++)
connections[i] = new MongoConnection(Host, Port, Serializer);
return connections;
}