(Go: >> BACK << -|- >> HOME <<)

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

Is it possible to use refit with HttpClientFactory in .net framework winform application? #1004

Open
softboy99 opened this issue Nov 25, 2020 · 7 comments

Comments

@softboy99
Copy link

Hi,
I'm not familiar with HttpClientFactory,which appears in asp.net core 2.1 above,Is it possible to use refit with HttpClientFactory in .net framework winform application?

@james-s-tayler
Copy link
Contributor

HttpClientFactory is only part of .NET from .NET Core 2.1 and onwards, so .NET 4.x doesn't have it. Refit has full support for HttpClientFactory / DependencyInjection, but doesn't require it. Refit works by making http requests with HttpClient which is part of .NET 4.5, so any version of .NET >= 4.5 is compatible with Refit.

It just means you'll need to use the RestService.For<T> semantics. If your application uses some Depedency Injection container like Autofac, Ninject or LightInject etc you can simply register a Refit client using your DI container with the RestService.For. In .NET Core the Refit's support for HttpClientFactory is just doing that same thing for you basically.

In .NET Core 2.1+ all HttpClientFactory really does is manage the lifecycle of HttpClient objects a bit more intelligently to avoid some problems with HttpClient in older versions of .NET Framework.

Does that answer your question?

@softboy99
Copy link
Author

Hi
It seems that, in .net framework, we also can use HttpClientFactory. Just adding Microsoft.Extensions.Http and Microsoft.Extensions.DependencyInjection, Could you please kindly help to give some sample code for that? Thanks a lot for your great help.

@james-s-tayler
Copy link
Contributor

True. I forgot it's technically netstandard2.0 compatible. The .NET 4.x project I work on isn't really in a position to upgrade to even make use of netstandard2.0 stuff, so I tend to equate netstandard2.0 exclusively with .NET Core in my mind.

Getting HttpClientFactory to work in .NET 4.x isn't a Refit specific question, so you might have better luck asking on Stack Overflow for that.

On the flipside - does your project actually require the use of HttpClientFactory? It only really helps solve the problems of DNS updates not being respected by HttpClient and socket exhaustion from instantiating too many HttpClients that keep the sockets open for a while after the request completes. I'd imagine in a winforms application this might not be a problem?

Anyway, good luck.

@softboy99
Copy link
Author

Hi,
I know how to do that in a normal .net framework winform application with httpclientfactory, but i don't know how to integrate it with your refit be configured together with httpclientfactory. Your documentation only for asp.net core. Could you please kindly help me? Thanks a lot.

@james-s-tayler
Copy link
Contributor
james-s-tayler commented Nov 28, 2020

If you have already got HttpClientFactory working in your project using the IServiceCollection interface from the netstandard2.0 dependency injection then it should be the same as the docs using the AddRefitClient<T> extension method on IServiceCollection.

If you've gotten HttpClientFactory working in your project without somehow making use of IServiceCollection then you'll have to provide some details on what you've done, or what you have tried so far.

@softboy99
Copy link
Author

Hi,
i don't know how to use IServiceCollection interface in winform with .net framework, IServiceCollection interface is in asp.net

@ChrisPulman
Copy link
Member

You could see if Dapplo.Microsoft.Extensions.Hosting.ReactiveUI.WinForms works for you.

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

3 participants