aspnet/Mvc

ASP.NET Core & System.Net.Http.Formatting

Closed this issue · 15 comments

Is System.Net.Http.Formatting coming to ASP.NET Core? Specifically, ObjectContent, ObjectContent<T>, and the ReadAsAsync<T>() extension methods for HttpClient? I started looking into porting it but the change from MediaTypeFormatter to IInputFormatter and IOutputFormatter - and more generally, the divergence of ASP.NET MVC's and HttpClient's HTTP models - makes things difficult for someone new to ASP.NET Core.
A couple of issues:

  • The new formatters' dependence on the entire HttpContext (e.g., see InputFormatterContext) instead of just the type to be deserialized and HttpContent
  • No default collection of formatters w/o using dependency injection (to get them from MvcOptions)

@smarts the System.Net.Http.Formatting.dll assembly and its Microsoft.AspNet.WebApi.Client package are the focus of aspnet/AspNetWebStack#4. We'll ship a new version which explicitly supports .NET Standard 1.1 (and higher).

For now, import the existing package as we do in the Microsoft.AspNetCore.Mvc.WebApiCompatShim project:

{
  "dependencies": {
    "Microsoft.AspNet.WebApi.Client": "5.2.2",
    "NETStandard.Library": "1.6.1-*"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": [
        "portable-net451+win8"
      ],
      "dependencies": {
        "System.Runtime.Serialization.Xml": "4.3.0-*",
        "System.Xml.XmlSerializer": "4.3.0-*"
      }
    }
  }
}

I get a 404 when I use that issue link, but I'll take your word for it =)

Right, it's a private repo and I shouldn't have linked to it (yet). We are however working on exactly this issue.

@dougbu do I need all of the JSON you presented? I assumed I just needed the "Microsoft.AspNet.WebApi.Client": "5.2.2" part under "dependencies", but I'm getting an error about that package not supporting the netcoreapp1.0 framework (which my app is targeting).

Yes, for now the "imports" bit as well as the additional dependencies are required.

So what is the alternative for System.Net.Http.Formatting?

Whats the dotnetcore version of:

return new ObjectContent<T>(body, formatter, mediaType);
// or
return httpResponseMessage.Content.ReadAsAsync<T>(formatters);

currently there isn't one @shawnmclean. you can use the suggestion above to use the old nuget packages in a .net core project. if you want i can provide a snippet that matches the csproj style instead of project.json. alternatively you can wait until the next version of that nuget package is released, which will support .net core.

@smarts yes please, the csproj style is much appreciated. Thanks!

in your csproj…

<PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);dnxcore50;portable-net451+win8</PackageTargetFallback>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="microsoft.aspnet.webapi.client" Version="5.2.3" />
    <PackageReference Include="system.runtime.serialization.xml" Version="4.3.0" />
    <PackageReference Include="system.xml.xmlserializer" Version="4.3.*" />
</ItemGroup>

So how about that .NET Core update for microsoft.aspnet.webapi.client? Still on the way?

Here's my approximation to the old ReadAsAsync<T> method targeting netcoreapp2.0. The parts are there, they just need to be put together.

public static T ReadAsAsync<T> (this HttpContext httpContext)
{
    var options  = services.GetRequiredService<IOptions<MvcOptions>> ().Value ;
    var readers  = services.GetRequiredService<IHttpRequestStreamReaderFactory> () ;
    var metadata = services.GetRequiredService<IModelMetadataProvider>          () ;

    var input = new InputFormatterContext (httpContext, String.Empty,
        new ModelStateDictionary (0), metadata.GetMetadataForType (typeof (T)),
        readers.CreateReader) ;

    foreach (var formatter in options.InputFormatters)
        if (formatter.CanRead (input))
        {
            var result = await formatter.ReadAsync (input).ConfigureAwait (false) ;
            if (result.IsModelSet)
                return (T) result.Model ;
        }

    throw new UnsupportedMediaTypeException (...) ;
}

Great effort @atykhyy ! The problem [for me] is that the code above is tied to the ASP.NET Core MVC abstraction(s), not HttpClient and its abstractions.

Yes, these are for server-side use. There is a netstandard2.0-compatible version of Microsoft.AspNet.WebApi.Client coming up for the client side. I'm using 5.2.4-alpha1 from the nightly server until the release.

I'm aware of the upcoming package. This issue pre-dates the tracking issue for the upcoming package and is referenced there. Unfortunately, this isn't a case for which a pre-release package is desirable. Thanks for the suggestion though @atykhyy =)

@junkomatic Looks like support for .NET Core 2.0 has just been added to Microsoft.AspNet.WebApi.Client.