refit icon indicating copy to clipboard operation
refit copied to clipboard

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

Open softboy99 opened this issue 5 years ago • 7 comments

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?

softboy99 avatar Nov 25 '20 11:11 softboy99

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<T>. 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?

james-s-tayler avatar Nov 26 '20 09:11 james-s-tayler

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.

softboy99 avatar Nov 26 '20 11:11 softboy99

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.

james-s-tayler avatar Nov 27 '20 09:11 james-s-tayler

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.

softboy99 avatar Nov 28 '20 02:11 softboy99

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.

james-s-tayler avatar Nov 28 '20 03:11 james-s-tayler

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

softboy99 avatar Apr 13 '23 07:04 softboy99

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

ChrisPulman avatar Apr 13 '23 07:04 ChrisPulman