-
Notifications
You must be signed in to change notification settings - Fork 29
-
The title has the question. So I want to initialize TelegramUserBot during injection but .Initialize() is an async method and factory dont accept a Task<> return.
IConfiguration To<T>(global::System.Func<IContext, T> factory);
Any solution ? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
It seems that what I thought was wrong. What would be the correct way to initialize this class would be the correct question.
Beta Was this translation helpful? Give feedback.
All reactions
-
@alex6dj Unfortunately for factories you can't use the syntax with the "async" keyword right now. But you can do it like this:
using Pure.DI; var composition = new Composition(); var consumer = composition.Consumer; await consumer.RunAsync(); DI.Setup(nameof(Composition)) .Bind().To(ctx => { ctx.Inject(out Bot bot); // Returns Task<Bot> return bot.InitializeAsync() .ContinueWith(_ => bot); }) .Root<Consumer>("Consumer"); class Bot { public async Task InitializeAsync() { Console.WriteLine("Initialize"); await Task.Delay(3000); Console.WriteLine("Initialized"); } public void DoSomethig() => Console.WriteLine("Done"); } class Consumer(Task<Bot> botTask) { public async Task RunAsync() { Console.WriteLine("Start"); var bot = await botTask; bot.DoSomethig(); Console.WriteLine("Finish"); } }
I'll think about a solution to this issue, but I'm not sure it can be done easily yet.
Beta Was this translation helpful? Give feedback.
All reactions
-
@NikolayPianikov thanks for the fast answer. Im using this DI library for NativeAOT publication.
Im using Rx.Net in my apps and doing for now a similar workaround for the initialization step and its working ok.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1