When writing a response at the service, the WriteToStreamAsync overload taking in the CancellationToken is not getting invoked.
In the below example, the commented code gets called but not the overloading taking in cancellation token.
Custom Formatter:
```
public class TextPlainFormatter : MediaTypeFormatter
{
public TextPlainFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
this.SupportedEncodings.Add(Encoding.UTF8);
}
public override bool CanWriteType(Type type)
{
return type == typeof(string);
}
public override bool CanReadType(Type type)
{
return false;
}
//// NOTE: this works
//public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
//{
// StreamWriter sw = new StreamWriter(writeStream);
// await sw.WriteAsync(value.ToString());
// await sw.FlushAsync();
//}
public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content,
TransportContext transportContext, CancellationToken cancellationToken)
{
StreamWriter sw = new StreamWriter(writeStream);
await sw.WriteAsync(value.ToString());
await sw.FlushAsync();
}
}
```
ObjectContent's SerializeToStreamAsync needs to invoke the overload with cancellation token:
```
/// <summary>
/// Asynchronously serializes the object's content to the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to which to write.</param>
/// <param name="context">The associated <see cref="TransportContext"/>.</param>
/// <returns>A <see cref="Task"/> instance that is asynchronously serializing the object's content.</returns>
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return _formatter.WriteToStreamAsync(ObjectType, Value, stream, this, context);
}
```
In the below example, the commented code gets called but not the overloading taking in cancellation token.
Custom Formatter:
```
public class TextPlainFormatter : MediaTypeFormatter
{
public TextPlainFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
this.SupportedEncodings.Add(Encoding.UTF8);
}
public override bool CanWriteType(Type type)
{
return type == typeof(string);
}
public override bool CanReadType(Type type)
{
return false;
}
//// NOTE: this works
//public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
//{
// StreamWriter sw = new StreamWriter(writeStream);
// await sw.WriteAsync(value.ToString());
// await sw.FlushAsync();
//}
public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content,
TransportContext transportContext, CancellationToken cancellationToken)
{
StreamWriter sw = new StreamWriter(writeStream);
await sw.WriteAsync(value.ToString());
await sw.FlushAsync();
}
}
```
ObjectContent's SerializeToStreamAsync needs to invoke the overload with cancellation token:
```
/// <summary>
/// Asynchronously serializes the object's content to the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to which to write.</param>
/// <param name="context">The associated <see cref="TransportContext"/>.</param>
/// <returns>A <see cref="Task"/> instance that is asynchronously serializing the object's content.</returns>
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return _formatter.WriteToStreamAsync(ObjectType, Value, stream, this, context);
}
```