You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If we see usually variant it will be the same as using interface and init client in the constructor. But we have named client or typed client and can set client on start application and use it in other places. It will very help if for initialization we have more parameters than ip, login, password.
When start it:
services.AddHttpClient("testClient", client =>
{
client.BaseAddress = new Uri(url);
})
.AddHttpMessageHandler<AuthSystemUserTokenToHeaderHandler>();
When need client: var client = _clientFactory.CreateClient("testClient");
Or are you asking for a Factory object and IFactory interface??
And Factory would have methods to create FtpClient and AsyncFtpClient??
Yes, I about a factory with methods to create FtpClient and AsyncFtpClient. And also I want to set clients parameters at start (as my example) and create tunned clients from any place.
I can work on adding client factories if needed! @kostazol I think the reason HttpClientFactory exists is because the usage of HttpClient is quite not intuitive as the IDisposable behaviour doesn't conform to with the rest of the framework.
@robinrodricks@kostazol Here is the spec for design of IAsynFtpClientFactory. Would appreciate feedback and suggestions before I start implementing it. The design inspiration for this Quartz.NET and Microsoft.Extensions.Http.
FTP Client Factory
The goal of IAsynFTPClientFactory is to create named (and later typed) FTP clients that can be configured at application start and reused throughout the application. Multiple clients with different configuration can be added and later injected as dependencies. In essence the goal is here: Configure once, use everywhere.
FTP Clients are configured by settings up appropriate values for Action<IAsynFTPClientConfigurator>. This interfaces exposes values for settings up FTP Client either by providing FTPConfig instance or by providing the most important properties required to configure the FTP Client. Additionally, the client can also be configured using sections from appsettings.json file if desired.
An IAsyncFTPClientBuilder is used to build IAsynFTPClient using the options provided in IAsynFTPClientConfigurator. This class is used internally to build an FTP Client every time Create is called on the factory.
Each new call to Create creates a new FTP Client. The onus of disposing the FTP client is on the user of the factory. I am still not sure on this behaviour. For example, it is possible for the factory to manage the lifetime, but this requires the client to know about this implicit behaviour.
Possible exceptions can arise if the client wants to
Create a named FTP Client has not been registered.
FTP client with the same name is already registered.
Configuration fails because FTPConfig validation fails. This error won't be surfaced till a call to Create is made.
Minimal examples of the FTP Client Factory API
// create a named FTP ClientIAsyncFTPClientFactoryftpFactory=newDefaultAsyncFTPClientFactory("ftp_client_v1", options =>{// setup FtpConfig options here});// in controller for examplepublicExample:ControllerBase{privateIAsyncFTPClientFactory_factory;publicExample(IAsynFTPClientFactory factory){_factory=factory;}// using the factory to create FTP Client to upload filespublicUploadFile(){varclient=_factory.Create("ftp_client_v1");// throws not registered expection if the named client has not been registeredclient.uploadFile(@"D:\Github\FluentFTP\README.md", "/public_html/temp/README.md);
}}
Activity
robinrodricks commentedon Feb 17, 2023
We have interfaces you can use https://github.com/robinrodricks/FluentFTP/tree/master/FluentFTP/Client/Interfaces
kostazol commentedon Feb 17, 2023
May you send some micro example?
If I use IAsyncFtpClient via DI into constructor, how I will set ip address, login and password?
robinrodricks commentedon Feb 17, 2023
AsyncFtpClient implements IAsyncFtpClient, cant you use that?
Manually set the properties of AsyncFtpClient once your DI container passes a concrete instance...
Like
And then register the type:
kostazol commentedon Feb 17, 2023
Thank you! But If in the future you will add factory it will be cool.
robinrodricks commentedon Feb 17, 2023
Can you give an example of what you are looking for?
kostazol commentedon Feb 17, 2023
https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory
If we see usually variant it will be the same as using interface and init client in the constructor. But we have named client or typed client and can set client on start application and use it in other places. It will very help if for initialization we have more parameters than ip, login, password.
When start it:
When need client:
var client = _clientFactory.CreateClient("testClient");robinrodricks commentedon Feb 17, 2023
See even in this example, you are manually setting properties on
client, so you can do the same with FluentFTPclient. Nothing is stopping you.Or are you asking for a Factory object and IFactory interface??
And Factory would have methods to create FtpClient and AsyncFtpClient??
kostazol commentedon Feb 21, 2023
Yes, I about a factory with methods to create FtpClient and AsyncFtpClient. And also I want to set clients parameters at start (as my example) and create tunned clients from any place.
parthvnp commentedon Feb 23, 2023
I can work on adding client factories if needed! @kostazol I think the reason
HttpClientFactoryexists is because the usage ofHttpClientis quite not intuitive as theIDisposablebehaviour doesn't conform to with the rest of the framework.robinrodricks commentedon Feb 23, 2023
@parthvnp Go ahead please!
parthvnp commentedon Feb 28, 2023
@robinrodricks @kostazol Here is the spec for design of
IAsynFtpClientFactory. Would appreciate feedback and suggestions before I start implementing it. The design inspiration for this Quartz.NET and Microsoft.Extensions.Http.FTP Client Factory
IAsynFTPClientFactoryis to create named (and later typed) FTP clients that can be configured at application start and reused throughout the application. Multiple clients with different configuration can be added and later injected as dependencies. In essence the goal is here: Configure once, use everywhere.Action<IAsynFTPClientConfigurator>. This interfaces exposes values for settings up FTP Client either by providingFTPConfiginstance or by providing the most important properties required to configure the FTP Client. Additionally, the client can also be configured using sections fromappsettings.jsonfile if desired.IAsyncFTPClientBuilderis used to buildIAsynFTPClientusing the options provided inIAsynFTPClientConfigurator. This class is used internally to build an FTP Client every timeCreateis called on the factory.Createcreates a new FTP Client. The onus of disposing the FTP client is on the user of the factory. I am still not sure on this behaviour. For example, it is possible for the factory to manage the lifetime, but this requires the client to know about this implicit behaviour.Createis made.Minimal examples of the FTP Client Factory API
kostazol commentedon Feb 28, 2023
Everything, is great. But about "The onus of disposing the FTP client is on the user of the factory. I am still not sure on this behaviour." as I know in Microsoft.Extensions.Http usually factory control disposing. I don't know details but it helps control "connection pool" ( HTTP persistent connection ), some discution:
https://stackoverflow.com/questions/54597303/should-i-cache-and-reuse-httpclient-created-from-httpclientfactory
robinrodricks commentedon Feb 28, 2023
@parthvnp Sounds good, you have my blessing to go ahead!
FanDjango commentedon Mar 21, 2023
I have moved this to the Wiki: https://github.com/robinrodricks/FluentFTP/wiki/Future-Plans