没有从'type1'到'type2'的隐式引用转换

时间:2018-06-11 07:51:47

标签: c# asp.net visual-studio-2017

我尝试做一个网络项目,但面临一个问题。

代码:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddSingleton(Configuration);
    services.AddScoped<ILibraryAssetService, LibraryAssetService>();  
    services.AddDbContext<LibraryContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LibraryConnection")));
}

但是我收到了一个错误:

  

错误CS0311类型'Library.LibraryAssetService'不能在泛型类型或方法'ServiceCollectionServiceExtensions.AddScoped(IServiceCollection)'中用作类型参数'TImplementation'。没有从“Library.LibraryAssetService”到“LibraryData.ILibraryAssetService”的隐式引用转换。库C:\ Users \ Austina \ source \ repos \ Library \ Library \ Startup.cs 29 Active

我到处搜索了它,但没有任何帮助。也许我错过了一些明显的东西?

同样使用类编码:

public class LibraryAssetService : ILibraryAssetService
{
    private LibraryContext _context;
    private int videoId;

    public LibraryAssetService(LibraryContext context)
    {
        _context = context;
    }

    public LibraryAssetService()
    {
    }

    public void Add(LibraryAsset newAsset)
    {
        _context.Add(newAsset);
        _context.SaveChanges();
    }

    public LibraryAsset Get(int id)
    {
        return _context.LibraryAssets
            .Include(a => a.Status)
            .Include(a => a.Location)
            .FirstOrDefault(a => a.Id == id);
    }

    public IEnumerable<LibraryAsset> All => _context.LibraryAssets
            .Include(a => a.Status)
            .Include(a => a.Location);

    public int Id => throw new NotImplementedException();

    public string ImageUrl => throw new NotImplementedException();

    public string Title => throw new NotImplementedException();

    public string GetAuthorOrDirector(int id)
    {
        var isBook = _context.LibraryAssets
            .OfType<Book>().Any(a => a.Id == id);

        var isVideo = _context.LibraryAssets
            .OfType<Video>().Any(video => videoId == id);

        return isBook
            ? GetAuthor(id)
            : GetDirector(id);
    }

    public LibraryAsset GetById(int id)
    {
        return _context.LibraryAssets
            .Include(asset => asset.Status)
            .Include(asset => asset.Location)
            .FirstOrDefault(asset => asset.Id == id);
    }

    public LibraryBranch GetCurrentLocation(int id)
    {
        return _context.LibraryAssets.FirstOrDefault(a => a.Id == id).Location;
    }

    public string GetDeweyIndex(int id)
    {
        if (_context.Books.Any(book => book.Id == id))
        {
            return _context.Books.FirstOrDefault(book => book.Id == id).DeweyIndex;
        }
        else
        {
            return "";
        }
    }

    public string GetIsbn(int id)
    {
        if (_context.Books.Any(a => a.Id == id))
             {
            return _context.Books
                .FirstOrDefault(a => a.Id == id).ISBN;
        }
        else
        {
            return "";
        }
    }
    public LibraryCard GetLibraryCardByAssetId(int id)
    {
        return _context.LibraryCards
            .FirstOrDefault(c => c.Checkouts
                .Select(a => a.LibraryAsset)
                .Select(v => v.Id).Contains(id));
    }

    public string GetTitle(int id)
    {
        return _context.LibraryAssets.First(a => a.Id == id).Title;
    }

    public string GetType(int id)
    {
        var book = _context.LibraryAssets.OfType<Book>()
             .Where(b => b.Id == id);
        return book.Any() ? "Book" : "Video";
    }
    private string GetAuthor(int id)
    {
        var book = (Book)Get(id);
        return book.Author;
    }
    private string GetDirector(int id)
    {
        var video = (Video)Get(id);
        return video.Director;
    }

    public IEnumerable<ILibraryAssetService> GetAll()
    {
        throw new NotImplementedException();
    }

    internal class LibraryContext_context
    {
    }
}

2 个答案:

答案 0 :(得分:0)

这应该是由名称空间冲突引起的,您有两个具有相同名称的类或接口,它们位于不同的名称空间中。

尝试使用fully qualified namespaces之类的

services.AddScoped<LibraryData.ILibraryAssetService, Library.LibraryAssetService>(); 

您需要更正上述完全限定的命名空间,因为Library.LibraryAssetService未实现LibraryData.ILibraryAssetService,正如编译错误所示。

答案 1 :(得分:0)

您没有实现接口。通过此类实现接口:

public class LibraryAssetService: ILibraryAssetService
{
///....
}