Voj*_*vic 13 c# asp.net-core-mvc asp.net-core
我有一个Repository.cs包含接口的文件及其实现如下:
public interface IRepository
{
IEnumerable<City> Cities { get; }
void AddCity(City newCity);
}
public class MemoryRepository : IRepository
{
private List<City> cities = new List<City> {
new City { Name = "London", Country = "UK", Population = 8539000},
new City { Name = "New York", Country = "USA", Population = 8406000 },
new City { Name = "San Jose", Country = "USA", Population = 998537 },
new City { Name = "Paris", Country = "France", Population = 2244000 }
};
public IEnumerable<City> Cities => cities;
public void AddCity(City newCity)
{
cities.Add(newCity);
}
}
Run Code Online (Sandbox Code Playgroud)
而在HomeController我试图将Cities吸气剂传递给视图:
public class HomeController : Controller
{
private IRepository repository;
public HomeController(IRepository repo)
{
repository = repo;
}
public IActionResult Index()
{
return View(repository.Cities);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到此错误:
InvalidOperationException:尝试激活"Cities.Controllers.HomeController"时无法解析类型"Cities.Models.IRepository"的服务.
Kir*_*kin 22
您需要注册IRepository到依赖注入框架.例如,ConfigureServices您可以添加:
services.AddScoped<IRepository, MemoryRepository>();
Run Code Online (Sandbox Code Playgroud)
有关ASP.NET Core中的依赖注入的更多信息,请参见此处.
AddScoped 只是一个例子,但是更常见的方法之一:
每个请求创建一次范围生命周期服务.
Tom*_*kel 22
其他答案是正确的,但是我正在启动一个新的 asp.net core 2.1.x 项目并收到此错误。
最终是我的错字。
所以在我的控制器中而不是像这样正确使用界面
public HomeController(IApplicationRepository applicationRepository)
{
_applicationRepository = applicationRepository;
}
Run Code Online (Sandbox Code Playgroud)
我的拼写错误让我使用了下面ApplicationRepository的界面通知,而不是它的界面IApplicationRepository
,因此没有错误发现丢失的“I”很有趣:/
public HomeController(IApplicationRepository applicationRepository)
{
_applicationRepository = applicationRepository;
}
Run Code Online (Sandbox Code Playgroud)
因此控制器无法解析 DI...
需要将这样的方法添加到您的Startup:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//...
// Add application services.
services.AddTransient<IRepository, MemoryRepository>();
//..
}
Run Code Online (Sandbox Code Playgroud)
服务应在使用前注册。
更新:
如果您不想在应用程序上使用 DI,只需MemoryRepository在 的构造函数上创建 DI 实例HomeController,如下所示:
public class HomeController : Controller
{
private IRepository repository;
public HomeController()
{
repository = new MemoryRepository();
}
public IActionResult Index()
{
return View(repository.Cities);
}
}
Run Code Online (Sandbox Code Playgroud)
我们在实体框架核心数据库的第一种方法中遇到此错误。我按照以下步骤操作,错误得到解决enter code here
步骤1:检查您的上下文类构造函数应如下所示
public partial class ZPHSContext : DbContext
{
public ZPHSContext(DbContextOptions<ZPHSContext> dbContextOptions):base(dbContextOptions)
{
}
}
Run Code Online (Sandbox Code Playgroud)
步骤2:在启动文件中
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<ZPHSContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}
Run Code Online (Sandbox Code Playgroud)
步骤3:appsettings中的连接字符串
"ConnectionStrings": {
"BloggingDatabase": "Server=Server=****;Database=ZPHSS;Trusted_Connection=True;"
}
Run Code Online (Sandbox Code Playgroud)
步骤4:在上下文类的OnConfiguring方法中删除默认代码
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35361 次 |
| 最近记录: |