ASP.NET Core 1.0 프로젝트에서 DI를 사용하여 어떻게 생성자에 매개 변수를 전달할 수 있습니까? 예를 들어, Startup.cs에서 다음 서비스를 어떻게 등록합니까? StateService()에 BlogingContext 유형의 입력 매개 변수가 필요하므로 services.AddTransient(typeof(IStateService), new StateService());
이 작동하지 않습니다. 또는 데이터베이스가 포함 된 다음 서비스를 작성하는 대체 방법이 있습니까? 여기 State
은 SQL Server Db에서 오는 테이블입니다. 앱에서 코드 첫 번째 접근 방식을 사용하여 EntityFrameworkCore
을 사용하고 있습니다. 저는 2016 년 6 월 27 일에 릴리스 된 ASP.NET Core 1.0 및 VS2015-Update 3의 최신 버전을 사용하고 있습니다.ASP.NET 핵심 종속성 주입, 매개 변수로 삽입
similar example here을 볼 수 있지만 입력 매개 변수 유형은 동일하지 않습니다.
서비스는 : 문서의 상태로
public interface IStateService
{
IEnumerable<State> List();
}
public class StateService : IStateService
{
private BloggingContext _context;
public StateService(BloggingContext context)
{
_context = context;
}
public IEnumerable<State> List()
{
return _context.States.ToList();
}
}
'BloggingContext'를 시작할 때 services.AddDbContext();와 같이 등록하려고 했습니까? https://docs.asp.net/ko/latest/fundamentals/dependency-injection.html#registering-your-own-services –
mollwe
@mollwe 도움을 주셔서 감사합니다. 귀하의 코드가 내 맞춤 서비스, 즉'StateService'를 등록하지 않습니다. – nam
상태 서비스를'service.AddTransient();'로 등록하면 작동합니까? –
mollwe