Adventures on the edge

Learning new development technologies
    "When living on the bleeding edge - you sometimes have to bleed" -BillKrat

Does not implement inherited abstract member 'HttpContent.SerializeToStreamAsync(Stream, TransportContext)

So what is wrong with the following code?

It should compile, I’ve seen identical code on an internet blog suggesting it should work.  I delete the offending method SerializationToStreamAsync and have Visual Studio generate it from the interface – it produces the same issue.

It should work – but it doesn’t, keep reading for the solution!

CannotOverride

I resolved the issue in 3 easy steps, after numerous long hours of research….

First, I had to remove the existing System.Net references,  second I loaded NuGet Package manager, and finally I browsed for and installed the v4.1.0 version of the System.Net.Http package and the problem was resolved – I could now compile my solution successfully!

Fix

 

using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Salesforce.Force.UnitTests
{
internal class JsonContent : HttpContent
{
private readonly MemoryStream _stream = new MemoryStream();
public JsonContent(object value)
{

Headers.ContentType = new MediaTypeHeaderValue("application/json");
var jw = new JsonTextWriter(new StreamWriter(_stream))
{
Formatting = Formatting.Indented
};
var serializer = new JsonSerializer();
serializer.Serialize(jw, value);
jw.Flush();
_stream.Position = 0;
}
protected JsonContent(string content)
{
Headers.ContentType = new MediaTypeHeaderValue("application/json");
var sw = new StreamWriter(_stream);
sw.Write(content);
sw.Flush();
_stream.Position = 0;
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return _stream.CopyToAsync(stream);
}
protected override bool TryComputeLength(out long length)
{
length = _stream.Length;
return true;
}
public static HttpContent FromFile(string filepath)
{
string content = File.ReadAllText(filepath);
return new JsonContent(content);
}
}
}

Comments are closed