Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类未实现接口成员,因为它没有匹配的返回类型_C#_Asp.net_Asp.net Core_Asp.net Web Api - Fatal编程技术网

C# 类未实现接口成员,因为它没有匹配的返回类型

C# 类未实现接口成员,因为它没有匹配的返回类型,c#,asp.net,asp.net-core,asp.net-web-api,C#,Asp.net,Asp.net Core,Asp.net Web Api,为ASP.NET核心Web API应用程序实现服务并获取持续错误 即使我只是实现接口而没有向方法中添加任何附加代码,错误仍然会发生 我已经检查了,再检查了,甚至三次检查了所有的签名,以确保没有打字错误 在查看其他SO线程后,尝试将GetUserInvite函数设置为IEnumerable。还是不走运 接口: using System; using System.Collections.Generic; using System.Threading.Tasks; using API.Data.Dt

为ASP.NET核心Web API应用程序实现服务并获取持续错误

  • 即使我只是实现接口而没有向方法中添加任何附加代码,错误仍然会发生
  • 我已经检查了,再检查了,甚至三次检查了所有的签名,以确保没有打字错误
  • 在查看其他SO线程后,尝试将GetUserInvite函数设置为IEnumerable。还是不走运
  • 接口:

    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using API.Data.Dtos.InviteDto;
    using API.Domain;
    
    namespace API.Services
    {
        public interface IInviteService
        {
            Task<UserInvite> GetUserInvite(Guid sourceUserId, Guid invitedUserId);
            Task<IEnumerable<InviteDto>> GetUserInvites(string predicate, Guid userId);
            Task<User> GetUserWithInvites(Guid userId);
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Threading.Tasks;
    使用API.Data.Dtos.InviteDto;
    使用API.Domain;
    名称空间API.Services
    {
    公共接口IInviteService
    {
    任务GetUserInvite(Guid sourceUserId,Guid invitedUserId);
    任务GetUserInvests(字符串谓词,Guid用户ID);
    任务GetUserWithInvests(Guid用户ID);
    }
    }
    
    服务:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using API.Data;
    using API.Data.Dtos.InviteDto;
    using API.Domain;
    using Microsoft.EntityFrameworkCore;
    
    namespace API.Services
    {
        public class InviteService : IInviteService
        {
            private readonly DataContext _context;
            public InviteService(DataContext context)
            {
                _context = context;
            }
    
            public async Task<UserInvite> GetUserInvite(Guid sourceUserId, Guid invitedUserId)
            {
                return await _context.Invites.FindAsync(sourceUserId, invitedUserId);
            }
    
            public async Task<User> GetUserWithInvites(Guid userId)
            {
                return await _context.Users.Include(x => x.InvitedUsers).FirstOrDefaultAsync(x => x.Id == userId);
            }
    
            public async Task<IEnumerable<InviteDto>> GetUserInvites(string predicate, Guid userId)
            {
                var users = _context.Users.OrderBy(u => u.UserName).AsQueryable();
                var invites = _context.Invites.AsQueryable();
    
                if (predicate == "invited")
                {
                    invites = invites.Where(invite => invite.SourceUserId == userId);
                    users = invites.Select(invite => invite.InvitedUser);
                }
    
                if (predicate == "invitedBy")
                {
                    invites = invites.Where(invite => invite.InvitedUserId == userId);
                    users = invites.Select(invite => invite.SourceUser);
                }
    
                return await users.Select(user => new InviteDto
                {
                    Username = user.UserName,
                    City = user.City
                }).ToListAsync();
            }
    
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    使用System.Threading.Tasks;
    使用API.Data;
    使用API.Data.Dtos.InviteDto;
    使用API.Domain;
    使用Microsoft.EntityFrameworkCore;
    名称空间API.Services
    {
    公共类服务:IInviteService
    {
    私有只读数据上下文_上下文;
    公共服务(DataContext上下文)
    {
    _上下文=上下文;
    }
    公共异步任务GetUserInvite(Guid sourceUserId、Guid invitedUserId)
    {
    return wait_context.Invites.FindAsync(sourceUserId,invitedUserId);
    }
    公共异步任务GetUserWithInvests(Guid用户ID)
    {
    return await _context.Users.Include(x=>x.InvitedUsers.FirstOrDefaultAsync(x=>x.Id==userId);
    }
    公共异步任务GetUserInvests(字符串谓词,Guid用户ID)
    {
    var users=_context.users.OrderBy(u=>u.UserName.AsQueryable();
    var invests=_context.invests.AsQueryable();
    如果(谓词==“邀请”)
    {
    invites=invites.Where(invite=>invite.SourceUserId==userId);
    users=invites.Select(invite=>invite.InvitedUser);
    }
    if(谓词==“invitedBy”)
    {
    invites=invites.Where(invite=>invite.InvitedUserId==userId);
    users=invites.Select(invite=>invite.SourceUser);
    }
    返回等待用户。选择(用户=>newinvitedto
    {
    Username=user.Username,
    城市=用户。城市
    }).ToListAsync();
    }
    }
    }
    
    问题是VS中出现错误。我重新启动并编译了所有内容。感谢所有帮助您的人。

    问题是VS中出现错误。我重新启动并编译了所有内容。感谢所有帮助您的人。

    您的代码返回类型为IEnumerable或UserInvite。因此,从数据库返回的查询与这两种类型中的一种不匹配。与其尝试确保正确写出签名,只要让VS通过从错误中选择该选项来自动生成它们,您就不必担心试图找出区别所在。@jdweng错误在编译之前发生,因此数据库永远不会与之交互。控制台错误说问题也出在GetUserInvite上。@Servy我实际上已经尝试过用VS生成代码(还有抛出异常样板代码),但仍然存在同样的问题。代码在不同的模块中吗?两个模块中的using语句不同。您可以将返回类型编码为IEnumerable或UserInvite。因此,从数据库返回的查询与这两种类型中的一种不匹配。与其尝试确保正确写出签名,只要让VS通过从错误中选择该选项来自动生成它们,您就不必担心试图找出区别所在。@jdweng错误在编译之前发生,因此数据库永远不会与之交互。控制台错误说问题也出在GetUserInvite上。@Servy我实际上已经尝试过用VS生成代码(还有抛出异常样板代码),但仍然存在同样的问题。代码在不同的模块中吗?两个模块中的using语句不同。