无法在dotnet核心中实现接口

时间:2017-01-25 18:57:39

标签: c# .net-core

我是dotnet核心的新手,我正在尝试创建一些简单的测试,看它是如何工作的。 我正在创建一个针对框架4.6.1的dotnet核心项目 我的project.json看起来像这样:

{
    "version": "1.0.0-*",
    "buildOptions": {
        "preserveCompilationContext": true,
        "debugType": "portable",
        "emitEntryPoint": true
    },
    "dependencies": {},
    "frameworks": {

        "net461": {
            "dependencies": {

                "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
                "Microsoft.AspNetCore.Mvc": "1.0.0"
            },
            "imports": "dnxcore50"
        }
    }
}

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

我的startup.cs

namespace TestWebCore3
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();
            app.Run(context =>
            {
            return context.Response.WriteAsync("Hello world");
            });
        }
    }
}

我的Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using System.IO;


namespace TestWebCore3
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .Build();

            host.Run();
        }
    }
}

基本上我到目前为止所做的与使用dotnet new,dotnet restore类似,但改为project.json到目标框架4.6.1,当我运行dotnet run或从VS2015运行时它似乎有效

但是,当我尝试创建一个接口,并从接口派生一个类时,我丢失了智能感知,并且项目没有编译。我是dotnet核心的新手,但我不是C#的新手,但我不确定需要包含哪些依赖项来摆脱编译错误

Severity    Code    Description Project File    Line    Suppression State
Error   CS0246  The type or namespace name 'IReceiptRepository' could not be found (are you missing a using directive or an assembly reference?)    TestWebCore3    D:\....\TestWebCore3\src\TestWebCore3\Infrastructure\ReceiptRepository.cs   8

我的收据类是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

    namespace TestWebCore3.Core
    {
        public class Receipt
        {
            public int Id { get; set; }
            public double Amount { get; set; }
            public string Description { get; set; }
            public DateTime TransactionDate { get; set; }
            public string Company { get; set; }
        }
}

我的界面:

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace TestWebCore3.Core.Interface
{
    public interface IReceiptRepository
    {
        IEnumerable<Receipt> List();
        Receipt GetById(int id);
        void Update(Receipt receipt);
    }
}

我的ReceiptRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace TestWebCore3.Infrastructure
{
    public class ReceiptRepository : IReceiptRepository
    {
    }
}

我的项目结构:

enter image description here

我很困惑为什么编译器无法识别我的界面。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

看起来你错过了在你的类文件中包含你的接口命名空间。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TestWebCore3.Core.Interface; //here


namespace TestWebCore3.Infrastructure
{
    public class ReceiptRepository : IReceiptRepository
    {
    }
}