I am trying to use dependency injection for a .Net Core Console application using the built in DI.
I have 2 following Methods :
private static void RegisterServices()
{
var collection = new ServiceCollection();
//repositories
collection.AddScoped<IAccountDataRepository, AccountDataRepository>();
collection.AddScoped<IClientDataRepository, ClientDataRepository>();
collection.AddScoped<IAddressDataRepository, AddressDataRepository>();
collection.AddScoped<IClientAccountDataRepository, ClientAccountDataRepository>();
//services
collection.AddScoped<IAccountDataService, AccountDataService>();
collection.AddScoped<IClientDataService, ClientDataService>();
collection.AddScoped<IAddressDataService, AddressDataService>();
collection.AddScoped<IClientAccountDataService, ClientAccountDataService>();
_serviceProvider = collection.BuildServiceProvider();
}
private static void DisposeServices()
{
if (_serviceProvider == null)
{
return;
}
if (_serviceProvider is IDisposable)
{
((IDisposable)_serviceProvider).Dispose();
}
}
I can get this to work in the main method by using this:
private static IServiceProvider _serviceProvider;
private static IClientDataRepository _clientDataRepository;
static void Main(string[] args)
{
RegisterServices();
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
However I need to inject the repository to the service and the service to main but I can t use the following in the service class :
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
Here is service:
public class ClientDataService : IClientDataService
{
private readonly ILogger _logger;
private readonly IClientDataRepository _clientDataRepository;
public ClientDataService(ILogger logger, IClientDataRepository clientDataRepository)
{
_logger = logger;
_clientDataRepository = clientDataRepository;
}
If I use
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
will give an error
Just resolve the service and the service provider will inject the repository into the service when building the object graph of the requested object
Based on the provided ClientDataService you would also need to make sure that all dependencies are registered with the service collection.
As it is current shown, ClientDataService also depends on ILogger which does not appear to be registered with the service collection.
services.AddLogging();
The following example uses the originally provided code and refactors to run the main using dependency injection.
public class Program
private readoonly IClientDataService service;
public Program(IClientDataService service) {
this.service = service;
}
public void SomeMethod() {
//...
}
//entry
public static void Main(string[] args) {
IServiceProvider serviceProvider = RegisterServices();
Program program = serviceProvider.GetService<Program>();
program.SomeMethod();
DisposeServices(serviceProvider);
}
//Support
private static IServiceProvider RegisterServices() {
var services = new ServiceCollection();
//repositories
services.AddScoped<IAccountDataRepository, AccountDataRepository>();
services.AddScoped<IClientDataRepository, ClientDataRepository>();
services.AddScoped<IAddressDataRepository, AddressDataRepository>();
services.AddScoped<IClientAccountDataRepository, ClientAccountDataRepository>();
//services
services.AddScoped<IAccountDataService, AccountDataService>();
services.AddScoped<IClientDataService, ClientDataService>();
services.AddScoped<IAddressDataService, AddressDataService>();
services.AddScoped<IClientAccountDataService, ClientAccountDataService>();
services.AddLogging(); //<-- LOGGING
//main
services.AddScoped<Program>(); //<-- NOTE THIS
return services.BuildServiceProvider();
}
private static void DisposeServices(IServiceProvider serviceProvider) {
if (serviceProvider == null) {
return;
}
if (serviceProvider is IDisposable sp) {
sp.Dispose();
}
}
}
Related
I am using Simple Injector to register a concrete type in the container in a .NET Core console app (in Program.cs), but Simple Injector throws an exception on start up:
The constructor of type Application contains the parameter with name 'configUpdater' and type ConfigUpdater, but ConfigUpdater is not registered. For ConfigUpdater to be resolved, it must be registered in the container. An implicit registration could not be made because Container.Options.ResolveUnregisteredConcreteTypes is set to 'false', which is now the default setting in v5. This disallows the container to construct this unregistered concrete type. For more information on why resolving unregistered concrete types is now disallowed by default, and what possible fixes you can apply, see https://simpleinjector.org/ructd
EDIT:
Adding a MRE example which throws the exception:
using System.Threading.Tasks;
using NLog;
using SimpleInjector;
namespace MRE
{
public static class Program
{
private static Container container;
static Program()
{
container = new Container();
container.Register<IApplication, Application>(Lifestyle.Singleton);
var appSettings = new AppSettings();
container.Register(
typeof(AppSettings),
() => appSettings,
Lifestyle.Singleton
);
container.RegisterConditional(
typeof(ILog),
typeCtx => typeof(NLogProxy<>).MakeGenericType(typeCtx.Consumer.ImplementationType),
Lifestyle.Singleton,
predCtx => true
);
container.Register<IConfigUpdater, ConfigUpdater>(Lifestyle.Scoped);
}
public static void Main(string[] args)
{
var application = container.GetInstance<IApplication>();
application.RunAsync();
}
}
public class AppSettings
{
public string ConnectionString { get; set; } = "DataSource=data.db";
}
public interface ILog
{
void Info(string message);
}
public class NLogProxy<T> : ILog
{
private static readonly NLog.ILogger Logger = LogManager.GetLogger(typeof(T).FullName);
public void Info(string message) => Logger.Log(LogLevel.Info, message);
}
public interface IApplication
{
Task RunAsync();
}
public class Application : IApplication
{
private readonly ILog logger;
private readonly IConfigUpdater configUpdater;
public Application(
ILog logger,
IConfigUpdater configUpdater
)
{
this.logger = logger;
this.configUpdater = configUpdater;
}
public Task RunAsync()
{
logger.Info("Running");
configUpdater.DoTask();
return Task.CompletedTask;
}
}
public interface IConfigUpdater
{
Task DoTask();
}
public class ConfigUpdater : IConfigUpdater
{
private readonly AppSettings appSettings;
private readonly ILog logger;
public ConfigUpdater(
AppSettings appSettings,
ILog logger
)
{
this.appSettings = appSettings;
this.logger = logger;
}
public Task DoTask()
{
var connectionString = appSettings.ConnectionString;
logger.Info(connectionString);
return Task.CompletedTask;
}
}
}
EDIT #2:
With the help of the MRE, I discovered my issue was actually hiding behind the scenes. It was a issue with using Lifestyle.Scoped which for some reason was not the first exception thrown. Setting the default lifestyle to AsyncScopedLifestyle fixes it.
With the help of the MRE, I found that the actual error was to do with the default Lifestyle SimpleInjector was using. Adding the line:
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
fixes the issue of this question.
As to why the Lifestyle exception wasn't thrown first, I don't know.
I'm trying to build a console app, using .NET CORE 3.1, and I'm having a problem injecting a console logger.
It looks like the way that logging is injected has changed significantly, in recent versions, and none of the various flavors of instructional tutorials seem to match what I'm trying to do.
I have an interface:
public interface IMyDoer
{
void DoSomething();
}
And a class into which I want to inject an ILogger:
public class MyDoer : IMyDoer
{
private readonly ILogger logger;
public MyDoer(ILogger logger)
{
this.logger = logger;
}
public void DoSomething()
{
this.logger.Log(LogLevel.Information, "Doing something");
}
}
Then I have my Main(), where I'm trying to configure a DI container to construct a Doer object, configuring ILogger to log to the console.
public class Program
{
public static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var serviceProvider serviceCollection.BuildServiceProvider();
var myDoer = serviceProvider.GetService<IMyDoer>();
myDoer.DoSomething();
}
private static void ConfigureServices(ServiceCollection serviceCollection)
{
serviceCollection.AddLogging(configure => {
configure.AddConsole();
});
serviceCollection.AddSingleton<IMyDoer, MyDoer>();
}
}
As an alternative, I've tried:
public class Program
{
static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
using (var serviceProvider = serviceCollection.BuildServiceProvider())
using (var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()))
{
var myDoer = serviceProvider.GetService<IMyDoer>();
myDoer.DoSomething();
}
}
private static void ConfigureServices(ServiceCollection serviceCollection)
{
serviceCollection
.AddSingleton<IMyDoer, MyDoer>();
}
}
In either case, I get an exception:
System.InvalidOperationException
HResult=0x80131509
Message=Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'MyDoer.DoSomething'.
Source=Microsoft.Extensions.DependencyInjection
Any ideas as to how I should be doing this?
ILogger isn't registered with the ASP.NET Core DI container. Instead, use ILogger<T>:
public class MyDoer : IMyDoer
{
private readonly ILogger<MyDoer> logger;
public MyDoer(ILogger<MyDoer> logger)
{
this.logger = logger;
}
// ...
}
ILogger<T> uses the type name (YourNamespace.MyDoer) as the log category:
That category is included with each log message created by that instance of ILogger.
To set your own log category and create an implementation of ILogger yourself, use ILoggerFactory:
public class MyDoer : IMyDoer
{
private readonly ILogger logger;
public MyDoer(ILoggerFactory loggerFactory)
{
this.logger = loggerFactory.CreateLogger("YourCategory");
}
// ...
}
I'm a little confused, I'm registering a RabbitMQ listener in my .NET Core application and its returning a scoping error:
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Cannot consume scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[AuthService.Models.DbEntities.User]' from singleton 'AuthService.Services.RabbitMqListener'.
However, I'm not actually using the UserManager in my service. The consturctor only uses other singleton services:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<RabbitMqListener>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseRabbitListener();
}
public static class ApplicationBuilderExtentions
{
public static RabbitMqListener Listener { get; set; }
public static IApplicationBuilder UseRabbitListener(this IApplicationBuilder app)
{
Listener = app.ApplicationServices.GetService<RabbitMqListener>();
var life = app.ApplicationServices.GetService<IApplicationLifetime>();
life.ApplicationStarted.Register(OnStarted);
life.ApplicationStopping.Register(OnStopping);
return app;
}
private static void OnStarted()
{
Listener.Register();
}
private static void OnStopping()
{
Listener.Deregister();
}
}
Constructor from my RabbitMQ Service:
public class RabbitMqListener
{
private readonly IJWTFactory _jwtFactory;
private readonly IConnection _mqConnection;
private readonly IModel _channel;
private readonly ILogger<RabbitMqListener> _logger;
public RabbitMqListener(
IJWTFactory jwtFactory,
ILogger<RabbitMqListener> logger
)
{
_jwtFactory = jwtFactory;
_mqConnection = new ConnectionFactory() { HostName = "localhost" }.CreateConnection();
_channel = _mqConnection.CreateModel();
_logger = logger;
}
}
Can anyone explain why the UserManager service is being used for my service that isn't using it?
There are lots of articles talking about how to use Structure Map with ASP.NET Core, but not very many talking about console applications or windows services. The default behavior in ASP.Net Core is that StructureMap creates a Nested Container per HTTPRequest so that a concrete class will be instantiated only once per HTTP Request.
I am creating a .Net Core Windows Service using the PeterKottas.DotNetCore.WindowsService nuget package. I setup StructureMap using this article: https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/
My windows service is setup on a Timer and performs an action every X number of seconds. I want each of these actions to use a nested container similar to how ASP.NET does it. In other words, I want everything created for polling pass #1 to be disposed of once that polling pass completes. When polling pass #2 starts I want all new instances of objects to be instantiated. However, within the scope of a single polling pass I only want one instance of each object to be created.
What is the proper way to do this?
Here is my program class
public class Program
{
public static ILoggerFactory LoggerFactory;
public static IConfigurationRoot Configuration;
static void Main(string[] args)
{
var applicationBaseDirectory = AppContext.BaseDirectory;
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (string.IsNullOrWhiteSpace(environment))
throw new ArgumentNullException("Environment not found in ASPNETCORE_ENVIRONMENT");
ConfigureApplication(applicationBaseDirectory, environment);
var serviceCollection = ConfigureServices();
var serviceProvider = ConfigureIoC(serviceCollection);
ConfigureLogging(serviceProvider);
var logger = LoggerFactory.CreateLogger<Program>();
logger.LogInformation("Starting Application");
ServiceRunner<IWindowsService>.Run(config =>
{
var applicationName = typeof(Program).Namespace;
config.SetName($"{applicationName}");
config.SetDisplayName($"{applicationName}");
config.SetDescription($"Service that matches Previous Buyers to Vehicles In Inventory to Fine Upgrade Opportunities.");
config.Service(serviceConfig =>
{
serviceConfig.ServiceFactory((extraArgs, microServiceController) =>
{
return serviceProvider.GetService<IWindowsService>();
});
serviceConfig.OnStart((service, extraArgs) =>
{
logger.LogInformation($"Service {applicationName} started.");
service.Start();
});
serviceConfig.OnStop((service =>
{
logger.LogInformation($"Service {applicationName} stopped.");
service.Stop();
}));
serviceConfig.OnError(error =>
{
logger.LogError($"Service {applicationName} encountered an error with the following exception:\n {error.Message}");
});
});
});
}
private static void ConfigureApplication(string applicationBaseDirectory, string environment)
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
var builder = new ConfigurationBuilder()
.SetBasePath(applicationBaseDirectory)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
private static IServiceCollection ConfigureServices()
{
var serviceCollection = new ServiceCollection().AddLogging().AddOptions();
serviceCollection.AddDbContext<JandLReportingContext>(options => options.UseSqlServer(Configuration.GetConnectionString("JandLReporting")), ServiceLifetime.Transient);
//serviceCollection.AddDbContext<JLMIDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("JLMIDB")), ServiceLifetime.Scoped);
serviceCollection.Configure<TimerSettings>(Configuration.GetSection("TimerSettings"));
serviceCollection.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
return serviceCollection;
}
private static IServiceProvider ConfigureIoC(IServiceCollection serviceCollection)
{
//Setup StructureMap
var container = new Container();
container.Configure(config =>
{
config.Scan(scan =>
{
scan.AssemblyContainingType(typeof(Program));
scan.AssembliesFromApplicationBaseDirectory();
scan.AssembliesAndExecutablesFromApplicationBaseDirectory();
scan.WithDefaultConventions();
});
config.Populate(serviceCollection);
});
return container.GetInstance<IServiceProvider>();
}
private static void ConfigureLogging(IServiceProvider serviceProvider)
{
LoggerFactory = serviceProvider.GetService<ILoggerFactory>()
.AddConsole(Configuration.GetSection("Logging"))
.AddFile(Configuration.GetSection("Logging"))
.AddDebug();
}
}
Here is my WindowsService class:
public class WindowsService : MicroService, IWindowsService
{
private readonly ILogger _logger;
private readonly IServiceProvider _serviceProvider;
private readonly TimerSettings _timerSettings;
public WindowsService(ILogger<WindowsService> logger, IServiceProvider serviceProvider, IOptions<TimerSettings> timerSettings)
{
_logger = logger;
_serviceProvider = serviceProvider;
_timerSettings = timerSettings.Value;
}
public void Start()
{
StartBase();
Timers.Start("ServiceTimer", GetTimerInterval(), async () =>
{
await PollingPassAsyc();
},
(error) =>
{
_logger.LogCritical($"Exception while starting the service: {error}\n");
});
}
private async Task PollingPassAsyc()
{
using (var upgradeOpportunityService = _serviceProvider.GetService<IUpgradeOpportunityService>())
{
await upgradeOpportunityService.FindUpgradeOpportunitiesAsync();
}
}
private int GetTimerInterval()
{
return _timerSettings.IntervalMinutes * 60 * 1000;
}
public void Stop()
{
StopBase();
_logger.LogInformation($"Service has stopped");
}
}
There is extension method CreateScope for IServiceProvider in Microsoft.Extensions.DependencyInjection namespace. What it does is resolve special interface (IServiceScopeFactory) from current DI container, which is responsible for creating new scopes, and creates new scope using this factory. StructureMap registers implementation of this interface, so when you call CreateScope - StructureMap will create nested container. Sample usage:
using (var scope = _serviceProvider.CreateScope()) {
// use scope.ServiceProvider, not _serviceProvider to resolve instance
var service = scope.ServiceProvider.GetService<IUpgradeOpportunityService>();
}
I have a console application that Autofac DI is used to inject data and service layer from web application project.
here is the setup on console application:
public static class ContainerConfig
{
public static IContainer Configure()
{
var builder = new ContainerBuilder();
builder.RegisterType<DbFactory>().As<IDbFactory>();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
builder.RegisterType<Application>().As<IApplication>();
builder.RegisterType<DataRepository>().As<IDataRepository>();
builder.RegisterType<DataService>().As<IDataService>();
return builder.Build();
}
}
public interface IApplication
{
void Run();
}
public class Application : IApplication
{
private readonly IDataService _dataService;
public Application(IDataService dataService)
{
_dataService = dataService;
}
public void Run()
{
var data = _dataService.GetDataById(1);
var task = new TestTask("test");
data.AddTask(task);
_dataService.Update(data);
_dataService.SaveChanges();
}
}
main Program class:
class Program
{
static void Main(string[] args)
{
var container = ContainerConfig.Configure();
using (var scope = container.BeginLifetimeScope())
{
var app = scope.Resolve<IApplication>();
app.Run();
}
}
}
When the application is run loading the data works fine. However, saving a new entry does not seem to do the work.
However, when I remove DI and use simple class initializing in the Run method as below the save works fine:
IDbFactory dbFactory = new DbFactory();
IDataRepository dataRepository = new DataRepository(dbFactory);
var unitOfWork = new UnitOfWork(dbFactory);
IDataService service = new DataService(dataRepository, unitOfWork);
var data = service.GetDataById(1);
var task = new TestTask("test");
data.AddTask(task);
service.Update(data);
service.SaveChanges();
Am I missing something while I setup the autofac? It seems to access the data fine but when it comes to save it does not save the data. I debugged to see any issue but the program runs fine with no error. How can I debug this sort of issues to find more details?
Updated
public interface IDataService
{
void Add(TestTask task);
void SaveChanges();
}
public class DataService : IDataService
{
private readonly IDataRepository _dataRepository;
private readonly IUnitOfWork _unitOfWork;
public DataService(IDataRepository dataRepository, IUnitOfWork unitOfWork)
{
_dataRepository = dataRepository;
_unitOfWork = unitOfWork;
}
public void Add(TestTask task)
{
_dataRepository.Add(task);
}
public void SaveChanges()
{
_unitOfWork.Commit();
}
}
public class UnitOfWork : IUnitOfWork
{
private readonly IDbFactory _dbFactory;
private ApplicationDbContext _dbContext;
public UnitOfWork(IDbFactory dbFactory)
{
this._dbFactory = dbFactory;
}
public ApplicationDbContext DbContext => _dbContext ?? (_dbContext = _dbFactory.Init());
public void Commit()
{
DbContext.Commit();
}
}
After reading autofac scopes here
I found out that default scope is Instance Per Dependency. Which means that a unique instance will be returned from each request for a service. DbFactory should be for InstancePerLifetimeScope.
So changing configuration below fixes the issue:
public static class ContainerConfig
{
public static IContainer Configure()
{
var builder = new ContainerBuilder();
builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerLifetimeScope();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
builder.RegisterType<Application>().As<IApplication>();
builder.RegisterType<DataRepository>().As<IDataRepository>();
builder.RegisterType<DataService>().As<IDataService>();
return builder.Build();
}
}