question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Data Provider setup through Dependency Injection

See original GitHub issue

Hi, 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
vukasinpetroviccommented, Apr 2, 2021

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?

image

0reactions
thepirat000commented, Apr 2, 2021

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:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>();
    services.AddSingleton<IMapper, Mapper>();
    services.AddSingleton<CustomDataProvider>();
    services.AddSingleton<EntityFrameworkDataProvider>(new EntityFrameworkDataProvider(...))
    // ...
}

public void Configure(IServiceProvider serviceProvider)
{
    Audit.Core.Configuration.DataProviderFactory = () => serviceProvider.GetRequiredService<CustomDataProvider>();
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found