Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FormUrlEncodedMediaTypeFormatter to client formatting assemblies #77

Closed
dougbu opened this issue Oct 19, 2017 · 2 comments
Closed

Comments

@dougbu
Copy link
Member

dougbu commented Oct 19, 2017

Creating new work to track work @YakhontovYaroslav has done in #76.

Use case

Copied from #4 (comment) with a few edits:

I'm talking about https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Net.Http.Formatting/Formatting/FormUrlEncodedMediaTypeFormatter.cs this class. Its included in https://github.com/aspnet/AspNetWebStack/tree/master/src/System.Net.Http.Formatting , but missing from https://github.com/aspnet/AspNetWebStack/tree/master/src/System.Net.Http.Formatting.NetStandard and Core versions.

I'm not looking for server side implementation. As i mentioned above, we are using this formatter (as well as Json and Xml from same namespace) to dynamically build HttpClient pipelines using DelegatingHandler and Castle.Core proxies. Its neat way to declare whole serialization\deserialization pipelines using just interface declaration with custom attributes like ContentType, Accept, Route, Method etc. Many 3rd party APIs still uses FormUrlEncoded in POST and GET requests, and we must somehow integrate with them. Removing FormUrlEncodedMediaTypeFormatter from Microsoft.AspNet.WebApi.Client package will force us (and many people who used it similar way) to create our own implementation of FormData formatter, which seems unnecessary, when package already contains this functionality, which can be easily ported to .netstandard.

Code Example of such interface:

    [RestContract("http://example.com/api", timeout: 180)]
    public interface IExampleApiClient
    {
        [RestMethod("/v2/login", RestMethod.Post)]
        [Accept("application/x-www-form-urlencoded")]
        [ContenType("application/json")]
        Task<LoginResult> LoginAsync(LoginModel model, [Header("HeaderName")] headerValue);
    }

Later we use helper method to create runtime implementation with necessary DelegatingHandlers and Formatters to serialize and deserialize data based on attributes. In the example above, generated proxy will use https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Net.Http.Formatting/Formatting/JsonMediaTypeFormatter.cs for serializing requests and FormUrlEncodedMediaTypeFormatter for deserializing responses.

In fact there is no changes needed to achieve this, besides adding file link to csproj. I`ve event created pull request for this: #76

This only affects .NetStandard project, as for some reason i cant open .Core version of project in VS, its crashing with NullReferenceException. But same change there would be good.
This pull request adds only FormUrlEncodedMediaTypeFormatter back, but there may be other files that simply left abandoned in new projects.

@Eilon Eilon added this to the 5.2.4 milestone Oct 19, 2017
@Eilon
Copy link
Member

Eilon commented Oct 19, 2017

Chatted with @dougbu and this seems reasonable. We should get the type in both of the client TFMs of this package: the PCL one and the .NET Standard one.

@dougbu
Copy link
Member Author

dougbu commented Oct 22, 2017

d86dda5

@dougbu dougbu closed this as completed Oct 22, 2017
@dougbu dougbu added 3 - Done and removed 1 - Ready labels Oct 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants