There is no implicit reference conversion from 'type1' to 'type2' |
I try to do a web project, but face an issue.
Code:
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"))); } But I get an error :
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
I googled it everywhere, but nothing helped. Maybe I am missing something obvious?
Also code with class:
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 { } } 13 Answers
This should be caused by namespace conflicts, you have two classes or interfaces with the same name that live in separate namespaces.
Try to use fully qualified namespaces like
services.AddScoped<LibraryData.ILibraryAssetService, Library.LibraryAssetService>(); you need to correct the above fully qualified namespaces since Library.LibraryAssetService does not implement LibraryData.ILibraryAssetService, as the compile error suggests.
You are not implementing the interface. Implement the interface by the class like this:
public class LibraryAssetService: ILibraryAssetService { ///.... } My Answer , You have two classes or two interfaces the same name.
services.AddTransient<IDepartMentService,DepartMentService>(); Here!!!
- I have two the same name interface IDepartMentService
- I have two the same name class DepartMentService
Correct :Using namespace for interface and class.
services.AddTransient< ClinicSystem.BOL.IServices.IDepartMentService,ClinicSystem.BLL.Services.DepartMentService>(); Good Luck with my Answer If You fixed your error give my vote .
ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobGlvaWeFcYCOrZ%2BeqpVitrR5zahkoqWgobaktdNmqZ6elaeyr6%2FEZpqopqaav7S1zqdkn6qfonq1xc%2BeaGasn2LBurzEaw%3D%3D