ASP.NET UserService方法未被标识为支持I UserService

chhkpiq4  于 2023-03-04  发布在  .NET
关注(0)|答案(1)|浏览(98)

这里我使用

[HttpPost]
        public async Task<IActionResult> Login(LoginRequest request)
        {
            var user = await _userService.GetUserByEmail(request.Email);

GetUserByEmail方法无法识别是否明确存在depsite

public async Task<User> GetUserByEmail(string email)
        {
            return await _context.Users.FirstOrDefaultAsync(u => u.Email == email);
        }

我试着用微软的建议修复它,出于某种原因,它试图在IUserService中构建方法,提醒你,_userService是UserService的一个示例我该如何修复这个问题?谢谢
编辑,为了准确这里是完整的错误脚本

Severity  Code    Description Project File    Line    Suppression State
Error   CS1061  'UserService' does not contain a definition for 'GetUserByEmail' and no accessible extension method 'GetUserByEmail' accepting a first argument of type 'UserService' could be found (are you missing a using directive or an assembly reference?)  LuxusCoffee2.0  C:\Users\User\source\repos\LuxusCoffee2.0\Controllers\LoginController.cs    25  Active

每次请求编辑2个

namespace LuxusCoffee2._0.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly UserService _userService;

        public LoginController(UserService userService)
        {
            _userService = userService;
        }

        [HttpPost]
        public async Task<IActionResult> Login(LoginRequest request)
        {
            var user = await _userService.GetUserByEmail(request.Email);
            if (user == null)
            {
                return BadRequest("Email or password is incorrect.");
            }
py49o6xq

py49o6xq1#

因此,我在IUserService.cs中是哑的,似乎我已经定义了一个从IUserService继承的UserService,它无法识别UserService中的方法的原因是因为它使用了IUserService中的UserService
IUserService.cs

using LuxusCoffee2._0.Models;
using Microsoft.EntityFrameworkCore;

public interface IUserService
{
    Task<IEnumerable<User>> GetAllUsers();
    Task<User> GetUserById(Guid id);
    Task AddUser(User user);
    Task UpdateUser(User user);
    Task DeleteUser(Guid id);

    Task<bool> UserExists(Guid id);
}

public class UserService : IUserService
{
    private readonly LuxusCoffeeDbContext _dbContext;

    public UserService(LuxusCoffeeDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task<IEnumerable<User>> GetAllUsers()
    {
        return await _dbContext.Users.ToListAsync();
    }

    public async Task<User> GetUserById(Guid id)
    {
        return await _dbContext.Users.FindAsync(id);
    }

    public async Task AddUser(User user)
    {
        _dbContext.Users.Add(user);
        await _dbContext.SaveChangesAsync();
    }

    public async Task UpdateUser(User user)
    {
        _dbContext.Users.Update(user);
        await _dbContext.SaveChangesAsync();
    }

    public async Task DeleteUser(Guid id)
    {
        var user = await _dbContext.Users.FindAsync(id);
        if (user != null)
        {
            _dbContext.Users.Remove(user);
            await _dbContext.SaveChangesAsync();
        }
    }
    public async Task<bool> UserExists(Guid id) // Implement the method in the service class
    {
        return await _dbContext.Users.AnyAsync(e => e.guid == id);
    }
}

UserService.cs

using System.Collections.Generic;
using System.Linq;
using LuxusCoffee2._0.Models;
using Microsoft.EntityFrameworkCore;

namespace LuxusCoffee2._0.Services
{
    public class UserService
    {
        private readonly LuxusCoffeeDbContext _context;

        public UserService(LuxusCoffeeDbContext context)
        {
            _context = context;
        }

        public List<User> GetAllUsers()
        {
            return _context.Users.ToList();
        }

        public User GetUserById(int id)
        {
            return _context.Users.Find(id);
        }

        public void AddUser(User user)
        {
            _context.Users.Add(user);
            _context.SaveChanges();
        }
        public async Task<User> GetUserByEmail(string email)
        {
            return await _context.Users.FirstOrDefaultAsync(u => u.Email == email);
        }


        public void UpdateUser(User user)
        {
            _context.Users.Update(user);
            _context.SaveChanges();
        }

        public void DeleteUser(int id)
        {
            var user = _context.Users.Find(id);
            _context.Users.Remove(user);
            _context.SaveChanges();
        }

    }
}

我通过删除UserService.cs并将缺少的方法添加到IUserService.cs中的UserService来修复它
IUserService.cs

using System.Collections.Generic;
using System.Threading.Tasks;
using LuxusCoffee2._0.Models;
using Microsoft.EntityFrameworkCore;

namespace LuxusCoffee2._0.Services
{
    public interface IUserService
    {
        Task<IEnumerable<User>> GetAllUsers();
        Task<User> GetUserById(Guid id);
        Task AddUser(User user);
        Task UpdateUser(User user);
        Task DeleteUser(Guid id);

        Task<bool> UserExists(Guid id);

        Task<User> GetUserByEmail(string email);
    }

    public class UserService : IUserService
    {
        private readonly LuxusCoffeeDbContext _dbContext;

        public UserService(LuxusCoffeeDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task<IEnumerable<User>> GetAllUsers()
        {
            return await _dbContext.Users.ToListAsync();
        }

        public async Task<User> GetUserById(Guid id)
        {
            return await _dbContext.Users.FindAsync(id);
        }

        public async Task AddUser(User user)
        {
            _dbContext.Users.Add(user);
            await _dbContext.SaveChangesAsync();
        }

        public async Task UpdateUser(User user)
        {
            _dbContext.Users.Update(user);
            await _dbContext.SaveChangesAsync();
        }

        public async Task DeleteUser(Guid id)
        {
            var user = await _dbContext.Users.FindAsync(id);
            if (user != null)
            {
                _dbContext.Users.Remove(user);
                await _dbContext.SaveChangesAsync();
            }
        }

        public async Task<bool> UserExists(Guid id)
        {
            return await _dbContext.Users.AnyAsync(e => e.guid == id);
        }

        public async Task<User> GetUserByEmail(string email)
        {
            return await _dbContext.Users.FirstOrDefaultAsync(u => u.Email == email);
        }
    }
}

我是哑巴,你很聪明,泰

相关问题