Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

InvalidOperationException: Unable to resolve service for type for API Repository Please help

$
0
0

 Hi

In my web application has two project BookWeb  and BookAPI . BookWeb is being referenced with BookApi apllication. In BookWeb project the controller name  'AuthorController' is  calling the the API controller  AuthorController action CreateAuthor(). In both project have same controller name and Action name but different name space.

When I use the same action name in both controller the following error is coming 

'InvalidOperationException: Unable to resolve service for type 'BookStoreAPI.Repository.IRepository.IAuthorRepository' while attempting to activate 'BookStoreAPI.Controllers.AuthorsController'.

If I use different actioname in BookWeb  from API controller it will work.

Can I use the same action name  and same controller name in different namespace? Please can you help.

BookAPI
namespace BookStoreAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuthorsController : Controller
    {
         private IAuthorRepository _authorRepository;
        private IBookRepository _bookRepository;
        private ICountryRepository _countryRepository;
        public AuthorsController(IAuthorRepository authorRepository, IBookRepository 
         bookRepository,
            ICountryRepository countryRepository)
        {
            _authorRepository = authorRepository;
            _bookRepository = bookRepository;
            _countryRepository = countryRepository;
        }

       public IActionResult CreateAuthor([FromBody]Author authorToCreate)
        {
            if (authorToCreate == null)
                return BadRequest(ModelState);

            if (!_countryRepository.CountryExists(authorToCreate.CountryId))
            {
                ModelState.AddModelError("", "Country doesn't exist!");
                return StatusCode(404, ModelState);
            }

            authorToCreate.Country = _countryRepository.GetCountry(authorToCreate.CountryId);

            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (!_authorRepository.CreateAuthor(authorToCreate))
            {
                ModelState.AddModelError("", $"Something went wrong saving the author " +$"{authorToCreate.FirstName} {authorToCreate.LastName}");
                return StatusCode(500, ModelState);
            }

            return CreatedAtRoute("GetAuthor", new { authorId = authorToCreate.AuthorId }, authorToCreate);
        }

    }
}

In BookWeb Controller

namespace BookWeb.Controllers
{
    public class AuthorsController : Controller
    {
        IAuthorRepositoryGUI _authorRepository;
        ICountryRepositoryGUI _countryRepository;
        ICategoryRepositoryGUI _categoryRepository;

        public AuthorsController(IAuthorRepositoryGUI authorRepository, ICountryRepositoryGUI countryRepository,
            ICategoryRepositoryGUI categoryRepository)
        {
            _authorRepository = authorRepository;
            _countryRepository = countryRepository;
            _categoryRepository = categoryRepository;
        }

[HttpGet]
        public async Task<IActionResult> CreateAuthor()
        {
            return View();
        }

In startup file

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();
            app.UseStatusCodePages();
            app.UseMvc(routes => {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}"
                    );
            });


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>