C# Cannot replace void with async Task -
i have following class derived entity framework class follows.
internal class myinitializer : createdatabaseifnotexists<configurationdbcontext> { protected override async void seed(configurationdbcontext context) { await dbhelper.addsampledataasync(context); base.seed(context); } }
i following compiler error when change "void" "async task".
return type must 'void' match overridden member
async/await best practices tell return async task instead of void. wanted make sure usage of void here 1 of acceptable scenarios using void. other options have anyways?
when overriding method must have same signature original method of base class (or interface / abstract class). imagine based on question expected behavior of seed()
synchronous operation. therefore it's not idea override , change behavior asynchronous.
instead should create second method asynchronous, , keep synchronous one.
this method below (as suggested @danielearwicker) allows have both synchronous , asynchronous version waiting asynchronous operation complete in synchronous version of seed()
internal class myinitializer : createdatabaseifnotexists<configurationdbcontext> { protected override void seed(configurationdbcontext context) { seedasync(context).wait(); } protected async task seedasync(configurationdbcontext context) { await dbhelper.addsampledataasync(context); base.seed(context) } }
Comments
Post a Comment