0

Here is the error I am receiving when I try to execute an insert into my SQL database on .NET:

System.InvalidOperationException: Unable to resolve service for type 'MotionPicturesCore.Interfaces.IMotionPictureService' while attempting to activate 'MotionPicturesCore.Controllers.MotionPictureApiControllerV2'.

at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method55(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass7_0.b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()

I am not sure why I am getting this error aside from the fact that I believe it may have to do with how I have set up my dependency injection. Eventually, this will be hooked up to a simple Vue.js application but for right now, this API I have built is shooting back this error at me.

Here are snippets from what I believe to be where my error in setting this up may be but again, I am not sure. I don't want to post whole blocks of code for anyone to sift through but if someone can point me in the right direction, it would be more than appreciated:

namespace MotionPicturesCore.Interfaces
{
    public interface IMotionPictureService
    {
        int AddMotionPicture(MotionPictureAddRequest model);
        void UpdateMotionPicture(MotionPictureUpdateRequest model);
        MotionPicture GetSingleMotionPicture(int id);
        List<MotionPicture> GetAllMotionPictures();
        void DeleteMotionPicture(int id);
    }
}

namespace MotionPicturesCore.StartUp
{
    public class DependencyInjection
    {
        public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            if (configuration is IConfigurationRoot)
            {
                services.AddSingleton<IConfigurationRoot>(configuration as IConfigurationRoot);  
            }

            services.AddSingleton<IConfiguration>(configuration); 

            string connString = configuration.GetConnectionString("Default");

            services.AddSingleton<IDataProvider, SqlDataProvider>(delegate (IServiceProvider provider)
            {
                return new SqlDataProvider(connString);
            });

            services.AddSingleton<IMotionPictureService, IMotionPictureService>();

            GetAllEntities().ForEach(tt =>
            {
                IConfigureDependencyInjection idi = Activator.CreateInstance(tt) as IConfigureDependencyInjection;
                idi.ConfigureServices(services, configuration);
            });
        }

        public static List<Type> GetAllEntities()
        {
            return AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
                 .Where(x => typeof(IConfigureDependencyInjection).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
                 .ToList();
        }

        public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
        }
    }
}

4 Answers 4

0

In your DI configuration, you setup up IMotionPictureService to use an instance of IMotionPictureService - notice the I that marks the interface:

services.AddSingleton<IMotionPictureService, IMotionPictureService>(); 

The configuration usually is like this:

services.AddSingleton<IMotionPictureService, MotionPictureService>(); 

This way, you bind the implementation MotionPictureService to the interface IMotionPictureService.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow I can't believe I missed that. Thank you... ..however I still get an error. It looks to be mostly the same message. What's another error I should be looking at?
Can you debug the ConfigureServices method? If you put a breakpoint on the line services.AddSingleton<IMotionPictureService, MotionPictureService>(); is it hit? Does the class MotionPictureService have any constructor parameters? Can DI resolve these parameters?
0

i think i found ur error.

this line services.AddSingleton<IMotionPictureService, IMotionPictureService>();

should be something like services.AddSingleton<IMotionPictureService, MotionPictureService>();

You are binding your interface to same interface. U need to bind it to a class, like MotionPictureService for exemple.

2 Comments

Wow I can't believe I missed that. Thank you... ..however I still get an error. It looks to be mostly the same message. What's another error I should be looking at?
@sleepytime_mcgee please look at my answer
0

You have to create a class to use as implementation, you can't pass for both type arguments of AddSingleton IMotionPictureService.

You need to create a class MotionPictureService which inherits from the interface IMotionPictureService and then you can register your singleton as such:

services.AddSingleton<IMotionPictureService, MotionPictureService>();

1 Comment

Wow I can't believe I missed that. Thank you... ..however I still get an error. It looks to be mostly the same message. What's another error I should be looking at?
0

Your startup class should return IServiceProvider in order to pass your registrations to controllers

public IServiceProvider ConfigureServices(IServiceCollection services)
{
  // setup
  services.AddSingleton<IMotionPictureService, MotionPictureService>();

  // other parts maybe called like that in your code
  DependencyInjection.ConfigureServices(services);

  return services.BuildServiceProvider();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.