本文整理汇总了C#中System.Net.TransportContext类的典型用法代码示例。如果您正苦于以下问题:C# TransportContext类的具体用法?C# TransportContext怎么用?C# TransportContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TransportContext类属于System.Net命名空间,在下文中一共展示了TransportContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteToStreamAsync
//OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext)
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
string uri = HttpContext.Current.Request.Url.Query;
string method = HttpContext.Current.Request.HttpMethod;
if(HttpContext.Current.Request.HttpMethod =="GET")
{
return Task.Factory.StartNew(() =>
{
var writer = new StreamWriter(writeStream);
var query = HttpUtility.ParseQueryString(uri);
string callback = query[CallbackQueryParameter];
writer.Write(callback + "(");
writer.Flush();
base.WriteToStreamAsync(type, value, writeStream, content, transportContext).Wait();
writer.Write(")");
writer.Flush();
});
}
else
{
return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}
}
示例2: WriteToStreamAsync
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, TransportContext transportContext)
{
if (type == null) throw new ArgumentNullException("type");
if (writeStream == null) throw new ArgumentNullException("writeStream");
var tcs = new TaskCompletionSource<object>();
try
{
if (_writerSettings == null)
{
this._writerSettings = new XmlWriterSettings()
{
OmitXmlDeclaration = true,
Indent = true,
Encoding = SelectCharacterEncoding(content != null ? content.Headers : null),
CloseOutput = false
};
}
using (XmlWriter writer = XmlWriter.Create(writeStream, _writerSettings))
{
XmlSerializer xmlSerializer = new XmlSerializer(type);
xmlSerializer.Serialize(writer, value, _namespaces);
tcs.SetResult(null);
}
}
catch (Exception e)
{
tcs.SetException(e);
}
return tcs.Task;
}
示例3: StreamVideo
public async Task StreamVideo(Stream stream, HttpContent content, TransportContext transport)
{
try
{
var buffer = new byte[65536];
var filename = Path.Combine(String.Format("{0}{1}video.{2}", System.Web.HttpContext.Current.Server.MapPath("~"), Constants.FilePath, _fileExtension));
using (var file = File.Open(filename, FileMode.Open, FileAccess.Read))
{
var videoLength = (int)file.Length;
var placeInFile = 1;
while (videoLength > 0 && placeInFile > 0)
{
placeInFile = file.Read(buffer, 0, Math.Min(videoLength, buffer.Length));
await stream.WriteAsync(buffer, 0, placeInFile);
videoLength -= placeInFile;
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.InnerException);
}
finally
{
stream.Close();
}
}
示例4: SerializeToStreamAsync
protected override Task SerializeToStreamAsync (Stream stream, TransportContext context)
{
if (OnSerializeToStreamAsync != null)
OnSerializeToStreamAsync ();
return base.SerializeToStreamAsync (stream, context);
}
示例5: WriteToStreamAsync
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
return Task.Factory.StartNew(() =>
{
XmlWriter writer = new XmlTextWriter(writeStream, Encoding.UTF8);
// todo: klopt het dat Bundle wel en <?xml ...> header heeft een Resource niet?
// follow up: is now ticket in FhirApi. Check later.
if (type == typeof(OperationOutcome))
{
Resource resource = (Resource)value;
FhirSerializer.SerializeResource(resource, writer);
}
else if (type == typeof(ResourceEntry))
{
ResourceEntry entry = (ResourceEntry)value;
FhirSerializer.SerializeResource(entry.Resource, writer);
content.Headers.SetFhirTags(entry.Tags);
}
else if (type == typeof(Bundle))
{
FhirSerializer.SerializeBundle((Bundle)value, writer);
}
else if (type == typeof(TagList))
{
FhirSerializer.SerializeTagList((TagList)value, writer);
}
writer.Flush();
});
}
示例6: SerializeToStreamAsync
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
if (HasNoData())
return new CompletedTask<bool>(true);
using ( var undisposableStream = new UndisposableStream(stream) )
using ( var bufferedStream = new BufferedStream(undisposableStream))
{
var writer = new StreamWriter(bufferedStream, DefaultEncoding);
if (string.IsNullOrEmpty(Jsonp) == false)
{
writer.Write(Jsonp);
writer.Write("(");
}
Data.WriteTo(new JsonTextWriter(writer)
{
Formatting = IsOutputHumanReadable ? Formatting.Indented : Formatting.None,
}, Default.Converters);
if (string.IsNullOrEmpty(Jsonp) == false)
writer.Write(")");
writer.Flush();
}
return new CompletedTask<bool>(true);
}
示例7: WriteToStream
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
var buffer = new byte[65536];
using (var video = new MemoryStream(this.videoData))
{
var length = (int)video.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
}
}
}
catch (Exception ex)
{
return;
}
finally
{
outputStream.Close();
}
}
示例8: WriteToStream
async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
var buffer = new byte[_chunkSize];
using (var inputStream = OpenInputStream())
{
var length = (int)inputStream.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = inputStream.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
}
}
}
catch (HttpException)
{
}
finally
{
outputStream.Close();
outputStream.Dispose();
}
}
示例9: OnWriteToStreamAsync
protected override Task OnWriteToStreamAsync(Type type, object value, Stream stream,
HttpContentHeaders contentHeaders,
FormatterContext formatterContext,
TransportContext transportContext)
{
var serializer = JsonSerializer.Create(jsonSerializerSettings);
var tcs = new TaskCompletionSource<object>();
try
{
using (var streamWriter = new StreamWriter(stream, Encoding))
{
using (var jsonTextWriter = new JsonTextWriter(streamWriter))
{
serializer.Serialize(jsonTextWriter, value);
tcs.SetResult(null);
}
}
}
catch (Exception ex)
{
tcs.SetException(ex);
}
return tcs.Task;
}
示例10: WriteToStreamAsync
public override async Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContent content, TransportContext transportContext)
{
using (stream)
{
Encoding encoding = SelectCharacterEncoding(content.Headers);
using (var writer = new StreamWriter(stream, encoding))
{
var individuals = value as IEnumerable<Individual>;
if (individuals != null)
{
foreach (var individu in individuals)
{
await writer.WriteLineAsync(String.Format("{0,-10};{1,-10};{2,-10}", individu.Id, individu.FirstName, individu.LastName));
}
await writer.FlushAsync();
}
var individual = value as Individual;
if (individual != null)
{
await writer.WriteLineAsync(String.Format("{0,-10};{1,-10};{2,-10}", individual.Id, individual.FirstName, individual.LastName));
await writer.FlushAsync();
}
}
}
}
示例11: WriteToStream
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
var channelBinding= context.GetChannelBinding(ChannelBindingKind.Endpoint);
var buffer = new byte[65536];
using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read))
{
var length = (int)video.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
}
}
}
catch (HttpResponseException ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}
finally
{
outputStream.Close();
}
}
示例12: OnStreamAvailable1
public static void OnStreamAvailable1(Stream stream, HttpContent headers, TransportContext context)
{
StreamWriter streamwriter = new StreamWriter(stream);
//_streammessage.TryAdd(_streammessage.Count+1,streamwriter);
StartWriting(streamwriter);
}
示例13: WriteToStreamAsync
public override Task WriteToStreamAsync(Type type, object value, Stream output, HttpContent content, TransportContext transportContext)
{
var writer = new StreamWriter(output);
JSON.Serialize(value, writer, _options);
writer.Flush();
return Done;
}
示例14: SerializeToStreamAsync
/// <summary>
/// Serialize the HTTP content to a stream as an asynchronous operation.
/// </summary>
/// <param name="stream">The target stream.</param>
/// <param name="context">Information about the transport (channel binding token, for example). This parameter may be null.</param>
/// <returns>
/// Returns <see cref="T:System.Threading.Tasks.Task" />.The task object representing the asynchronous operation.
/// </returns>
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
using (Stream compressedStream = new GZipStream(stream, CompressionMode.Compress, true))
{
await this._originalContent.CopyToAsync(compressedStream);
}
}
示例15: WriteToStreamAsync
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
return Task.Factory.StartNew(() =>
{
WriteHTMLOutput(type, value, writeStream);
});
}