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)
{
}
}
}
CodePudding user response:
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
.
CodePudding user response:
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.
CodePudding user response:
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>();