Welcome to OverStack Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

c# - IServiceProvider that is not registered

When the application starts the error occurs

The constructor of type ImportExportController contains the parameter with name 'serviceProvider' and type IServiceProvider that is not registered. Please ensure IServiceProvider is registered, or change the constructor of ImportExportController

My code

public class ImportExportController : BaseController
{
   private readonly IServiceProvider _serviceProvider;

   public ImportExportController(IServiceProvider serviceProvider)
   {
     _serviceProvider = serviceProvider;
   }
}

for the injection of dependencies I'm using the Simple Injector

The error happens in the container.Verify();

private static void Register(Container container)
{
   _container = container;
   container.Register<IXmlServices, XmlServices>();
   container.Verify();
}

I'm using _serviceProvider here

foreach (var element in productsToImport)
{???????
   using (var scope = _serviceProvider.CreateScope())
   {???????
      var app = scope.ServiceProvider.GetService<IImportApp>();
      var task = Task.Run(() => app.ImportData(element, UserName, ImportID, PricesCurrencies));
      listTask.Add(task);
   }???????
}???????
Task.WaitAll(listTask.ToArray());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to register the IServiceProvider, in the same way you did for IXmlServices.

container.Register<IServiceProvider, ServiceProvider>();

Given that, the ServiceProvider is a class that implements interface IServiceProvider.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OverStack Developer Community-Open, Learning and Share

548k questions

547k answers

4 comments

40.2k users

...