4

How do I properly Activate a configuration that was added using the ServiceCollection.Configure function ?

     public static void Main(args[] args)
     { 
        serviceCollection = new ServiceCollection();
        serviceCollection .Configure<MyOptions>(Configuration.GetSection("options"));
        Services = serviceCollection.BuildServiceProvider();

        ActivatorUtilities.CreateInstance<MyOptions>(Services);
        ActivatorUtilities.CreateInstance<SomeClassThatNeedsoptions>(Services);
     }


 public SomeClassThatNeedsoptions(IOptions<MyOptions> options)
 {
     _options = options.Value;
 }

On the 2nd ActivatorUtilities.CreateInstance I get the following error

Unable to resolve service for type 'Microsoft.Extensions.Options.IOptions [MyOptions]`

I am writing this in a console application and from what I understand am manually injecting the dependencies using the ActivatorUtilities class. I seem to be able to do it with services added with AddSingleton but not with services added with .Configure

cesara
  • 842
  • 1
  • 7
  • 19
  • Your question is bit confusing/unclear. You create `ServiceCollection()` inside main method and build the service provider from it (you do this in console applications), yet your question is tagged ASP.NET Core. What is it now? Console or ASP.NET Core? In ASP.NET core you do that via Startup.cs and `ConfigureServices`/`Configure` methods – Tseng Oct 17 '16 at 11:29
  • Second, why using ActivatorUtilities instead of resolving via `Services.GetService()`? – Tseng Oct 17 '16 at 11:29
  • Hi tseng, sorry for the confusion, I'm really new to aspnetcore. it's a console app using aspnetcore ioc. I thought activator utilities is used to inject the services into the classes (constructor injection) instead of passing the service provider around. – cesara Oct 17 '16 at 13:55
  • See my answer here, http://stackoverflow.com/a/39605743/455493. While its about microsoervices w/o endpoint (which effectively makes it a normal console background service), it shows how to "bootstrap" a console application. Basically you have one class, which starts your application and injects the required services to do the work. From there everything should be resolved by the IoC (via constructor injection or factory pattern). In my example the `MyHost` would be your class, it would have dependencies on something which can have dependencies on their own etc. – Tseng Oct 17 '16 at 14:11
  • Also `Microsoft.Extension.DependencyInjection` is not specific or limited to ASP.NET Core and can be used w/o ASP.NET Core (i.e. Microsoft Orleans uses it too) – Tseng Oct 17 '16 at 14:12
  • Hi Tseng, your example is good but its not exactly what I am looking for. Imagine in your ConfigureServices you add a serviceCollection.Configure(...) instead of Singleton. Now I want to access that configuration in a seperate class with a constuctor parameter of IOption. How does the constructor get the options parameter automatically populated by options that I added in the serviceCollection ? – cesara Oct 17 '16 at 20:19
  • First, options are always singletons (https://github.com/aspnet/Options/blob/dev/src/Microsoft.Extensions.Options.ConfigurationExtensions/OptionsConfigurationServiceCollectionExtensions.cs#L35-L36), second: You resolve the class (not the options) via DI. If you have Class A which has dependency on B and B has dependency on Options, when you resolve A via `services.GetService()`, the `IOpctions` will be resolved, injected in B, B injected in A and A returned from `GetService()` method. That's the whole point of DI. – Tseng Oct 17 '16 at 20:36
  • in ASP.NET Core the framework does resolve the first class (usually controller), in Console you do it yourself. Beyond that, you request dependencies in Constructor or use factory pattern (for "late resolving", when its really required rather than when the class is constructed or when you need runtime parameters) – Tseng Oct 17 '16 at 20:38
  • Hi Tseng, I figured out the problem. I needed to add .AddOptions() to my serviceCollection for the injection to work properly for IOptions. If you want to answer the question I can accept as the answer.Thanks for all your help. – cesara Oct 18 '16 at 15:34

2 Answers2

0

Try adding right after you instantiate ServiceCollection

serviceCollection.AddOptions();
gandarez
  • 2,609
  • 4
  • 34
  • 47
0

Please see this link having a great answer which works for "Access from class library to appsetting.json in Asp.net-core" https://stackoverflow.com/a/39548459/966557?stw=2. It worked for my application.

But please note that I am not sure whether it is applicable for Console based app.

Mohammed Hameed
  • 73
  • 1
  • 10