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

Question: Is it possible use Task<IActionResult> with refit. #1178

Open
DaveDillon opened this issue May 18, 2021 · 1 comment
Open

Question: Is it possible use Task<IActionResult> with refit. #1178

DaveDillon opened this issue May 18, 2021 · 1 comment

Comments

@DaveDillon
Copy link

I've setup a test controller using "Task" and it's interface using refit.
I suspect this isn't ideal and won't work based on what I've read but I wanted a clear answer on subject.

When I use "Task" I get the following error

"System.NotSupportedException: Deserialization of interface types is not supported"

Because the concreate class isn't specified I doubt a custom serializer would help, is that assumption correct?

I've been tasked with evaluating refit for our current project so maybe I'm missing something.
Thanks in advanced :D

@james-s-tayler
Copy link
Contributor

james-s-tayler commented May 18, 2021

In the case of using the Task<T> return type on a Refit interface, once the HttpResponseMessage has come back Refit simply hands it off to the IHttpContentSerializer implementation you've got configured in order to handle the deserialization, so whatever that supports, it will be able to do. As of Refit 6 System.Text.Json is the default, but it's also possible to use Newtonsoft.Json if you either have some backwards compatibility requirement with existing code/system you're interacting with, or it does something that System.Text.Json doesn't support.

A bit of googling seems to suggest Newtonsoft.Json can certainly handle the task of deserializing to an interface type through use of a custom converter, but when I search the same for System.Text.Json I find a lot of GitHub issues and discussions requesting this feature, which I'm not sure the current status of. RefitSettings allows you to pass any custom settings you need along to the serializer to support your use case.

So, in summary, it is theoretically possible to handle this with Refit today, at least with Newtonsoft.Json, though when/if System.Text.Json supports in the future you could switch to that serializer instead. The same will go for any library trying to provide a nice abstraction over http calls that is delegating the serialization/deserialization concerns to one of those libraries like I imagine all of them do. At least all the code I have read in all libraries like Refit seems to do so. The performance difference between System.Text.Json and Newtonsoft.Json is that, at least in my benchmarks, System.Text.Json takes half the time, and allocates only 2/3 the memory.

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

No branches or pull requests

2 participants