Custom Data Provider setup through Dependency Injection
See original GitHub issueHi, I’m trying to resolve an issue I currently have with my solution. I’m trying to combine WebApi and EntityFramework extensions.
Namely, I’m trying to insert my DbContext object into the CustomDataProvider I have. Now, I’ve already tried the solutions told in the issue #194 , but none of them worked.
Here’s what my constructor needs in my CustomDataProvider class:
public CustomDataProvider(IMapper mapper, ApplicationDbContext context, EntityFrameworkDataProvider efDataProvider)
{
EFDataProvider = efDataProvider;
Context = context;
Mapper = mapper;
}
And here’s the current problem I’m facing (in Startup.cs):
public void Configure(...)
{
EntityFrameworkDataProvider efdp = new EntityFrameworkDataProvider(...);
Audit.Core.Configuration.DataProvider = new CustomDataProvider(serviceProvider.GetRequiredService<IMapper>(),
serviceProvider.GetRequiredService<ApplicationDbContext>(), efdp);
}
P.S. I need the DbContext instance inside the CustomDataProvider so I could insert custom data models into my database.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Custom Data Providers and Dependency Injection #194
I'm working on a custom provider that just publishes the AuditEvent further, using IMediatr, to be processed. So I need to inject IMediatr...
Read more >Implement a custom configuration provider - .NET
Learn how to implement a custom configuration provider in .NET apps. ... DependencyInjection; using Microsoft.Extensions.
Read more >Dependency Injection with Custom Membership Provider
I have an ASP.NET MVC web application that implements a custom membership provider. The custom membership provider takes a UserRepository to its ...
Read more >Asp.Net Core: Injecting custom data/classes into startup ...
I've been working with Asp.Net core and recently came across an issue where I ... custom data/classes into startup classes' constructor and configure...
Read more >Custom Data provider example - Xperience 13 Documentation
Adding a custom data provider assembly (obsolete) · Right-click the solution in the Solution Explorer and select Manage NuGet Packages for ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is what worked finally, using DataProviderFactory and getting dbContext/services via contextAccessor.HttpContext.RequestService method. Is that good approach or should I do it another way because it requests service on every webapi method call?
I think that’s the right approach. Requesting services on each action is fine. Also instantiating DbContext is cheap.
But you could use the service provider to request the
CustomDataProvider
and the dependencies will get injected on the constructor, like: