Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

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

Closed
smarts opened this issue Nov 23, 2016 · 15 comments
Closed

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

smarts opened this issue Nov 23, 2016 · 15 comments

Comments

@smarts
Copy link

smarts commented Nov 23, 2016

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)
@dougbu
Copy link
Member

dougbu commented Nov 24, 2016

@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-*"
      }
    }
  }
}

@smarts smarts closed this as completed Nov 24, 2016
@smarts
Copy link
Author

smarts commented Nov 24, 2016

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

@dougbu
Copy link
Member

dougbu commented Nov 24, 2016

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

@smarts
Copy link
Author

smarts commented Nov 28, 2016

@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).

@dougbu
Copy link
Member

dougbu commented Nov 28, 2016

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

@shawnmclean
Copy link

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);

@smarts
Copy link
Author

smarts commented May 13, 2017

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.

@shawnmclean
Copy link

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

@smarts
Copy link
Author

smarts commented May 16, 2017

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>

@junkomatic
Copy link

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

@atykhyy
Copy link

atykhyy commented Nov 11, 2017

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 (...) ;
}

@smarts
Copy link
Author

smarts commented Nov 14, 2017

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.

@atykhyy
Copy link

atykhyy commented Nov 15, 2017

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.

@smarts
Copy link
Author

smarts commented Nov 16, 2017

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 =)

@mguinness
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants