Problem with Razor

Asked

Viewed 69 times

0

I have a problem I haven’t been able to identify what you’re causing. When trying to enter the Log View, an "Inter-Ternal Server Error" occurs and the message appears below.

I am using ASP.NET Core 2.1.1

Invalidoperationexception: Cannot consume scoped service 'Agendaweb.Data.Applicationdbcontext' from Singleton 'Agendaweb.domain.Irepository`1[Agendaweb.domain.Users.User]'.

[Agendaweb.domain.Users.User]

using AgendaWeb.Domain.Adresses;
using System;
using System.Collections.Generic;
using System.Text;

namespace AgendaWeb.Domain.Users
{
    public class User : Entity
    {       
    public string UserName { get; private set; }
    public string FullName { get; private set; }
    public string Cpf { get; private set; }
    public string Email { get; private set; }
    public string Phone { get; private set; }
    public string CellPhone { get; private set; }
    public string PhotoUrl { get; private set; }
    public string Password { get; private set; }
    public DateTime RegistrationDate { get; private set; }
    public DateTime? DateOfBirth { get; private set; }
    public string ProfileType { get; private set; }
    public Address Address { get; private set; }

    public User()
    {

    }     

    public User(string userName, string fullName, string cpf, string email, string phone, string cellPhone, string photoUrl, string password, DateTime registrationDate, DateTime? dateOfBirth, string profileType, Address address)
    {
        ValidateValues(userName, fullName, cpf, email, phone, cellPhone, photoUrl, password, registrationDate, dateOfBirth, profileType, address);
        SetProperties(userName, fullName, cpf, email, phone, cellPhone, photoUrl, password, registrationDate, dateOfBirth, profileType, address);

    }

    public void Update(string userName, string fullName, string cpf, string email, string phone, string cellPhone, string photoUrl, string password, DateTime registrationDate, DateTime? dateOfBirth, string profileType, Address address)
    {
        ValidateValues(userName, fullName, cpf, email, phone, cellPhone, photoUrl, password, registrationDate, dateOfBirth, profileType, address);
        SetProperties(userName, fullName, cpf, email, phone, cellPhone, photoUrl, password, registrationDate, dateOfBirth, profileType, address);

    }

    private void SetProperties(string userName, string fullName, string cpf, string email, string phone, string cellPhone, string photoUrl, string password, DateTime registrationDate, DateTime? dateOfBirth, string profileType, Address address)
    {
        UserName = userName;
        FullName = fullName;
        Cpf = cpf;
        Email = email;
        Phone = phone;
        CellPhone = cellPhone;
        PhotoUrl = photoUrl;
        Password = password;
        RegistrationDate = registrationDate;
        DateOfBirth = dateOfBirth;
        ProfileType = profileType;
        Address = address;
    }

    private static void ValidateValues(string userName, string fullName, string cpf, string email, string phone, string cellPhone, string photoUrl, string password, DateTime registrationDate, DateTime? dateOfBirth, string profileType, Address address)
    {
        DomainException.When(string.IsNullOrEmpty(userName), "O campo nome é obrigatório!");
        DomainException.When(address == null, "O campo endereço é obrigatório!");
    }
}
}

Agendaweb.domain.Irepository

using System;
using System.Collections.Generic;
using System.Text;

namespace AgendaWeb.Domain
{
    public interface IRepository<TEntity>
    {
        TEntity GetById(int id);

        void Save(TEntity entity);
    }
}
  • your context, and your controller ?

  • put your startup class please

1 answer

0


In your Startup class, the Irepository interface must be set to Singleton, switch to Scoped or Transient.

This is because the Applicationdbcontext class is as scoped, ie an instance is created by request, and Irepository is as Singleton, ie an instance for the entire application, and this divergence is what is causing the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.