Unable to resolve service for type when using my repositoryWhen to use struct?Type Checking: typeof, GetType, or is?Solving error - Unable to resolve service for type 'Serilog.ILogger'An unhandled exception was thrown by the application. System.InvalidOperationException: Unable to > resolve service for typeRegistrations not resolvingCan't connect to remote SQL Server hosted on ubuntu with EFCore 2Asp.Net Core 2.1 Identity - UserStore dependency injection“Exception has been thrown by the target of an invocation” error when injecting Amazon S3 Service in ASP .Net Core constructorUnable to resolve dependency for generic repository using autofac
How to search for Android apps without ads?
Was the Lonely Mountain, where Smaug lived, a volcano?
My players want to use called-shots on Strahd
A flower's head or heart?
Can artificial satellite positions affect tides?
What's the reason for the decade jump in the recent X-Men trilogy?
What did the 8086 (and 8088) do upon encountering an illegal instruction?
Nth term of Van Eck Sequence
In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?
What happens when one target of Paradoxical Outcome becomes illegal?
How can I detect if I'm in a subshell?
Purpose of cylindrical attachments on Power Transmission towers
What is the color associated with lukewarm?
Should I worry about having my credit pulled multiple times while car shopping?
Can you open the door or die? v2
What do I need to do, tax-wise, for a sudden windfall?
Why can't we feel the Earth's revolution?
What is the theme of analysis?
Parsing text written the millitext font
Any gotchas in buying second-hand sanitary ware?
Arrows inside a commutative diagram using tikzcd
usage of mir gefallen
Integrate without expansion?
Can an escape pod land on Earth from orbit and not be immediately detected?
Unable to resolve service for type when using my repository
When to use struct?Type Checking: typeof, GetType, or is?Solving error - Unable to resolve service for type 'Serilog.ILogger'An unhandled exception was thrown by the application. System.InvalidOperationException: Unable to > resolve service for typeRegistrations not resolvingCan't connect to remote SQL Server hosted on ubuntu with EFCore 2Asp.Net Core 2.1 Identity - UserStore dependency injection“Exception has been thrown by the target of an invocation” error when injecting Amazon S3 Service in ASP .Net Core constructorUnable to resolve dependency for generic repository using autofac
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
When I try and access my repository, I receive an error:
InvalidOperationException: Unable to resolve service for type 'software.Notes.Repositories.NoteRepository' while attempting to activate 'software.Notes.Http.Handlers.ShowNote'.
So, I have a simple SoftwareContext:
using Microsoft.EntityFrameworkCore;
using software.Contacts.Entities;
using software.Notes.Entities;
namespace software.Core.Entities
public class SoftwareContext : DbContext
/// <inheritdoc />
/// <summary>
/// Constructor
/// </summary>
public SoftwareContext(DbContextOptions options)
: base(options)
/// <summary>
/// Contact model
/// </summary>
public DbSet<Contact> Contact get; set;
/// <summary>
/// Note model
/// </summary>
public DbSet<Note> Note get; set;
which is instantiated in my startup.cs file:
services.AddDbContext<SoftwareContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
Now, I have a simple request handler to show a note:
using Microsoft.AspNetCore.Mvc;
using software.Notes.Entities;
using software.Notes.Repositories;
namespace software.Notes.Http.Handlers
[ApiController]
public class ShowNote : Controller
/// <summary>
/// Note Repository
/// </summary>
private readonly NoteRepository _note;
/// <summary>
/// ShowNote constructor
/// </summary>
/// <param name="note"></param>
public ShowNote(NoteRepository note)
_note = note;
/// <summary>
/// Get the Note via the ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("note/show/id")]
public IActionResult init(int id)
Note note = _note.Find(id);
if (note != null)
return Ok(note);
return NotFound();
And inside my repository I have the following:
using System;
using System.Collections.Generic;
using System.Linq;
using software.Core.Entities;
using software.Notes.Entities;
using software.Notes.Repositories.Contracts;
namespace software.Notes.Repositories
public abstract class NoteRepository : INoteRepository
/// <summary>
/// Database context
/// </summary>
private readonly SoftwareContext _context;
/// <summary>
/// Bind the database to the repo
/// </summary>
/// <param name="context"></param>
protected NoteRepository(SoftwareContext context)
_context = context;
/// <summary>
/// Create an item from the object
/// </summary>
/// <param name="note"></param>
/// <returns></returns>
public Note Create(Note note)
var add = _context.Note.Add(note);
return note;
/// <summary>
/// Find a note by the id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Note Find(int id)
return _context.Note.FirstOrDefault(x => x.Id.Equals(id));
Shouldn't the Context be injected via the constructor of my repository like I am currently doing? Can someone explain why this isn't working, and what the correct way to do it for it to work would be?
Full exception log:
System.InvalidOperationException: Unable to resolve service for type 'software.Notes.Repositories.NoteRepository' while attempting to activate 'software.Notes.Http.Handlers.ShowNote'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
c# asp.net-mvc asp.net-web-api asp.net-core
add a comment |
When I try and access my repository, I receive an error:
InvalidOperationException: Unable to resolve service for type 'software.Notes.Repositories.NoteRepository' while attempting to activate 'software.Notes.Http.Handlers.ShowNote'.
So, I have a simple SoftwareContext:
using Microsoft.EntityFrameworkCore;
using software.Contacts.Entities;
using software.Notes.Entities;
namespace software.Core.Entities
public class SoftwareContext : DbContext
/// <inheritdoc />
/// <summary>
/// Constructor
/// </summary>
public SoftwareContext(DbContextOptions options)
: base(options)
/// <summary>
/// Contact model
/// </summary>
public DbSet<Contact> Contact get; set;
/// <summary>
/// Note model
/// </summary>
public DbSet<Note> Note get; set;
which is instantiated in my startup.cs file:
services.AddDbContext<SoftwareContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
Now, I have a simple request handler to show a note:
using Microsoft.AspNetCore.Mvc;
using software.Notes.Entities;
using software.Notes.Repositories;
namespace software.Notes.Http.Handlers
[ApiController]
public class ShowNote : Controller
/// <summary>
/// Note Repository
/// </summary>
private readonly NoteRepository _note;
/// <summary>
/// ShowNote constructor
/// </summary>
/// <param name="note"></param>
public ShowNote(NoteRepository note)
_note = note;
/// <summary>
/// Get the Note via the ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("note/show/id")]
public IActionResult init(int id)
Note note = _note.Find(id);
if (note != null)
return Ok(note);
return NotFound();
And inside my repository I have the following:
using System;
using System.Collections.Generic;
using System.Linq;
using software.Core.Entities;
using software.Notes.Entities;
using software.Notes.Repositories.Contracts;
namespace software.Notes.Repositories
public abstract class NoteRepository : INoteRepository
/// <summary>
/// Database context
/// </summary>
private readonly SoftwareContext _context;
/// <summary>
/// Bind the database to the repo
/// </summary>
/// <param name="context"></param>
protected NoteRepository(SoftwareContext context)
_context = context;
/// <summary>
/// Create an item from the object
/// </summary>
/// <param name="note"></param>
/// <returns></returns>
public Note Create(Note note)
var add = _context.Note.Add(note);
return note;
/// <summary>
/// Find a note by the id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Note Find(int id)
return _context.Note.FirstOrDefault(x => x.Id.Equals(id));
Shouldn't the Context be injected via the constructor of my repository like I am currently doing? Can someone explain why this isn't working, and what the correct way to do it for it to work would be?
Full exception log:
System.InvalidOperationException: Unable to resolve service for type 'software.Notes.Repositories.NoteRepository' while attempting to activate 'software.Notes.Http.Handlers.ShowNote'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
c# asp.net-mvc asp.net-web-api asp.net-core
At a guess:SoftwareContext
is being registered asDbContext
only.
– John
Mar 25 at 1:37
Have you registeredNoteRepository
instartup.cs
?
– Simply Ged
Mar 25 at 1:51
The error isn't aboutSoftwareContext
it is about injectingNoteRepository
intoShowNote
constructor.
– Simply Ged
Mar 25 at 1:58
what dependency injection framework did you use?
– Hien Nguyen
Mar 25 at 3:33
add a comment |
When I try and access my repository, I receive an error:
InvalidOperationException: Unable to resolve service for type 'software.Notes.Repositories.NoteRepository' while attempting to activate 'software.Notes.Http.Handlers.ShowNote'.
So, I have a simple SoftwareContext:
using Microsoft.EntityFrameworkCore;
using software.Contacts.Entities;
using software.Notes.Entities;
namespace software.Core.Entities
public class SoftwareContext : DbContext
/// <inheritdoc />
/// <summary>
/// Constructor
/// </summary>
public SoftwareContext(DbContextOptions options)
: base(options)
/// <summary>
/// Contact model
/// </summary>
public DbSet<Contact> Contact get; set;
/// <summary>
/// Note model
/// </summary>
public DbSet<Note> Note get; set;
which is instantiated in my startup.cs file:
services.AddDbContext<SoftwareContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
Now, I have a simple request handler to show a note:
using Microsoft.AspNetCore.Mvc;
using software.Notes.Entities;
using software.Notes.Repositories;
namespace software.Notes.Http.Handlers
[ApiController]
public class ShowNote : Controller
/// <summary>
/// Note Repository
/// </summary>
private readonly NoteRepository _note;
/// <summary>
/// ShowNote constructor
/// </summary>
/// <param name="note"></param>
public ShowNote(NoteRepository note)
_note = note;
/// <summary>
/// Get the Note via the ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("note/show/id")]
public IActionResult init(int id)
Note note = _note.Find(id);
if (note != null)
return Ok(note);
return NotFound();
And inside my repository I have the following:
using System;
using System.Collections.Generic;
using System.Linq;
using software.Core.Entities;
using software.Notes.Entities;
using software.Notes.Repositories.Contracts;
namespace software.Notes.Repositories
public abstract class NoteRepository : INoteRepository
/// <summary>
/// Database context
/// </summary>
private readonly SoftwareContext _context;
/// <summary>
/// Bind the database to the repo
/// </summary>
/// <param name="context"></param>
protected NoteRepository(SoftwareContext context)
_context = context;
/// <summary>
/// Create an item from the object
/// </summary>
/// <param name="note"></param>
/// <returns></returns>
public Note Create(Note note)
var add = _context.Note.Add(note);
return note;
/// <summary>
/// Find a note by the id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Note Find(int id)
return _context.Note.FirstOrDefault(x => x.Id.Equals(id));
Shouldn't the Context be injected via the constructor of my repository like I am currently doing? Can someone explain why this isn't working, and what the correct way to do it for it to work would be?
Full exception log:
System.InvalidOperationException: Unable to resolve service for type 'software.Notes.Repositories.NoteRepository' while attempting to activate 'software.Notes.Http.Handlers.ShowNote'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
c# asp.net-mvc asp.net-web-api asp.net-core
When I try and access my repository, I receive an error:
InvalidOperationException: Unable to resolve service for type 'software.Notes.Repositories.NoteRepository' while attempting to activate 'software.Notes.Http.Handlers.ShowNote'.
So, I have a simple SoftwareContext:
using Microsoft.EntityFrameworkCore;
using software.Contacts.Entities;
using software.Notes.Entities;
namespace software.Core.Entities
public class SoftwareContext : DbContext
/// <inheritdoc />
/// <summary>
/// Constructor
/// </summary>
public SoftwareContext(DbContextOptions options)
: base(options)
/// <summary>
/// Contact model
/// </summary>
public DbSet<Contact> Contact get; set;
/// <summary>
/// Note model
/// </summary>
public DbSet<Note> Note get; set;
which is instantiated in my startup.cs file:
services.AddDbContext<SoftwareContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
Now, I have a simple request handler to show a note:
using Microsoft.AspNetCore.Mvc;
using software.Notes.Entities;
using software.Notes.Repositories;
namespace software.Notes.Http.Handlers
[ApiController]
public class ShowNote : Controller
/// <summary>
/// Note Repository
/// </summary>
private readonly NoteRepository _note;
/// <summary>
/// ShowNote constructor
/// </summary>
/// <param name="note"></param>
public ShowNote(NoteRepository note)
_note = note;
/// <summary>
/// Get the Note via the ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("note/show/id")]
public IActionResult init(int id)
Note note = _note.Find(id);
if (note != null)
return Ok(note);
return NotFound();
And inside my repository I have the following:
using System;
using System.Collections.Generic;
using System.Linq;
using software.Core.Entities;
using software.Notes.Entities;
using software.Notes.Repositories.Contracts;
namespace software.Notes.Repositories
public abstract class NoteRepository : INoteRepository
/// <summary>
/// Database context
/// </summary>
private readonly SoftwareContext _context;
/// <summary>
/// Bind the database to the repo
/// </summary>
/// <param name="context"></param>
protected NoteRepository(SoftwareContext context)
_context = context;
/// <summary>
/// Create an item from the object
/// </summary>
/// <param name="note"></param>
/// <returns></returns>
public Note Create(Note note)
var add = _context.Note.Add(note);
return note;
/// <summary>
/// Find a note by the id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Note Find(int id)
return _context.Note.FirstOrDefault(x => x.Id.Equals(id));
Shouldn't the Context be injected via the constructor of my repository like I am currently doing? Can someone explain why this isn't working, and what the correct way to do it for it to work would be?
Full exception log:
System.InvalidOperationException: Unable to resolve service for type 'software.Notes.Repositories.NoteRepository' while attempting to activate 'software.Notes.Http.Handlers.ShowNote'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
c# asp.net-mvc asp.net-web-api asp.net-core
c# asp.net-mvc asp.net-web-api asp.net-core
asked Mar 25 at 1:17
DumbAnswersForDumbQuestionsDumbAnswersForDumbQuestions
205
205
At a guess:SoftwareContext
is being registered asDbContext
only.
– John
Mar 25 at 1:37
Have you registeredNoteRepository
instartup.cs
?
– Simply Ged
Mar 25 at 1:51
The error isn't aboutSoftwareContext
it is about injectingNoteRepository
intoShowNote
constructor.
– Simply Ged
Mar 25 at 1:58
what dependency injection framework did you use?
– Hien Nguyen
Mar 25 at 3:33
add a comment |
At a guess:SoftwareContext
is being registered asDbContext
only.
– John
Mar 25 at 1:37
Have you registeredNoteRepository
instartup.cs
?
– Simply Ged
Mar 25 at 1:51
The error isn't aboutSoftwareContext
it is about injectingNoteRepository
intoShowNote
constructor.
– Simply Ged
Mar 25 at 1:58
what dependency injection framework did you use?
– Hien Nguyen
Mar 25 at 3:33
At a guess:
SoftwareContext
is being registered as DbContext
only.– John
Mar 25 at 1:37
At a guess:
SoftwareContext
is being registered as DbContext
only.– John
Mar 25 at 1:37
Have you registered
NoteRepository
in startup.cs
?– Simply Ged
Mar 25 at 1:51
Have you registered
NoteRepository
in startup.cs
?– Simply Ged
Mar 25 at 1:51
The error isn't about
SoftwareContext
it is about injecting NoteRepository
into ShowNote
constructor.– Simply Ged
Mar 25 at 1:58
The error isn't about
SoftwareContext
it is about injecting NoteRepository
into ShowNote
constructor.– Simply Ged
Mar 25 at 1:58
what dependency injection framework did you use?
– Hien Nguyen
Mar 25 at 3:33
what dependency injection framework did you use?
– Hien Nguyen
Mar 25 at 3:33
add a comment |
2 Answers
2
active
oldest
votes
You will have to add your NoteRepository inside the .NET Core’s IOC container. There are three ways to do that:
in the startup.cs class add
services.AddScoped<INoteRepository, NoteRepository>();
which will create instance of NoteRepository class once per request
services.AddSingleton <INoteRepository, NoteRepository>();
which mean instance of your NoteRepository class will be shared across requests
services.AddTransient <INoteRepository, NoteRepository>();
which will create the instance each time application request it.
then you can inject the dependency through constructor of your controller
[ApiController]
public class ShowNote : Controller
private readonly INoteRepository _note;
public ShowNote(INoteRepository note)
_note = note;
add a comment |
maybe you need to pass the context to the dbcontextoptions in the constructor? So try change this:
public SoftwareContext(DbContextOptions options)
: base(options)
to this:
public SoftwareContext(DbContextOptions<SoftwareContext> options)
: base(options)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330179%2funable-to-resolve-service-for-type-when-using-my-repository%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You will have to add your NoteRepository inside the .NET Core’s IOC container. There are three ways to do that:
in the startup.cs class add
services.AddScoped<INoteRepository, NoteRepository>();
which will create instance of NoteRepository class once per request
services.AddSingleton <INoteRepository, NoteRepository>();
which mean instance of your NoteRepository class will be shared across requests
services.AddTransient <INoteRepository, NoteRepository>();
which will create the instance each time application request it.
then you can inject the dependency through constructor of your controller
[ApiController]
public class ShowNote : Controller
private readonly INoteRepository _note;
public ShowNote(INoteRepository note)
_note = note;
add a comment |
You will have to add your NoteRepository inside the .NET Core’s IOC container. There are three ways to do that:
in the startup.cs class add
services.AddScoped<INoteRepository, NoteRepository>();
which will create instance of NoteRepository class once per request
services.AddSingleton <INoteRepository, NoteRepository>();
which mean instance of your NoteRepository class will be shared across requests
services.AddTransient <INoteRepository, NoteRepository>();
which will create the instance each time application request it.
then you can inject the dependency through constructor of your controller
[ApiController]
public class ShowNote : Controller
private readonly INoteRepository _note;
public ShowNote(INoteRepository note)
_note = note;
add a comment |
You will have to add your NoteRepository inside the .NET Core’s IOC container. There are three ways to do that:
in the startup.cs class add
services.AddScoped<INoteRepository, NoteRepository>();
which will create instance of NoteRepository class once per request
services.AddSingleton <INoteRepository, NoteRepository>();
which mean instance of your NoteRepository class will be shared across requests
services.AddTransient <INoteRepository, NoteRepository>();
which will create the instance each time application request it.
then you can inject the dependency through constructor of your controller
[ApiController]
public class ShowNote : Controller
private readonly INoteRepository _note;
public ShowNote(INoteRepository note)
_note = note;
You will have to add your NoteRepository inside the .NET Core’s IOC container. There are three ways to do that:
in the startup.cs class add
services.AddScoped<INoteRepository, NoteRepository>();
which will create instance of NoteRepository class once per request
services.AddSingleton <INoteRepository, NoteRepository>();
which mean instance of your NoteRepository class will be shared across requests
services.AddTransient <INoteRepository, NoteRepository>();
which will create the instance each time application request it.
then you can inject the dependency through constructor of your controller
[ApiController]
public class ShowNote : Controller
private readonly INoteRepository _note;
public ShowNote(INoteRepository note)
_note = note;
edited Mar 25 at 4:29
answered Mar 25 at 4:15
Muhammad AliMuhammad Ali
8317
8317
add a comment |
add a comment |
maybe you need to pass the context to the dbcontextoptions in the constructor? So try change this:
public SoftwareContext(DbContextOptions options)
: base(options)
to this:
public SoftwareContext(DbContextOptions<SoftwareContext> options)
: base(options)
add a comment |
maybe you need to pass the context to the dbcontextoptions in the constructor? So try change this:
public SoftwareContext(DbContextOptions options)
: base(options)
to this:
public SoftwareContext(DbContextOptions<SoftwareContext> options)
: base(options)
add a comment |
maybe you need to pass the context to the dbcontextoptions in the constructor? So try change this:
public SoftwareContext(DbContextOptions options)
: base(options)
to this:
public SoftwareContext(DbContextOptions<SoftwareContext> options)
: base(options)
maybe you need to pass the context to the dbcontextoptions in the constructor? So try change this:
public SoftwareContext(DbContextOptions options)
: base(options)
to this:
public SoftwareContext(DbContextOptions<SoftwareContext> options)
: base(options)
answered Mar 25 at 8:35
Johan HerstadJohan Herstad
489412
489412
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330179%2funable-to-resolve-service-for-type-when-using-my-repository%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
At a guess:
SoftwareContext
is being registered asDbContext
only.– John
Mar 25 at 1:37
Have you registered
NoteRepository
instartup.cs
?– Simply Ged
Mar 25 at 1:51
The error isn't about
SoftwareContext
it is about injectingNoteRepository
intoShowNote
constructor.– Simply Ged
Mar 25 at 1:58
what dependency injection framework did you use?
– Hien Nguyen
Mar 25 at 3:33