0

Regarding to this topic and answer I have adapters for my async services registrations : Async provider in .NET Core DI

And it looks very fine. But I have some services, where I have properties in interface. Properties cant be async, but I need to await client object : var client = await this.connectedClient.Value;

I mean I cant use public bool Connected => (await this.connectedClient.Value).Connected;

What should I do in this case?

public interface IMyAppService
{
    bool Connected{ get; }
    string Host{ get; }
    Task<Data> GetData();
    Task SendData(Data data);
}

PS : I dont want to .Result and .GetAwaiter().GetResult() etc. I know Its potentially deadlock there

andrey1567
  • 97
  • 1
  • 13

1 Answers1

0

Given the adapter structure suggested by Steven in the linked question, you can simply implement the Connected property as:

public bool Connected => this.connectedClient.IsValueCreated;

but in the general case, there is no way to run code asynchronously in a property getter or setter.

Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46
  • Thanks. Unfortunatelly, Connected not related to connectedClient. Its related to another internal client. And also I have many properties in class, here two is just for example. – andrey1567 Feb 12 '21 at 09:01