Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

c# - Error while trying to inject with services.AddScoped

I am building a WarehouseApi, but when I try to inject my service it gives me an error(CS0119)

public static IServiceCollection AddServices(this IServiceCollection services)
        {
            services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
            services.AddScoped(IEmployeeRepository, EmployeeRepository); //CS0119
            return services;
        }

Error descriptions: EmployeeRepository' is a type, which is not valid in the given context and IEmployeeRepository' is a type, which is not valid in the given context

Here are IEmployeeRepository and EmployeeRepository:

public interface IEmployeeRepository : IRepository<Employee>
    {
        Employee GetFirstOrDefaultByFirstName(string firstNameToFind);
        Employee GetFirstOrDefaultByLastName(string lastNameToFind);
        Employee GetFirstOrDefaultbByAge(int ageToFind);
        Employee GetByEmail(string emailToFind);
        Employee GetByAddress(string addressToFind);
        Employee GetByPhoneNumber(string phoneNumberToFind);
    }

public class EmployeeRepository : Repository<Employee>, IEmployeeRepository
    {
        public EmployeeRepository(WareHouseDBContext empContext) : base(empContext) { }

        public Employee GetByAddress(string addressToFind)
        {
            return context.Set<Employee>().FirstOrDefault(x => x.Address == addressToFind);
        }

        public Employee GetByEmail(string emailToFind)
        {
            return context.Set<Employee>().FirstOrDefault(x => x.Email == emailToFind);
        }

        public Employee GetByPhoneNumber(string phoneNumberToFind)
        {
            return context.Set<Employee>().FirstOrDefault(x => x.PhoneNumber == phoneNumberToFind);
        }

        public Employee GetFirstOrDefaultbByAge(int ageToFind)
        {
            return context.Set<Employee>().FirstOrDefault(x => x.Age == ageToFind);
        }

        public Employee GetFirstOrDefaultByFirstName(string firstNameToFind)
        {
            return context.Set<Employee>().FirstOrDefault(x => x.FirstName == firstNameToFind);
        }

        public Employee GetFirstOrDefaultByLastName(string lastNameToFind)
        {
            return context.Set<Employee>().FirstOrDefault(x => x.LastName == lastNameToFind);
        }
    }

I'm new to APIs, Dependency Injection, etc, so this might be an easy fix


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use the method that accepts type arguments to resolve the compiler warning:

services.AddScoped<IEmployeeRepository, EmployeeRepository>();

However if you're not using .NET 5.0 or later, then open generics (IFoo<>) is not supported by the built-in DI container.

In that case you'll need to register it like this:

services.AddScoped<IRepository<Employee>, EmployeeRepository>();

Support for open generics is available from .NET 5.0 (PR). There's some more information about it here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[2] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

56.7k users

...