Return an element in a WCF method that uses wait

I am creating a RESTful web service using WCF. Consider the following service contract:

[ServiceContract]
    public interface IItemsSaleService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getItemDetails/{itemId}")]
        SaleItem GetItemDetails(string itemId); //GET
}

//IMPLEMENTATION

async public SaleItem GetItemDetails(string itemId) //This wont work. It should be Task<SaleItem>
        {

            SaleItem item = await FindItem(itemId);
            return item;
        }

Assume FindItem is a third party API method. Since I am using await, the GetItemDetails method must be marked as async and therefore cannot return SaleItem. It should return void or Task, which will not work for me, since the HAS method should return SaleItem for the serializer in order to convert it to JSON and return it to the client.

How to fix it?

Another related question:

Method A calls Method B calls method C - method C uses wait. Does this automatically require that all other methods be asynchronous and therefore always return Task <> or void?

+4
1

, FindItem API . await, GetItemDetails async , , SaleItem. void Task, , HAS SaleItem , JSON .

. MSDN:

, , , . , - , .

, WCF . Noseratios: WCF

A B C - C . , , , Task < > void?

. , await, async Task, Task<T>, void. , void return async async .

, async , Task, : " Task.Run(); ; " " return Task.Run()"?

+4

Source: https://habr.com/ru/post/1544124/


All Articles