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)
{
}
}
}