相关疑难解决方法(0)

ASP.NET Core Dependency Injection错误:尝试激活时无法解析类型服务

我创建了一个.NET Core MVC应用程序,并使用依赖注入和存储库模式将一个存储库注入我的控制器.但是,我收到一个错误:

InvalidOperationException:尝试激活"WebApplication1.Controllers.BlogController"时,无法解析类型"WebApplication1.Data.BloggerRepository"的服务.

型号(Blog.cs)

namespace WebApplication1.Models
{
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

DbContext(BloggingContext.cs)

using Microsoft.EntityFrameworkCore;
using WebApplication1.Models;

namespace WebApplication1.Data
{
    public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions<BloggingContext> options)
            : base(options)
        { }
        public DbSet<Blog> Blogs { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

存储库(IBloggerRepository.cs和BloggerRepository.cs)

using System;
using System.Collections.Generic;
using WebApplication1.Models;

namespace WebApplication1.Data
{
    internal interface IBloggerRepository : IDisposable
    {
        IEnumerable<Blog> GetBlogs();

        void InsertBlog(Blog blog);

        void …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection asp.net-core-mvc asp.net-core

146
推荐指数
13
解决办法
18万
查看次数

如何在Asp.Net MVC 6中检索AppSettings配置?

假设我使用新的DepencyInjection框架在新的ASP.Net/vNext中配置我的类和依赖项.

我该如何使用,如何获取预定义的配置设置?

    public void ConfigureServices(IServiceCollection services)
    {
        // Add Application settings to the services container.
        services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

        // Add EF services to the services container.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
        // Add Identity services to the services container.
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Configure the options for the authentication middleware.
        // You can add options for Google, Twitter and other middleware as shown below.
        // For more information see http://go.microsoft.com/fwlink/?LinkID=532715
        services.Configure<FacebookAuthenticationOptions>(options =>
        {
            options.AppId = Configuration["Authentication:Facebook:AppId"];
            options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
        });

        services.Configure<MicrosoftAccountAuthenticationOptions>(options …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection appsettings asp.net-core-mvc

7
推荐指数
1
解决办法
2837
查看次数

尝试激活“ TheWorld.Startup”时,无法解析类型为“ Microsoft.Extensions.Configuration.IConfigurationRoot”的服务

似乎我遇到了某种不匹配的参考问题(对不起,我在ASP版本上非常新)。

我使用VS 2016从头开始创建一个ASP.core应用程序。我的启动课程如下:

public class Startup
    {
        private IHostingEnvironment _env;
        private IConfigurationRoot _config;

        public Startup(IHostingEnvironment env, IConfigurationRoot config)
        {
            _env = env;

            var builder = new ConfigurationBuilder()
                .SetBasePath(_env.ContentRootPath)
                .AddJsonFile("config.json");

            _config = builder.Build();
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(_config);

            if(_env.IsEnvironment("Development"))
               services.AddScoped<Services.IMailServices, Services.DebugMailService>();

            services.AddMvc();
        }

        // This method gets called by the …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc

3
推荐指数
3
解决办法
5528
查看次数