本文整理汇总了C#中RavenJArray.WriteTo方法的典型用法代码示例。如果您正苦于以下问题:C# RavenJArray.WriteTo方法的具体用法?C# RavenJArray.WriteTo怎么用?C# RavenJArray.WriteTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RavenJArray
的用法示例。
在下文中一共展示了RavenJArray.WriteTo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FlushBatch
private static void FlushBatch(string instanceUrl, List<RavenJObject> batch)
{
var sw = Stopwatch.StartNew();
long size;
using (var webClient = new WebClient())
{
webClient.Headers.Add("Content-Type", "application/json; charset=utf-8");
webClient.UseDefaultCredentials = true;
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
using (var stream = new MemoryStream())
{
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
using (var jsonTextWriter = new JsonTextWriter(streamWriter))
{
var commands = new RavenJArray();
foreach (var doc in batch)
{
var metadata = doc.Value<RavenJObject>("@metadata");
doc.Remove("@metadata");
commands.Add(new RavenJObject
{
{"Method", "PUT"},
{"Document", doc},
{"Metadata", metadata},
{"Key", metadata.Value<string>("@id")}
});
}
commands.WriteTo(jsonTextWriter);
jsonTextWriter.Flush();
streamWriter.Flush();
stream.Flush();
size = stream.Length;
using (var netStream = webClient.OpenWrite(instanceUrl + "bulk_docs", "POST"))
{
stream.WriteTo(netStream);
netStream.Flush();
}
}
}
}
Console.WriteLine("Wrote {0} documents [{1:#,#} kb] in {2:#,#} ms",
batch.Count, Math.Round((double)size / 1024, 2), sw.ElapsedMilliseconds);
batch.Clear();
}
示例2: TryReplicationDocuments
private bool TryReplicationDocuments(RavenConnectionStringOptions destination, RavenJArray jsonDocuments)
{
try
{
log.Debug("Starting to replicate {0} documents to {1}", jsonDocuments.Length, destination);
var request = (HttpWebRequest)WebRequest.Create(destination.Url + "/replication/replicateDocs?from=" + UrlEncodedServerUrl());
request.UseDefaultCredentials = true;
request.ContentType = "application/json; charset=utf-8";
request.Credentials = destination.Credentials ?? CredentialCache.DefaultNetworkCredentials;
request.Method = "POST";
using (var stream = request.GetRequestStream())
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
{
jsonDocuments.WriteTo(new JsonTextWriter(streamWriter));
streamWriter.Flush();
stream.Flush();
}
using (request.GetResponse())
{
log.Info("Replicated {0} documents to {1}", jsonDocuments.Length, destination);
}
return true;
}
catch (WebException e)
{
var response = e.Response as HttpWebResponse;
if (response != null)
{
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var error = streamReader.ReadToEnd();
log.WarnException("Replication to " + destination + " had failed\r\n" + error, e);
}
}
else
{
log.WarnException("Replication to " + destination + " had failed", e);
}
return false;
}
catch (Exception e)
{
log.WarnException("Replication to " + destination + " had failed", e);
return false;
}
}
示例3: TryReplicationAttachments
private bool TryReplicationAttachments(ReplicationStrategy destination, RavenJArray jsonAttachments)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(destination.ConnectionStringOptions.Url + "/replication/replicateAttachments?from=" + UrlEncodedServerUrl());
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = destination.ConnectionStringOptions.Credentials ?? CredentialCache.DefaultNetworkCredentials;
request.Method = "POST";
using (var stream = request.GetRequestStream())
{
jsonAttachments.WriteTo(new BsonWriter(stream));
stream.Flush();
}
using (request.GetResponse())
{
log.Info("Replicated {0} attachments to {1}", jsonAttachments.Length, destination);
}
return true;
}
catch (WebException e)
{
var response = e.Response as HttpWebResponse;
if (response != null)
{
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var error = streamReader.ReadToEnd();
log.WarnException("Replication to " + destination + " had failed\r\n" + error, e);
}
}
else
{
log.WarnException("Replication to " + destination + " had failed", e);
}
return false;
}
catch (Exception e)
{
log.WarnException("Replication to " + destination + " had failed", e);
return false;
}
}