I am building a throwaway application in MVC 6 and experimenting with different architectures for dependencies.
The problem I am facing is how to create a custom '
' object specific to the Application. This would require some information from the
and some information from the database, and will be a request-scoped repository for application specific attributes. I want to pass the instance of the
into the constructor of the '
I have successfully created a '
' object with an
interface using DI and this works Ok.
The difference with the 'MyAppContext' class is that it has two parameters in the constructor - the '
' and the
. Here is the MyAppContext class:
public class MyAppContext : IMyAppContext
public MyAppContext(IDataService dataService, HttpContext httpContext)
//do stuff here with the httpContext
In the startup code, I register the DataService instance and the MyAppContext instance:
public void ConfigureServices(IServiceCollection services)
//adds a singleton instance of the DataService using DI
public void Configure(IApplicationBuilder app)
app.UseMvc(routes => /* routes stuff */);
I am expecting the
parameter in the constructor to get resolved by DI.
When running the code, this is the exception I get returned:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNet.Http.HttpContext' while attempting to activate 'MyAppContext'
I figure this is because there is no specific instance of
that this error is occurring, but I don't know how to register the
instance in DI. I added the line '
' but this hasn't made any difference. I also tried a variant of:
But this fails because the second
is supposed to be an instance - I know it's not correct but haven't been able to work out what is.
So, in summary - how can I pass in the
object into the constructor of MyAppContext?