简体   繁体   English

如何解决此错误:并非所有代码路径都返回值

[英]How can I fix this Error: not all code paths return a value

My project has 2 part ClientSide and Server side.我的项目有两部分客户端和服务器端。 In Server side I have Controller that it needs query and command.在服务器端我有 Controller 它需要查询和命令。 I put both of them and my command has a handler but after I'd done my handler it throws a error that say: not all code paths return a value.我把它们都放了,我的命令有一个处理程序,但是在我完成我的处理程序之后它会抛出一个错误,说:并非所有代码路径都返回一个值。 this is my handler:这是我的处理程序:

     public Task<ReturnCreateDataQuery> Handle(CreateCompletedActCommand request, CancellationToken cancellationToken)
    {
        var party = _dbContext.Parties.SingleOrDefault(t => t.PartyCode == request.PartyCode);
        if (party == null) throw new Exception("");

        if ((bool)request.ResonableType)
        {

            var departmentText = request.DepartmentIds.Any() ? string.Join(",", request.DepartmentIds.Distinct().OrderBy(t => t)) : string.Empty;
            var cartableActs = new List<CartableActModel>();

            var cartable = new CartableStateModel()
            {

                ClaimId = request.ClaimId,
                CreationDate = DateTime.Now,
                StatusCode = (int)Enumeration.StateType.Compeleted,
                PreviouseCartableStateId = request.CartableStateId
            };

            var cartableAct = new CartableActCompleteModel()
            {
                ActCode = (int)Enumeration.ActType.CompleteCustomerData,
                ActorId = party.PartyId,
                CartableStateId = cartable.CartableStateId,
                ChangeDate = DateTime.Now,
                ClaimSubjectId = request.ClaimSubjectId,
                ClaimType = request.ClaimType,
                Departments = departmentText,
                ExpertPartyId = request.ExpertPartyId,
                ResonableType = request.ResonableType,
                SubClaimSubjectId = request.SubClaimSubjectId,
                CompletedDescription = request.CompletedDescription,
            };

            var attachments = request.Attachments.Select(t => new ActAttachmentModel
            {
                AttachmentContent = t.AttachmentContent,
                ActAttachmentId = cartableAct.CartableActId,
                ActId = cartableAct.CartableActId,
                CreationDate = DateTime.Now,
                Creator = party.PartyId,
                FileExtension = t.FileExtension,
                Title = t.Title,
                MimeType = t.MimeType
            }).ToList();

            cartableAct.ActAttachments = attachments;

            cartableActs.Add(cartableAct);

            cartable.CartableActs = cartableActs;

            _dbContext.Cartables.Add(cartable); 
            _dbContext.SaveChangesAsync(cancellationToken);
        }
        else
        {
            var cartableActs = new List<CartableActModel>();
            var cartable = new CartableStateModel()
            {
                ClaimId = request.ClaimId,
                CreationDate = DateTime.Now,
                StatusCode = (int)Enumeration.StateType.Finished,

            };

            var cartableAct = new CartableActSatisficationModel()
            {
                ActCode = (int)Enumeration.ActType.SatisficationCustomer,
                ActorId = party.PartyId,
                CartableStateId = cartable.CartableStateId,
                ChangeDate = DateTime.Now,
                IsSatisfy = false,
                SatisfyLevel = "1",
            };

            var attachments = request.Attachments.Select(t => new ActAttachmentModel
            {
                AttachmentContent = t.AttachmentContent,
                ActAttachmentId = cartableAct.CartableActId,
                ActId = cartableAct.CartableActId,
                CreationDate = DateTime.Now,
                Creator = party.PartyId,
                FileExtension = t.FileExtension,
                Title = t.Title,
                MimeType = t.MimeType
            }).ToList();

            var outBox = new OutBoxModel
            {
                SentType = "SMS",
                ClaimId = request.ClaimId,
                IsSent = false,
                PartyCode = request.PartyCode,
                IsCustomer = true

            };

            cartableAct.ActAttachments = attachments;

            cartableActs.Add(cartableAct);

            cartable.CartableActs = cartableActs;

            _dbContext.Cartables.Add(cartable);
            _dbContext.OutBoxes.Add(outBox);
            _dbContext.SaveChangesAsync(cancellationToken);
        }
    }

I don't know how can I fix this error I search a lot of source but I can't understand which value should return if you know this I would thank you.我不知道如何解决这个错误我搜索了很多来源但我不明白如果你知道应该返回哪个值我会感谢你的。

Change your metod header更改您的方法 header

 public async Task Handle(CreateCompletedActCommand request, CancellationToken cancellationToken)
    {
....your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM