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

标签 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")));
}

但是我收到错误:

Error CS0311 The type 'Library.LibraryAssetService' cannot be used as type parameter 'TImplementation' in the generic type or method 'ServiceCollectionServiceExtensions.AddScoped(IServiceCollection)'. There is no implicit reference conversion from 'Library.LibraryAssetService' to 'LibraryData.ILibraryAssetService'. Library 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
    {
    }
}

最佳答案

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

尝试使用fully qualified namespaces喜欢

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

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

关于c# - 没有从 'type1' 到 'type2' 的隐式引用转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792804/

相关文章:

javascript - 禁用时无法调整文本区域的大小

tfs - 清除 Visual Studio 2017 中存储的 TFS 凭据

c# - Asp.net razor View - lambda 表达式输入

c# - 如何为Asp.net 控件的链接按钮制作双击和单击事件?

使用 FFMpegConverter 的 C# 视频连接

asp.net - VS安装过程中找不到符合以下参数的产品

c# - 在核心的 odataqueryoptions 中处理选择?

c# - 在数据库中查找随机内容 - Linq

c# - 如何使用环境变量覆盖 Blazor 客户端配置?

c# - 如何使用 .Net (C#/VB) 在 MySQL 中存储/检索 HTML