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.
0 replies
-
@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.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The title has the question. So I want to initialize TelegramUserBot during injection but
.Initialize()
is an async method and factory dont accept aTask<>
return.IConfiguration To<T>(global::System.Func<IContext, T> factory);
Any solution ? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions