Home > database >  Not all code paths return a value on Task
Not all code paths return a value on Task

Time:10-27

I need to wait for the whole task to finish then return a value. My code gives me an error of "Not all code paths return a value". Could you show me how to fix this? I am confused really with Tasks.

 public async override Task<TagInfoReply> GetInfo(TagInfoRequest request, ServerCallContext context)
        {
            var tagInfo = JsonSerializer.Deserialize<TagInfo>(request.TagInfo);
            var parameter = JsonSerializer.Deserialize<ParameterGetWhiteListWithEntryInfo>(request.DetectedTag);
            var task = Task.Run(() =>
            {
                return filters.FilterTags(tagInfo);
            });
            await task.ContinueWith(
                    async antecedent =>
                    {
                        if (antecedent.Result)
                        {
                            var reply = await pcs.GetTagInfo(parameter, tagInfo, TagFilterTime);
                            return new TagInfoReply
                            {
                                FetchedTagInfo = reply
                            };
                        }
                        else
                        {
                            return new TagInfoReply
                            {
                                FetchedTagInfo = string.Empty
                            };
                        }
                    }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }

If I put in a

return new TagInfoReply
                                {
                                    FetchedTagInfo = string.Empty
                                };

at the very end and outside the task.ContinueWith, well the error goes away but the problem is when it runs, it doesn't wait for the task to complete. It goes directly to the last return.

CodePudding user response:

At the moment, your return code only returns from the anonymous method you've passed to ContinueWith, and they return to the calling code within ContinueWith. You're not actually returning anything from GetInfo.

The result of ContinueWith is made available to you as its result, so you should be able to rewrite your code like this:

public async override Task<TagInfoReply> GetInfo(TagInfoRequest request, ServerCallContext context)
{
    var tagInfo = JsonSerializer.Deserialize<TagInfo>(request.TagInfo);
    var parameter = JsonSerializer.Deserialize<ParameterGetWhiteListWithEntryInfo>(request.DetectedTag);
    var task = Task.Run(() =>
    {
        return filters.FilterTags(tagInfo);
    });
    return await task.ContinueWith(
            async antecedent =>
            {
                if (antecedent.Result)
                {
                    var reply = await pcs.GetTagInfo(parameter, tagInfo, TagFilterTime);
                    return new TagInfoReply
                    {
                        FetchedTagInfo = reply
                    };
                }
                else
                {
                    return new TagInfoReply
                    {
                        FetchedTagInfo = string.Empty
                    };
                }
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
}
  •  Tags:  
  • c#
  • Related