本文整理汇总了C#中System.IO.StringWriter.WriteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# StringWriter.WriteAsync方法的具体用法?C# StringWriter.WriteAsync怎么用?C# StringWriter.WriteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StringWriter
的用法示例。
在下文中一共展示了StringWriter.WriteAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WithProxy
public async void WithProxy()
{
var writer = new StringWriter();
var printer = Proxy.CreateProxy<HelloWorldPrinter>(async invocation =>
{
await writer.WriteAsync("John says, \"");
await invocation.Proceed();
await writer.WriteAsync("\"");
return null;
});
await printer.SayHello(writer);
Assert.AreEqual("John says, \"Hello World!\"", writer.ToString());
}
示例2: MiscWritesAsync
public static async Task MiscWritesAsync()
{
var sw = new StringWriter();
await sw.WriteAsync('H');
await sw.WriteAsync(new char[] { 'e', 'l', 'l', 'o', ' ' });
await sw.WriteAsync("World!");
Assert.Equal("Hello World!", sw.ToString());
}
示例3: FormatBodyStreamAsync
/// <summary>
/// Formats the contents of an HTTP request or response body into a string.
/// </summary>
/// <param name="bodyStream"></param>
/// <returns>
/// The request or response body stream to use (must replace the current stream).
/// </returns>
private async Task<string> FormatBodyStreamAsync(Stream bodyStream)
{
if ((bodyStream == null) || !bodyStream.CanRead || !bodyStream.CanSeek)
{
return string.Empty;
}
// Creo un vettore di caratteri usato come buffer temporaneo.
var buffer = new char[512];
// E' fondamentale che lo stream del body rimanga aperto.
using (var bodyReader = new StreamReader(bodyStream, PortableEncoding.UTF8WithoutBOM, false, buffer.Length, true))
using (var psb = _stringBuilderPool.GetObject())
using (var stringWriter = new StringWriter(psb.StringBuilder, CultureInfo.InvariantCulture))
{
// Riporto all'inizio la posizione dello stream per poterlo copiare in una stringa.
bodyStream.Position = 0;
// Avvio il ciclo di lettura a blocchi.
var readIndex = 0;
var readChars = 0;
do
{
// Leggo un blocco di caratteri dallo stream.
readChars = await bodyReader.ReadBlockAsync(buffer, 0, buffer.Length);
readIndex += readChars;
// Scrivo i caratteri letti.
await stringWriter.WriteAsync(buffer, 0, readChars);
} while (readChars == buffer.Length && readIndex < _settings.MaxLoggedBodyLength);
// Riporto all'inizio la posizione dello stream per consentire agli altri di leggerlo.
bodyStream.Position = 0;
return stringWriter.ToString();
}
}