0

I have a method in repository with this implementation which returns a Task

Task<IList<ApplicationDTO>> GetAllAppsRequestAsync(); 

I write the getter which call this async method

public IList<ApplicationPropertyModel> ApplicationsList
    {
        get
        {
            IList<ApplicationDTO> applicationDTO = _appService.GetAllAppsRequestAsync();
            if (applicationDTO != null)
            {
                applicationList = mapper.DefaultContext.Mapper.Map<IList<ApplicationPropertyModel>>(applicationDTO);
            }

            return applicationList;
        }
        set => Set(ref applicationList, value);
    }

_appService.GetAllAppsRequestAsync(); - return task, and proble that getter doesn't have async

I try to write delegate, i take the problem when i map the models.

I try to find something but whatever I do, I take deathlock or don't wait my task with result.(try getAwaiter, wait,some example with wrapping) and doesn't know what to do?

forget i try to it by this

public static void RunSync(Func<Task> func)
{
    AsyncHelper._myTaskFactory
      .StartNew<Task>(func)
      .Unwrap()
      .GetAwaiter()
      .GetResult();
}

but doesn't take the result, before project will finish

Node

I should use the property because it binding with with my itemControl in wpr xaml page.

Edit

private async Task<IList<ApplicationPropertyModel>> TakeAppListAsync()
    {
        IList<ApplicationDTO> applicationDTO = await _appService.GetAllAppsRequestAsync();

        if (applicationDTO != null)
        {
            applicationList = mapper.DefaultContext.Mapper.Map<IList<ApplicationPropertyModel>>(applicationDTO);
        }

        return applicationList;
    }
Andrey
  • 147
  • 3
  • 12
  • 4
    You have [an oxymoron](https://blog.stephencleary.com/2013/01/async-oop-3-properties.html) – H H Jun 08 '19 at 16:12
  • I read it, but what to do in my situation if i use mvvm light, where not need realize NotifyPropertyChanged and if i need take the result in getter – Andrey Jun 08 '19 at 16:37
  • Fundamentally, the issue here is that your property implementation is completely messed up. If you call the setter of your property and then immediately call the getter, the getter should return exactly what you set. Your implementation is not guaranteed to do that. If you need to asynchronously assign a new value to the property, that code should be _outside_ of the property, and leave the property as a simple one. Then you can use a real async method for the assignment, and assign the property value explicitly when the async method completes. – Peter Duniho Jun 08 '19 at 18:27
  • 1
    If you insist on mixing async operation with your property, the marked duplicate provides some advice on how to do this successfully (if not safely). There are numerous other Q&A on this site, if you would just search, discussing the relationship between async operations and C# properties. – Peter Duniho Jun 08 '19 at 18:28
  • Peter Duniho - thank you for your advices, i write as duplicate, but 1 more question. If I want to create that right, I write the method (edit question), but it return Task , and i can't process it in property because need write it with await/async, how write the the method in this situation ( I don't ever collide with this, and want to know how do that right) – Andrey Jun 09 '19 at 08:06

0 Answers0